 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
majkinetor
Joined: 24 May 2006 Posts: 4116 Location: Belgrade
|
Posted: Tue Jul 01, 2008 12:54 pm Post subject: |
|
|
I was looking today how to add text to the buttons along with icon and I found that there is doucmentation error in MSDN (as always... ) that this is natively supported while it is not. Ownerdrawn buttons are the only way, it seems, and I kinda hesistate to do all that for simple thing when its easier to create image with text.
Anyway, while trying to do that i created my own version of this function. Here it is:
| Code: | #singleinstance, force
loop, 5
{
Gui, Add, Button, HWNDhBtn 0x8000 w100 h40
Button_Image(hbtn, A_WinDir "\system32\shell32.dll:" A_Index)
}
gui, show
return
/*-----------------------------------------------------------------------
Function: Image
Adds image to the button
Parameters:
Image - Path to the image file (bmp, ico, dll, icl) or image handle. Specify path:idx to extract icon from the icon resource.
Size - Size of the image, by default 32
Returns:
Handle to the image
*/
Button_Image(hBtn, Image, Size=32){
static BM_SETIMAGE=247, IMAGE_ICON=2, BS_ICON=0x40
if Image is not integer
{
j := InStr(Image, ":", 0, 0), idx := 1
if j > 2
idx := Substr( Image, j+1), pPath := SubStr( Image, 1, j-1)
DllCall("PrivateExtractIcons","str",pPath,"int",idx-1,"int",Size,"int",Size,"uint*",hIco,"uint*",0,"uint",1,"uint",0,"int")
ifEqual, hIco, 0, return A_ThisFunc " >> Can't load image: " Image
} else hIco := Image
WinSet, Style, +%BS_ICON%, ahk_id %hBtn%
SendMessage, BM_SETIMAGE, IMAGE_ICON, hIco, , ahk_id %hBtn%
if ErrorLevel
DllCall("DeleteObject", "UInt", ErrorLevel)
return hImg
} |
This doesn't work:
| MSDN wrote: | The appearance of text, an icon, or both on a button control depends on the BS_ICON and BS_BITMAP styles, and whether the BM_SETIMAGE message is called. The possible results are as follows:
BS_ICON or BS_BITMAP Set? BM_SETIMAGE Called? Result
Yes Yes Show icon only.
No Yes Show icon and text.
Yes No Show text only.
No No Show text only |
_________________
 |
|
| Back to top |
|
 |
tic
Joined: 22 Apr 2007 Posts: 1458
|
Posted: Tue Jul 01, 2008 4:34 pm Post subject: |
|
|
Heh....or you could just use my gdip library which would be better in every way
| Code: | #SingleInstance, Force
#NoEnv
SetBatchLines, -1
#Include, Gdip.ahk
pToken := Gdip_Startup()
pBitmap := Gdip_CreateBitmap(100, 50), G := Gdip_GraphicsFromImage(pBitmap)
hBrush := Gdip_BrushCreateHatch(0xff553323, 0xffc09e8e, 31)
Gdip_FillRectangle(G, hBrush, 0, 0, 100, 50)
Gdip_TextToGraphics(G, "Hello", "Center s20 y25p", "Arial", 100, 50)
hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
Gui, 1: Add, Picture, x10 y10 w100 h50 0xE hwndImage
SetImage(Image, hBitmap)
Gui, 1: Show
Gdip_DeleteGraphics(G), Gdip_DisposeImage(pBitmap), Gdip_DeleteBrush(hBrush), DeleteObject(hBitmap)
Gdip_Shutdown(pToken)
Return |
Obviously hover effects and click effects can be added, but i made that in under a minute |
|
| Back to top |
|
 |
majkinetor
Joined: 24 May 2006 Posts: 4116 Location: Belgrade
|
Posted: Tue Jul 01, 2008 4:57 pm Post subject: |
|
|
Hello tic
This is selfcontained function that uses standard OS mechanism and requires no libraries, doesn't create any globals etc. So its a totaly different thing then that you are presenting.
I would use your library for other things, more adavanced ones.
Thanks for showing example how it could be done with your library. Having images with hardcoded text on them is definitely not good solution as nor text nor image it can be customized. Your thing is surely good solution for those that need such type of customization.
I, muself, never liked images + text on buttons for every day tools and apps. I use either one or another. _________________
 |
|
| Back to top |
|
 |
tic
Joined: 22 Apr 2007 Posts: 1458
|
Posted: Tue Jul 01, 2008 5:13 pm Post subject: |
|
|
Yeh I haven't spoken to you in a while Maj...I've been a lot less "ahk active" of late...
Perhaps you'll like this example more: TextToGraphics
It uses a layered window, which I prefer much more over using standard windows controls. |
|
| Back to top |
|
 |
corrupt
Joined: 29 Dec 2004 Posts: 2446
|
Posted: Tue Jul 01, 2008 6:35 pm Post subject: |
|
|
Thanks for the code maj, tic . I think I might have a bit of code around somewhere for adding text but I don't remember which method was used. I'll see if I can locate it . |
|
| Back to top |
|
 |
majkinetor
Joined: 24 May 2006 Posts: 4116 Location: Belgrade
|
Posted: Tue Jul 01, 2008 7:15 pm Post subject: |
|
|
Thats some nasty shit tic It looks so good.
2 corrupt
Ofcourse, if you have something that doesn't involve drawing but some undocumented OS hack it would be good. Otherwise, its generaly done as Lex posted above. _________________
 |
|
| Back to top |
|
 |
corrupt
Joined: 29 Dec 2004 Posts: 2446
|
Posted: Tue Jul 01, 2008 11:27 pm Post subject: |
|
|
| tic wrote: | Yeh I haven't spoken to you in a while Maj...I've been a lot less "ahk active" of late...
Perhaps you'll like this example more: TextToGraphics
It uses a layered window, which I prefer much more over using standard windows controls. |
| Quote: | ---------------------------
Font does not exist!
---------------------------
The font Garamond was not found on your system
---------------------------
OK
---------------------------
| Win XP Pro SP3
Looks slick though  |
|
| Back to top |
|
 |
tic
Joined: 22 Apr 2007 Posts: 1458
|
Posted: Wed Jul 02, 2008 1:06 am Post subject: |
|
|
| corrupt wrote: | | tic wrote: | Yeh I haven't spoken to you in a while Maj...I've been a lot less "ahk active" of late...
Perhaps you'll like this example more: TextToGraphics
It uses a layered window, which I prefer much more over using standard windows controls. |
| Quote: | ---------------------------
Font does not exist!
---------------------------
The font Garamond was not found on your system
---------------------------
OK
---------------------------
| Win XP Pro SP3
Looks slick though  |
Yeh either download and install Garamond or just change the line:
Thanks for the kind words corrupt
Much crazier things can be created with that library....I wrote that one as a tutorial, but realised it was too hard compared to the other ones I released. For anyone interested here is a screenshot:
 |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 4473 Location: Qld, Australia
|
Posted: Wed Jul 02, 2008 6:33 am Post subject: |
|
|
| majkinetor wrote: | I was looking today how to add text to the buttons along with icon and I found that there is doucmentation error in MSDN (as always... ) that this is natively supported while it is not. | IIRC, it is supported only on Vista. I'd say the documentation is correct, but not complete... |
|
| Back to top |
|
 |
corrupt
Joined: 29 Dec 2004 Posts: 2446
|
Posted: Wed Jul 02, 2008 8:42 am Post subject: |
|
|
- Updated to version 2.2 (details, code, downloads in first post)
- Updated images.dll to support loading/resizing icons also (BMP, GIF, JPG, WMF, EMF, ICO) |
|
| Back to top |
|
 |
Phischi
Joined: 01 Sep 2005 Posts: 5
|
Posted: Thu Aug 14, 2008 1:54 pm Post subject: |
|
|
I spent more than 5 hours now with searching this forum for a solution, but nearly every thread concerning graphic buttons redirected to this one. I would not call myself unexperienced, but I am starting to wonder if I am too dumb for all this...
What I already did is I downloaded version 2.2 and the custom dlls from the starting page.
My Autohotkey version is the latest 1.0.47.06
All I want to have is a single button in my script:
| Code: | Gui, Add, Button, x10 y10 w36 h36,
Gui, Show |
This button should not have any text in it, but Icon 166 from SYSTEM32.DLL
Like that:
Could maybe someone explain in simple words what I need to do for this? Do I need to do something with the dll's? How can I enable commands like "AddGraphicButton(...", if AHK doesn't recognize them?
Any help would be highly appreciated! |
|
| Back to top |
|
 |
garry
Joined: 19 Apr 2005 Posts: 1557 Location: switzerland
|
Posted: Thu Aug 14, 2008 2:44 pm Post subject: |
|
|
@Phischi
another easy idea with gui,add,picture
| Code: |
;-- close with escape --
Gui,1: Color, FFFFFF
Gui,1: -Caption +Border +AlwaysOnTop +ToolWindow +LastFound
Gui,1: Add, Picture, x5 y5 w32 h32 Icon5 vIcon2 gAA, SndVol32.exe
Gui,1: show
Gui,3: Add, Picture, x0 y0 w100 h100 gBB, c:\test1.jpg
Gui,3: Add, Picture, x100 y0 w100 h100 gBB, c:\test2.jpg
Gui,3: show,x200 y50 h120 w230,TEST
return
AA:
run,sndvol32
return
BB:
MsgBox, You clicked %A_GuiControl%
if A_GuiControl=c:\test1.jpg
run,notepad
Return
esc::exitapp
|
|
|
| Back to top |
|
 |
Phischi
Joined: 01 Sep 2005 Posts: 5
|
Posted: Fri Aug 15, 2008 7:36 am Post subject: |
|
|
Thanks garry
But I rather thought of something like this:
| Code: |
; Toggle.ahk - Readymade code for a Graphical Checkbox
Gui, +Sysmenu +ToolWindow
Gui, Margin,12,12
Gui, Add , Picture, x13 y13 w32 h32 E0x200 vState1 icon166 gSubRoutine1, %SystemRoot%\system32\SHELL32.dll
Gui, Add , Picture, x13 y13 w34 h34 Border vState0 icon166 gSubRoutine1, %SystemRoot%\system32\SHELL32.dll
GoSub, Toggle_Switch
Gui, Show, x50 y50 AutoSize, Toggle
Return
Toggle_Switch:
If Toggle=0
{
GuiControl, Hide, State0
GuiControl, Show, State1
Toggle=1
}
Else
{
GuiControl, Hide, State1
GuiControl, Show, State0
Toggle=0
}
Return
SubRoutine1:
GoSub,Toggle_Switch
Return
GuiEscape:
GuiClose:
ExitApp
Return
|
(taken from http://www.autohotkey.com/forum/viewtopic.php?p=59899#59899)
The only thing is the button style isn't really my sort of thing, I would prefer a "real" button and its usual behaviour like in this thread, but I do not understand how to achieve with corrupt's documentation  |
|
| Back to top |
|
 |
garry
Joined: 19 Apr 2005 Posts: 1557 Location: switzerland
|
Posted: Fri Aug 15, 2008 7:50 am Post subject: |
|
|
example above works, seems like you wanted, small modified
(button has 2 states (toggle) ) can modify to one state
| Code: |
Gui, +Sysmenu +ToolWindow
Gui, Margin,12,12
Gui, Add , Picture, x13 y13 w32 h32 E0x200 vState1 icon166 gAAAA, %SystemRoot%\system32\SHELL32.dll
Gui, Show, x50 y50 AutoSize, Example
Return
AAAA:
msgbox,You pressed button
return
GuiClose:
ExitApp
|
toggle:
| Code: | Gui, +Sysmenu +ToolWindow
Gui, Margin,12,12
Gui, Add , Picture, x13 y13 w32 h32 E0x200 vState1 icon166 gSubRoutine1, %SystemRoot%\system32\SHELL32.dll
Gui, Add , Picture, x13 y13 w34 h34 Border vState0 icon166 gSubRoutine1, %SystemRoot%\system32\SHELL32.dll
;GoSub, Toggle_Switch
Gui, Show, x50 y50 AutoSize, Toggle
Return
Toggle_Switch:
If Toggle=0
{
GuiControl, Hide, State0
GuiControl, Show, State1
Toggle=1
}
Else
{
GuiControl, Hide, State1
GuiControl, Show, State0
Toggle=0
}
Return
SubRoutine1:
GoSub,Toggle_Switch
if toggle=0
msgbox,toggle=0
else
msgbox,toggle=1
Return
GuiEscape:
GuiClose:
ExitApp
Return
|
|
|
| Back to top |
|
 |
Phischi
Joined: 01 Sep 2005 Posts: 5
|
Posted: Fri Aug 15, 2008 9:15 am Post subject: |
|
|
Thanks for your efforts, garry!
This is all fine, but as already said it doesn't offer the same possibilities as with real buttons. E.g. if button pressed and mouse moved away, the subroutine is not processed.
That's why I wanted to understand corrupt's graphic button... |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|