AutoHotkey Community

It is currently May 26th, 2012, 8:39 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 111 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6, 7, 8  Next
Author Message
 Post subject:
PostPosted: July 1st, 2008, 12:54 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
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... :roll: ) 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

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 1st, 2008, 4:34 pm 
Offline

Joined: April 22nd, 2007, 6:33 pm
Posts: 1833
Heh....or you could just use my gdip library which would be better in every way :P

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 1st, 2008, 4:57 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
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.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 1st, 2008, 5:13 pm 
Offline

Joined: April 22nd, 2007, 6:33 pm
Posts: 1833
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 1st, 2008, 6:35 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2542
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 :) .


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 1st, 2008, 7:15 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
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.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 1st, 2008, 11:27 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2542
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 8)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 2nd, 2008, 1:06 am 
Offline

Joined: April 22nd, 2007, 6:33 pm
Posts: 1833
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 8)


Yeh either download and install Garamond or just change the line:

Code:
Font = Garamond


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:

Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 2nd, 2008, 6:33 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
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... :roll: ) 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...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 2nd, 2008, 8:42 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2542
- 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)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 14th, 2008, 1:54 pm 
Offline

Joined: September 1st, 2005, 11:04 am
Posts: 5
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:
Image

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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 14th, 2008, 2:44 pm 
Offline

Joined: April 19th, 2005, 10:26 am
Posts: 2249
Location: switzerland
@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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 15th, 2008, 7:36 am 
Offline

Joined: September 1st, 2005, 11:04 am
Posts: 5
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 :(


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 15th, 2008, 7:50 am 
Offline

Joined: April 19th, 2005, 10:26 am
Posts: 2249
Location: switzerland
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 15th, 2008, 9:15 am 
Offline

Joined: September 1st, 2005, 11:04 am
Posts: 5
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...


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 111 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6, 7, 8  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Exabot [Bot], Google Feedfetcher, MSN [Bot], SKAN, Stigg and 21 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group