让winform中的button控件可以旋转
By
o3o_o3o_o3o
at 2021-08-13 • 1人收藏 • 1134人看过
我们可以根据winform.setPos()函数做出窗口的移动效果,就像分享一个有趣的火柴人。
那我们可不可以让窗口(控件)做出旋转的效果?
代码:
import win.ui; /*DSG{{*/ mainForm = win.form(text="旋转控件";right=959;bottom=591) mainForm.add( button={cls="button";text="旋转";left=563;top=237;right=687;bottom=307;z=1} ) /*}}*/ import gdip; mainForm.add( btndefault = { cls="button";bottom=20;right=20;left=10;top=10;z=1;text="";id=123;autoResize=false; }; btnhover = { cls="button";bottom=2;right=2;left=1;top=1;z=2;text="";id=234;autoResize=false; }; btnactive = { cls="button";bottom=2;right=2;left=1;top=1;z=3;text="";id=234;autoResize=false; }; ) var graphics = gdip.graphics( mainForm ) var pen1 = gdip.pen( 0xFFADADAD, 1, 2/*_GdipUnitPixel*/ ); var pen2 = gdip.pen( 0xFF3995DE, 1, 2/*_GdipUnitPixel*/ ); var pen3 = gdip.pen( 0xFF3879AF, 1, 2/*_GdipUnitPixel*/ ); var brush1 = gdip.solidBrush(0xFFE1E1E1); var brush2 = gdip.solidBrush(0xFFE5F1FB); var brush3 = gdip.solidBrush(0xFFCCE4F7); var brush4 = gdip.solidBrush(0xFF000000); var family = gdip.family("宋体"); var strformat = gdip.stringformat ( ); strformat.align = 0/*_GdipStringAlignmentNear*/; var curFont = family.createFont( 20,0/*_FontStyleRegular*/, 2/*_GdipUnitPixel*/) graphics.smoothingMode = 4/*_GdipSmoothingModeAntiAlias*/ ; graphics.textRenderingHint = 3/*_GdipTextRenderingHintAntiAliasGridFit*/; var str = "控件"; rclayout = gdip.RECTF(100,100,100,100); mainForm.btndefault.oncommand = function(id,event){ graphics.drawRectangle(pen1,100,100,100,100) graphics.fillRectangle(brush1,101,101,99,99) graphics.drawString(str,curFont,rclayout,strformat,brush4); } mainForm.btnhover.oncommand = function(id,event){ graphics.drawRectangle(pen2,100,100,100,100) graphics.fillRectangle(brush2,101,101,99,99) graphics.drawString(str,curFont,rclayout,strformat,brush4); } mainForm.btnactive.oncommand = function(id,event){ graphics.drawRectangle(pen3,100,100,100,100) graphics.fillRectangle(brush3,101,101,99,99) graphics.drawString(str,curFont,rclayout,strformat,brush4); win.msgboxTimeout("旋转控件设置成功","Aardio",500)//oncommand } mainForm.btndefault.hide = true mainForm.btnhover.hide = true mainForm.btnactive.hide = true import mouse.hook; var hook = mouse.hook(); hook.proc = function(msg,x,y,mouseData,injected,flags,timeStamp,extraInfo){ if( injected ) return; var Fx,Fy = mainForm.getPos(); if(((x-Fx-10>=100)&&(x-Fx-10<=200))&&((y-Fy-37>=100)&&(y-Fy-37<=200))){ select(msg) { case 0x201/*_WM_LBUTTONDOWN*/ { mainForm.btnactive.oncommand() } else { mainForm.btnhover.oncommand() } } } else { mainForm.btndefault.oncommand() } } mainForm.onClose = function(hwnd,message,wParam,lParam){ hook.close() } import color; rc = ::RECT(100,100,200,200) mainForm.button.oncommand = function(id,event){ graphics.clear(0xFFF0F0F0) graphics.rotateRect(rc,30) mainForm.btndefault.oncommand()//实际上就是刷新一下画板 for(hwnd,ctrl in mainForm.eachControlEx() ){ ctrl.redraw()//把所有控件都刷新一下,避免被画板盖上。 } } mainForm.show(true); return win.loopMessage();
这个控件我还会持续更新,然后会编写成一个扩展库。大家有什么建议或者什么问题都可以写在评论里并发表。我会慢慢努力的。
3 个回复 | 最后更新于 2021-08-14
2021-08-14
#2
第二个问题已经解决。
winform.plus.onDrawContent函数里有graphics,可以直接用graphics来完成控件旋转的效果。
感谢来自站主的自定义漂亮进度条的方法。
文件:
代码:
main.aardio:
import win.ui; /*DSG{{*/ var mainForm = win.form(text="aardio form";right=488;bottom=397) mainForm.add( button={cls="button";text="Button";left=310;top=308;right=403;bottom=360;z=2}; plus={cls="plus";left=51;top=42;right=351;bottom=342;notify=1;z=1} ) /*}}*/ import gdip; var winform = mainForm.loadForm("/res/1.aau"); mainForm.button.oncommand = function(id,event){ winform.angle += 20 } mainForm.wndproc = function(hwnd,message,wParam,lParam){ winform.setPos(mainForm.getPos()) } winform.plus.oncommand = function( id,event ){ win.msgboxTimeout("旋转控件设置成功!","Aardio",500) win.setForeground(mainForm.hwnd) } winform.plus.onDrawString = function(graphics,text,font,rectf,strformat,brush){ var brush4 = gdip.solidBrush(0xFF000000); var family = gdip.family("宋体"); var strformat = gdip.stringformat ( ); strformat.align = 0/*_GdipStringAlignmentNear*/; var font = family.createFont( 20,1, 2/*_GdipUnitPixel*/) graphics.smoothingMode = 4/*_GdipSmoothingModeAntiAlias*/ ; graphics.textRenderingHint = 3/*_GdipTextRenderingHintAntiAliasGridFit*/; text = "控件"; rectf = gdip.RECTF(winform.plus.width/2-50,winform.plus.height/2-50,100,100); graphics.drawString(text,font,rectf,strformat,brush); } mainForm.show(); win.loopMessage();
1.aau:
import win.ui; /*DSG{{*/ var winform = win.form(text="aardio form";right=488;bottom=397) winform.add( button={cls="button";text="Button";left=418;top=328;right=467;bottom=371;z=2}; plus={cls="plus";left=51;top=42;right=351;bottom=342;notify=1;z=1} ) /*}}*/ import gdip; import win.ui.layered; win.ui.layered(winform) var pen1 = gdip.pen( 0xFFADADAD, 2, 2/*_GdipUnitPixel*/ ); var pen2 = gdip.pen( 0xFF3995DE, 2, 2/*_GdipUnitPixel*/ ); var pen3 = gdip.pen( 0xFF3879AF, 2, 2/*_GdipUnitPixel*/ ); var brush1 = gdip.solidBrush(0xFFE1E1E1); var brush2 = gdip.solidBrush(0xFFE5F1FB); var brush3 = gdip.solidBrush(0xFFCCE4F7); winform.plus.setPos(0,0,400,400) a = 0; winform.angle = 0 winform.plus.onDrawContent = function(graphics,rc,color){ graphics.rotateRect(rc,winform.angle); if(a == 0){ graphics.drawRectangle(pen1,winform.plus.width/2-50,winform.plus.height/2-50,100,100) graphics.fillRectangle(brush1,winform.plus.width/2+1-50,winform.plus.height/2+1-50,100-1,100-1) } elseif(a == 1) { graphics.drawRectangle(pen2,winform.plus.width/2-50,winform.plus.height/2-50,100,100) graphics.fillRectangle(brush2,winform.plus.width/2+1-50,winform.plus.height/2+1-50,100-1,100-1) } elseif(a == 2) { graphics.drawRectangle(pen3,winform.plus.width/2-50,winform.plus.height/2-50,100,100) graphics.fillRectangle(brush3,winform.plus.width/2+1-50,winform.plus.height/2+1-50,100-1,100-1) } } winform.plus.onMouseMove = function(wParam,lParam){ if( wParam & 0x1/*_MK_LBUTTON*/ ){ a = 2; } else { a = 1; } } winform.plus.onMouseHover = function(wParam,lParam){ a = 1; } winform.plus.onMouseDown = function(wParam,lParam){ a = 2; } winform.plus.onMouseUp = function(wParam,lParam){ a = 1; } winform.plus.onMouseLeave = function(wParam,lParam){ a = 0; } winform.plus.startAnimation(1,0) winform.show(); win.loopMessage(); return winform;
2021-08-14
#3
第一个问题已经解决,运用了一些数学上的计算实现了文本居中:
main.aardio的代码更改:
import win.ui; /*DSG{{*/ var mainForm = win.form(text="aardio form";right=488;bottom=397) mainForm.add( button={cls="button";text="Button";left=310;top=308;right=403;bottom=360;z=2}; plus={cls="plus";left=51;top=42;right=351;bottom=342;notify=1;z=1} ) /*}}*/ import gdip; var winform = mainForm.loadForm("/res/1.aau"); mainForm.wndproc = function(hwnd,message,wParam,lParam){ winform.setPos(mainForm.getPos()) } mainForm.button.oncommand = function(id,event){ winform.angle += 20 } winform.plus.oncommand = function( id,event ){ win.msgboxTimeout("旋转控件设置成功!","Aardio",500) win.setForeground(mainForm.hwnd) } winform.plus.onDrawString = function(graphics,text,font,rectf,strformat,brush){ var brush4 = gdip.solidBrush(0xFF000000); var family = gdip.family("宋体"); var strformat = gdip.stringformat ( ); strformat.align = 0/*_GdipStringAlignmentNear*/; txtheight = 20 var font = family.createFont( txtheight,1, 2/*_GdipUnitPixel*/) graphics.smoothingMode = 4/*_GdipSmoothingModeAntiAlias*/ ; graphics.textRenderingHint = 3/*_GdipTextRenderingHintAntiAliasGridFit*/; str = "这次的文本终于居中了,并且实现了文本的自动换行。"; var txtwidth = 0; i = 0; abs = "1" while(abs != ""){ i = i+1; abs = string.charAt(str,i) } i = i-1; for(j=1;i;1){ strr = string.slice(str,j,j,true) byte = string.bytes(strr,1) if(byte >= 2){ txtwidth += txtheight; } else { txtwidth += txtheight/2; } } if(txtwidth >= 100){ int,float = math.modf(txtwidth/100) if(float){ int = int+1; } txtheight = int*(txtheight+(int-1)*0.5/*增加行距*/); txtwidth = 100+10; } rectf = gdip.RECTF(winform.plus.width/2-txtwidth/2,winform.plus.height/2-txtheight/2,txtwidth,txtheight); text = str; graphics.drawString(text,font,rectf,strformat,brush); } mainForm.show(); win.loopMessage();
下一步就是编写一个库了!
登录后方可回帖
这个东西有几个缺点:
旋转控件的文本没有居中。
旋转控件有的时候“不好使”(注意鼠标光标):
不好意思露了半张脸……