找回密码
 加入
搜索
查看: 7687|回复: 16

[GUI管理] 如何创建直角按钮 [已解决]

 火.. [复制链接]
发表于 2011-2-18 09:35:11 | 显示全部楼层 |阅读模式
本帖最后由 yhxhappy 于 2011-2-18 16:02 编辑

使用 GUICtrlCreateButton  创建的按钮默认边角有圆角效果,试过了几种样式都没法做出边角是直角的按钮
如下效果:

以上截图的按钮效果出自http://www.autoitx.com/forum.php ... hlight=%B0%B4%C5%A5
看了代码,觉得写得太过复杂,还请各位帮忙看有没有更简单的方法

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2011-2-18 10:14:08 | 显示全部楼层
怎么没有设置是否为直角, 这个可能跟系统有关吧。 他那个可能是WIN7.  所以才会是直角的吧
他那个函数里是没有对直角与否的可调参数的
 楼主| 发表于 2011-2-18 10:45:30 | 显示全部楼层
本帖最后由 yhxhappy 于 2011-2-18 10:46 编辑

回复 2# chenronting


    我想只用 GUICtrlCreateButton 或 _GUICtrlButton_Create 创建按钮,而不用第三方UDF,不知是否有我未知的样式可以实现直角
一楼截图效果的源码如下:
#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>

Local $hGUI = GUICreate("Funkey's BitBtn-Example", 1060, 270)
Local $IcoPath = StringLeft(@AutoItExe, StringInStr(@AutoItExe, "", 0, -1)) & "Icons\au3.ico"

$BitBtn1 = _GUICtrlCreateBitBtn("Explorer", 10, 10, 200, 25, "shell32.dll", 5)
$BitBtn2 = _GUICtrlCreateBitBtn("Button2", 10, 40, 200, 35, "shell32.dll", 7)
$BitBtn3 = _GUICtrlCreateBitBtn("Button3", 10, 80, 200, 55, "shell32.dll", 10, 20)
$BitBtn4 = _GUICtrlCreateBitBtn("Button4", 10, 140, 200, 55, $IcoPath, 20)
$BitBtn5 = _GUICtrlCreateBitBtn("Papierkorb", 10, 200, 200, 55, "shell32.dll", 32, 30, 20, 0, 20)

$BitBtn6 = _GUICtrlCreateBitBtn("Explorer", 220, 10, 200, 25, "shell32.dll", 5, -1, 0, 1)
$BitBtn7 = _GUICtrlCreateBitBtn("Button7", 220, 40, 200, 35, "shell32.dll", 7, -1, 0, 1)
$BitBtn8 = _GUICtrlCreateBitBtn("Button8", 220, 80, 200, 55, "shell32.dll", 10, 20, 0, 1)
$BitBtn9 = _GUICtrlCreateBitBtn("Button9", 220, 140, 200, 55, $IcoPath, 20, -1, 0, 1)
$BitBtn10= _GUICtrlCreateBitBtn("Papierkorb", 220, 200, 200, 55, "shell32.dll", 32, 30, 20, 1, 20)

$BitBtn11 = _GUICtrlCreateBitBtn("Explorer", 430, 10, 200, 25, "shell32.dll", 5, -1, 0, 2)
$BitBtn12 = _GUICtrlCreateBitBtn("Button12", 430, 40, 200, 35, "shell32.dll", 7, -1, 0, 2)
$BitBtn13 = _GUICtrlCreateBitBtn(@CR & @CR & "Button13", 430, 80, 200, 55, "shell32.dll", 10, 20, 0, 2)
$BitBtn14 = _GUICtrlCreateBitBtn(@CR & @CR & "Button14", 430, 140, 200, 55, $IcoPath, 20, 30, 0, 2, 10)
$BitBtn15 = _GUICtrlCreateBitBtn(@CR & "Papierkorb", 430, 200, 200, 55, "shell32.dll", 32, 20, 20, 2, 15)

$BitBtn16 = _GUICtrlCreateBitBtn("Explorer", 640, 10, 200, 25, "shell32.dll", 5, -1, 0, 3)
$BitBtn17 = _GUICtrlCreateBitBtn("Button17", 640, 40, 200, 35, "shell32.dll", 7, -1, 0, 3)
$BitBtn18 = _GUICtrlCreateBitBtn("Button18", 640, 80, 200, 55, "shell32.dll", 10, 20, 0, 3, 15)
$BitBtn19 = _GUICtrlCreateBitBtn("Button19", 640, 140, 200, 55, $IcoPath, 20, 35, 0, 3)
$BitBtn20 = _GUICtrlCreateBitBtn("Papierkorb", 640, 200, 200, 55, "shell32.dll", 32, -1, 20, 3, 20)

$BitBtn21 = _GUICtrlCreateBitBtn("", 850, 10, 200, 25, "shell32.dll", 5, -1, 0, 4)
$BitBtn22 = _GUICtrlCreateBitBtn("", 850, 40, 200, 35, "shell32.dll", 7, -1, 0, 4)
$BitBtn23 = _GUICtrlCreateBitBtn("", 850, 80, 200, 55, "shell32.dll", 10, 20, 0, 4)
$BitBtn24 = _GUICtrlCreateBitBtn("", 850, 140, 200, 55, $IcoPath, 20, -1, 0, 4)
$BitBtn25 = _GUICtrlCreateBitBtn("Papierkorb", 850, 200, 200, 55, "shell32.dll", 32, -1, 20, 4, 20)

GUISetState()

Local $msg
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit
    For $i = 1 To 25
        If $msg = Eval("BitBtn" & $i) Then ConsoleWrite("$BitBtn"&$i& ": " & _BitBtnClick(Eval("BitBtn" & $i)) & @CR)
    Next
    Sleep(10)
WEnd

Func _GUICtrlCreateBitBtn($sText, $iLeft, $iTop, $iWidth, $iHeight, $sImg, $iIndex = 0, $iMaxIconHeight = -1, $iDist = 0, $nAllign = 0, $iFontSize = 8.5, $iFontWeight = Default)
    ;funkey 16.12.2009
    Local $iOffsetX, $iOffsetY, $LabelStyle = 0x201
    Local $a = _GetTextSize("a  a", $iFontSize)
    Local $b = _GetTextSize("aa", $iFontSize)
    Local $SpaceLen = ($a[0] - $b[0]) / 5
    Local $aSize = _GetTextSize($sText, $iFontSize)
    Local $iIconHeight = $iMaxIconHeight
    If $iMaxIconHeight = -1 Or $iMaxIconHeight >  $iHeight - 8 Then
        $iIconHeight = $iHeight - 8
    EndIf
    Local $iSpaces = Ceiling(($iIconHeight + $iDist) / $SpaceLen)
    Switch $nAllign
        Case 0  ;Icon is left
            $iOffsetX = $iWidth / 2 - $aSize[0] / 2 - $iIconHeight / 2 - $iDist / 2
            $iOffsetY = ($iHeight - $iIconHeight) / 2
            $sText = StringFormat("%" & $iSpaces + StringLen($sText) & "s", $sText)
        Case 1  ;Icon is right
            $iOffsetX = $iWidth / 2 + $aSize[0] / 2 - $iIconHeight / 2 + $iDist / 2
            $iOffsetY = ($iHeight - $iIconHeight) / 2
            $sText = StringFormat("%" & -$iSpaces - StringLen($sText) & "s", $sText)
        Case 2  ;Icon is on the top
            $iOffsetX = $iWidth / 2 - $iIconHeight / 2
            $iOffsetY = 4
            $LabelStyle = 0x1
        Case 3  ;Icon is on the bottom
            $iOffsetX = $iWidth / 2 - $iIconHeight / 2
            $iOffsetY = $iHeight - $iIconHeight -4
            $LabelStyle = 0x1
        Case 4 ;Icon is in the middle
            $iOffsetX = $iWidth /2 - $iIconHeight / 2 + $iDist / 2
            $iOffsetY = ($iHeight - $iIconHeight) / 2
    EndSwitch
    Local $hBtn = GUICtrlCreateLabel("", $iLeft, $iTop, $iWidth, $iHeight)
    GUICtrlCreateIcon($sImg, $iIndex, $iLeft + $iOffsetX, $iTop + $iOffsetY, $iIconHeight, $iIconHeight, 0)
    GUICtrlCreateLabel($sText, $iLeft, $iTop, $iWidth, $iHeight, $LabelStyle, 0x200)
    GUICtrlSetBkColor(-1, -2) ; $GUI_BKCOLOR_TRANSPARENT
    GUICtrlSetFont(-1, $iFontSize, $iFontWeight)
    GUICtrlSetState(-1, 32) ;$GUI_HIDE
    GUICtrlCreateLabel($sText, $iLeft, $iTop, $iWidth, $iHeight, $LabelStyle, 0x001)
    GUICtrlSetBkColor(-1, -2) ; $GUI_BKCOLOR_TRANSPARENT
    GUICtrlSetFont(-1, $iFontSize, $iFontWeight)
    Return $hBtn
EndFunc   ;==>_GUICtrlCreateBitBtn

Func _BitBtnClick($IdBtn)
    ;funkey 16.12.2009
    Local $aInfo, $aInfoLast
    GUICtrlSetState($IdBtn + 3, 32) ;$GUI_HIDE
    GUICtrlSetState($IdBtn + 2, 16) ;$GUI_SHOW
    $aInfoLast = GUIGetCursorInfo()
    Do
        Sleep(10)
        $aInfo = GUIGetCursorInfo()
        If @error Then Return 0
        If $aInfo[4] <> $aInfoLast[4] Then ;if changing the hover-control
            If $aInfo[4] < $IdBtn Or $aInfo[4] > $IdBtn + 3 Then ;if not hovering the BitBtn
                GUICtrlSetState($IdBtn + 2, 32) ;$GUI_HIDE
                GUICtrlSetState($IdBtn + 3, 16) ;$GUI_SHOW
            Else
                GUICtrlSetState($IdBtn + 3, 32) ;$GUI_HIDE
                GUICtrlSetState($IdBtn + 2, 16) ;$GUI_SHOW
            EndIf
        EndIf
        $aInfoLast = $aInfo
    Until Not $aInfo[2]
    GUICtrlSetState($IdBtn + 2, 32) ;$GUI_HIDE
    GUICtrlSetState($IdBtn + 3, 16) ;$GUI_SHOW
    If $aInfo[4] <> $IdBtn + 2 And $aInfo[4] <> $IdBtn + 1 Then Return 0
    Return 1
EndFunc   ;==>_BitBtnClick

Func _GetTextSize($nText, $iFontSize = 8.5, $sFont = 'Microsoft Sans Serif', $iFontAttributes = 0)
 ;Author: Bugfix
 ;Modified: funkey
 If $nText = '' Then Return
 _GDIPlus_Startup()
 Local $hFormat = _GDIPlus_StringFormatCreate(0)
 Local $hFamily = _GDIPlus_FontFamilyCreate($sFont)
 Local $hFont = _GDIPlus_FontCreate($hFamily, $iFontSize, $iFontAttributes, 3)
 Local $tLayout = _GDIPlus_RectFCreate(15, 171, 0, 0)
 Local $hGraphic = _GDIPlus_GraphicsCreateFromHWND(0)
 Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $nText, $hFont, $tLayout, $hFormat)
 Local $iWidth = Ceiling(DllStructGetData($aInfo[0], "Width"))
 Local $iHeight = Ceiling(DllStructGetData($aInfo[0], "Height"))
 _GDIPlus_StringFormatDispose($hFormat)
 _GDIPlus_FontDispose($hFont)
 _GDIPlus_FontFamilyDispose($hFamily)
 _GDIPlus_GraphicsDispose($hGraphic)
 _GDIPlus_Shutdown()
 Local $aSize[2] = [$iWidth, $iHeight]
 Return $aSize
EndFunc   ;==>_GetTextSize
发表于 2011-2-18 11:01:43 | 显示全部楼层
回复 3# yhxhappy


    他这里面没有调整是否是直角的参数, 我也无法理解
发表于 2011-2-18 11:25:17 | 显示全部楼层
xp下,GUICtrlCreateButton建的那有圆角效果???
[au3]
GUICreate("test")
GUICtrlCreateButton("1", 10, 10, 50, 20)
GUICtrlCreateButton("2", 10, 50, 50, 20)
GUISetState()

While 1

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2011-2-18 11:29:36 | 显示全部楼层
倒是我为了写这样的圆角效果,老费劲了

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2011-2-18 11:33:38 | 显示全部楼层
本帖最后由 lanfengc 于 2011-2-18 12:04 编辑

他创建的是个标签控件

程序运行机制:
上面的创建控件有个取巧的地方。 创建的是3个标签控件,形象的比喻下,就好比是3张纸,从上到下3张纸,最上面的那张是凸出来的,中间的是平整的,后面的是凹下去的
当发生鼠标左键消息的时候,前面的两张隐藏,第三张显示出来,GUI上显示出的就是一个按钮按下的状态。其实是个标签的样式。
松开鼠标左键时候,前面的两张再显示出来,GUI上显示的就是按钮凸出的状态。 也是个标签的样式。
表面上,中间的那张没起作用。其实是创建了一个按钮初始化的状态, 防止出现点击按钮了,按钮陷下去弹不上来的状态。
好了,按钮的GUI效果就这样创建完成。 接下来就是消息循环了。
消息循环中,通过FOR循环遍历控件ID,看是否和当前消息ID相同,相同则执行相应操作。
按钮的响应函数 其实就是遵循了上面的操作。 对前两张显示或隐藏。
最后的那个函数是获取一段字符串在屏幕上显示的这个矩形区域的宽和高。 相当于API中的GetTextMetrics。

以上是我的一点见解,不知道准确不。 如有什么不对,欢迎指正,让我也学习下。
发表于 2011-2-18 11:36:33 | 显示全部楼层
回复 7# lanfengc

一个意思,做的时候,只是一个找窗口句柄,一个找控件句柄而已
发表于 2011-2-18 11:38:03 | 显示全部楼层
正常GUI的形态都是由系统主题决定的,除非自建的UI
 楼主| 发表于 2011-2-18 11:51:19 | 显示全部楼层
本帖最后由 yhxhappy 于 2011-2-18 11:58 编辑

回复 5# happytc

想实现一个在不同系统上都可以的用的,如下图的效果,有没有阴影都没有关系


我想 Pic 加上 Label 做也可以实现,只是想找找更简单的方法而已
如果Label可以垂直居中,那做起来就更简单了,但我不会

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2011-2-18 12:07:57 | 显示全部楼层
找到垂直居中的办法了,贴出来

#include <StaticConstants.au3>

$hGUI = GUICreate("Test", 200, 100)
GUICtrlCreateLabel("SIGN IN", 20, 20, 160, 40, $SS_CENTER + $SS_CENTERIMAGE)
GUICtrlSetFont(-1, 12, 400, 0, "Arial")
GUICtrlSetColor(-1,0xffffff)
GUICtrlSetBkColor(-1, 0x88b131)
GUISetState()

Do
Until guiGetMsg() = -3
发表于 2011-2-18 12:54:02 | 显示全部楼层
学习了.创建label加样式了?
发表于 2011-2-18 13:58:58 | 显示全部楼层
不错,正好我也学习了,谢谢
 楼主| 发表于 2011-2-18 14:00:07 | 显示全部楼层
再改进了一些。

增加了点击后的阴影,如上图。但是第二个按钮添加立体效果时,感觉有一条白边,不知道如何弄细一些或者去掉
还有,为什么我用Chrome浏览器无法上传图片呢,点击上传后就一直没反应了
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>

$GUI = GUICreate("Test", 200, 140)
$Button  = GUICtrlCreateLabel("SIGN IN", 20, 20, 160, 40, $SS_CENTER + $SS_CENTERIMAGE);, -1, 0x001);, $SS_CENTER + $SS_CENTERIMAGE, 0x001)
        GUICtrlSetFont(-1, 14, 400, 0, "Arial")
        GUICtrlSetColor(-1, 0xffffff)
        GUICtrlSetBkColor(-1, 0x88b131)
$Button2  = GUICtrlCreateLabel("SIGN IN", 20, 70, 160, 40, $SS_CENTER + $SS_CENTERIMAGE, 0x001);, $SS_CENTER + $SS_CENTERIMAGE, 0x001)
        GUICtrlSetFont(-1, 14, 400, 0, "Arial")
        GUICtrlSetColor(-1, 0xffffff)
        GUICtrlSetBkColor(-1, 0x88b131)


GUISetState()

While 1
        Switch GUIGetMsg()
        Case -3
                Exit
        Case $Button
                GUICtrlSetStyle($Button, -1, 0x200)
                While 1
                        If GUIGetMsg() = $GUI_EVENT_PRIMARYUP Then
                                GUICtrlSetStyle($Button, -1, 0)
                                ExitLoop
                        EndIf
                WEnd
        Case $Button2
                GUICtrlSetStyle($Button2, -1, 0x200)
                While 1
                        If GUIGetMsg() = $GUI_EVENT_PRIMARYUP Then
                                GUICtrlSetStyle($Button2, -1, 0x001)
                                ExitLoop
                        EndIf
                WEnd
        EndSwitch
WEnd

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2011-2-18 14:11:56 | 显示全部楼层
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-10-2 14:28 , Processed in 0.097763 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表