AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Graphic Buttons
Goto page Previous  1, 2, 3, 4, 5, 6  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
lingoist



Joined: 05 Oct 2004
Posts: 121
Location: Brasília, Brazil

PostPosted: Tue Mar 27, 2007 4:47 pm    Post subject: GRAPHIC BUTTON WITH TOOLTIP Reply with quote

I've improved the function and added optional ToolTip to Graphic Buttons.

There is a bug in putting text in Groupboxes, Buttons or Radios.

Code:

   
#SingleInstance force
   
;PROGRAM TITLE
   ProgramTitle = Gui_Add_GraphicButton_Function

;GUI SHOW + HIDE
   Gui, Show, w650 h200 +Hide, %ProgramTitle%
   
;SET BUTTONS       ;DON'T PUT ANY TEXT IN BUTTONS, RADIOS OR GROUPBOXES BEFORE CREATING GRAPHIC BUTTONS
loop 15
   GuiAddGraphicButton("h30 w30 xp+30 GMyButton", "D:\_kirk\img_kirk\icone_vazio.bmp", ProgramTitle, "ToolTip which will be showed")

;GUI SHOW AGAIN
   Gui, Show, , %ProgramTitle%
   Return

   
MyButton:
MsgBox, Graphic button clicked :)
return

GuiClose:
ExitApp
Return
      
GuiAddGraphicButton(GuiAddButtonOptions, ImgPath, WindowTitle, ToolTipText="", GuiNumber=1, ImgHeight=26, ImgWidth=26) {
   { ;DETAILS
      ; ImgPath = Path to the image to be displayed
      ; ImgHeight = Image height (default = 32)
      ; ImgWidth = Image width (default = 32)
   }
   { ;GLOBALS AND BUTTON
      DetectHiddenWindows, On
      HWND := WinExist(WindowTitle)
      BS_BITMAP := 128
      IMAGE_BITMAP := 0
      BS_ICON := 64
      IMAGE_ICON := 1
      IfNotExist %ImgPath%
         ImgPath = img_kirk\%ImgPath%
   }
   { ;CHOOSE .ICO OU .BMP
      If ImgPath contains .ico
      {
         bs_chosen = %BS_ICON%
         ImgType=1
      }
      Else If ImgPath contains .bmp
      {
         bs_chosen = %BS_BITMAP%
         ImgType = 0
      }
      ;Gui, Add, Button, %GuiAddButtonOptions% +%bs_chosen%
   }
   { ;ADD BUTTON
      Gui, %GuiNumber%:Add, Button, %GuiAddButtonOptions% +%bs_chosen%
   }
   { ;SEARCH LAST BUTTON
      WinGet, ControlList, ControlList, %WindowTitle%
      Loop, parse, ControlList, `n
      {
       If A_LoopField not contains Button
         Continue
       StringReplace, butt, A_LoopField, Button, , all
       ac_buttons = %ac_buttons%|%butt%
       ;MsgBox %A_LoopField%
      }
      StringTrimLeft, ac_buttons, ac_buttons, 1
      Sort, ac_buttons, D| R N
      Loop, parse, ac_buttons, |
      {
       last_button = %A_LoopField%
       Break
      }
      ;MsgBox %last_button%
      CtrlInstance = %last_button%
   }
   { ;TOOLTIP - GLOBALS
      global Button1
      global Button2
      global Button3
      global Button4
      global Button5
      global Button6
      global Button7
      global Button8
      global Button9
      global Button10
      global Button11
      global Button12
      global Button13
      global Button14
      global Button15
      global Button16
      global Button17
      global Button18
      global Button19
      global Button20
      global Button21
      global Button22
      global Button23
      global Button24
      global Button25
      global Button26
      global Button27
      global Button28
      global Button29
      global Button30
      global Button31
      global Button32
      global Button33
      global Button34
      global Button35
      global Button36
      global Button37
      global Button38
      global Button39
      global Button40
      global Button41
      global Button42
      global Button43
      global Button44
      global Button45
      global Button46
      global Button47
      global Button48
      global Button49
      global Button50
      global Button51
      global Button52
      global Button53
      global Button54
      global Button55
      global Button56
      global Button57
      global Button58
      global Button59
   }
   { ;TOOLTIP
      Button%last_button% = %ToolTipText%
   }
   { ;DLL CALL
   ; Set Constants
      NULL=
      LR_LOADFROMFILE := 16
      BM_SETIMAGE := 247

   ; Find the handle to the Button based on the windle handle and button number
     ;msgbox %CtrlInstance%
      Loop, %CtrlInstance%
      {
        CtrlHandle := DllCall("FindWindowExA", "Uint", HWND, "Uint", CtrlHandle, "str", "Button", "str", NULL, "Uint")
        If CtrlHandle = 0
          Return
      }

   ; Load the image from the file and retrieve the image handle
      ImgHwnd%CtrlInstance% := DllCall("LoadImage", "UInt", NULL, "Str", ImgPath, "UInt", ImgType, "Int", ImgWidth, "Int", ImgHeight, "UInt", LR_LOADFROMFILE, "UInt")

   ; Assign the image to the button
      DllCall("SendMessage", "UInt", CtrlHandle, "UInt", BM_SETIMAGE, "UInt", ImgType,  "UInt", ImgHwnd%CtrlInstance%)

   ; Return the handle to the image
      retImg := ImgHwnd%CtrlInstance%
   }
   Return, %retImg%
}
GraphicButtonToolTip() {
   ;CONTROL UNDER MOUSE
      MouseGetPos,,,, ACtrl
   ;TEXT ASSOCIATED
      If ACtrl contains Button
         Tip := %ACtrl%
    ;TOOLTIP IF DIFFERENT
      If ACtrl <> %LastCtrl%
      {
         ToolTip, %Tip%
         LastCtrl = %ACtrl%
      }
}
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 4114
Location: Belgrade

PostPosted: Tue Mar 27, 2007 5:45 pm    Post subject: Reply with quote

hmh... you created 60 global variables for tooltips ...
_________________
Back to top
View user's profile Send private message
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Tue Mar 27, 2007 6:26 pm    Post subject: Reply with quote

It's good to see that there's still an interest in this functionality Smile .

@lingoist - Thanks for the code. I'l have a closer look... The code I had originally posted is a bit outdated now and was originally intended more as a "proof of concept" than a generic function. I hadn't updated the code since it seemed likely that the functionality might get added to AHK... Twisted Evil Wink
Back to top
View user's profile Send private message Visit poster's website
lingoist



Joined: 05 Oct 2004
Posts: 121
Location: Brasília, Brazil

PostPosted: Tue Mar 27, 2007 10:55 pm    Post subject: Reply with quote

majkinetor wrote:
hmh... you created 60 global variables for tooltips ...


Yes... I'd like to use only one global, but the code above is not possible:


Code:
global Button%last_button%


Help AHK says: "a function can create a global array manually (by means such as Array%i% := A_Index) only if it has been defined as an assume-global function."
Back to top
View user's profile Send private message
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Wed Mar 28, 2007 6:59 am    Post subject: Reply with quote

Here's an updated version of the original script that should offer a lot more flexibility. The AddGraphicButton function can be used instead of Gui, add, button for creating graphic buttons. Please see the comments in the function and example for usage. Comments/suggestions welcome Smile .

Code:
; Create buttons
 
Gui, Add, Button, h30 w140 gNbutton, Normal Button
AddGraphicButton("SampleButton1", A_WorkingDir . "\testbmp.bmp", "h30 w140 gMyButton", 30, 140)
AddGraphicButton("SampleButton2", A_WorkingDir . "\ahk.bmp", "h30 w140 gMyButton", 80, 140)
Gui, Add, Button, h30 w140 gNbutton, Another Normal Button
AddGraphicButton("SampleButton3", A_WorkingDir . "\test2.bmp", "h30 w140 gMyButton", 20, 130)

; Show the window
Gui, Show,, Bitmap Buttons
Return


AddGraphicButton(VariableName, ImgPath, Options="", bHeight=32, bWidth=32)
{
Global
Local ImgType, ImgType1, LR_LOADFROMFILE, NULL, BM_SETIMAGE
; *******************************************************************
; Version: 2.0 Updated: March 28, 2007
; by corrupt
; *******************************************************************
; VariableName = variable name for the button
; ImgPath = Path to the image to be displayed
; Options = AutoHotkey button options (g label, button size, etc...)
; bHeight = Image height (default = 32)
; bWidth = Image width (default = 32)
; *******************************************************************
; BS_BITMAP := 128, IMAGE_BITMAP := 0, BS_ICON := 64, IMAGE_ICON := 1
LR_LOADFROMFILE := 16
BM_SETIMAGE := 247
NULL=
SplitPath, ImgPath,,, ImgType1
ImgTYpe := (ImgType1 = "bmp") ? 128 : 64
Gui, Add, Button,  v%VariableName% hwnd%VariableName%_hwnd +%ImgTYpe% %Options%
ImgType := (ImgType1 = "bmp") ? 0 : 1
%VariableName%_img := DllCall("LoadImage", "UInt", NULL, "Str", ImgPath, "UInt", ImgType, "Int", bWidth, "Int", bHeight, "UInt", LR_LOADFROMFILE, "UInt")
DllCall("SendMessage", "UInt", %VariableName%_hwnd, "UInt", BM_SETIMAGE, "UInt", ImgType,  "UInt", %VariableName%_img)
Return, %VariableName%_img ; Return the handle to the image
}

MyButton:
MsgBox, Graphic button clicked :)
return

Nbutton:
MsgBox, Normal button Clicked :)
Return

GuiClose:
ExitApp
Back to top
View user's profile Send private message Visit poster's website
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Wed Mar 28, 2007 8:10 am    Post subject: Reply with quote

Updated the first post and included a screenshot and download Smile .
Back to top
View user's profile Send private message Visit poster's website
lingoist



Joined: 05 Oct 2004
Posts: 121
Location: Brasília, Brazil

PostPosted: Thu Mar 29, 2007 12:58 am    Post subject: Reply with quote

Corrupt,

Much simplier and much better!!!

Thanks!!
Lingoist
Back to top
View user's profile Send private message
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Fri Mar 30, 2007 1:07 am    Post subject: Reply with quote

lingoist wrote:
Corrupt,

Much simplier and much better!!!

Thanks!!
Lingoist
Thank you Smile . Added credit for contributing code to the code in the first post.

After having another look it seems that the code you posted will offer a bit more flexibility if merged with the last version I posted as your version supports adding to other GUI windows and supports adding tooltips. I think both versions still look a bit more complicated than necessary to me though...
Back to top
View user's profile Send private message Visit poster's website
lingoist



Joined: 05 Oct 2004
Posts: 121
Location: Brasília, Brazil

PostPosted: Fri Mar 30, 2007 2:04 am    Post subject: Reply with quote

Corrupt,

I abandoned my last code to use version 2.00!!! I just added Tooltip (6th parameter) and GuiNumber option (7th parameter). The fact of using Global in the fist line of the ToolTip function saved me 60 lines...!!!

Thanks!!!
lingoist
Back to top
View user's profile Send private message
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Sun May 20, 2007 8:49 am    Post subject: Reply with quote

Updated to version 2.1
  • added the ability to change images [thanks cynicmusic]
  • added image rollover example for SampleButton1 in the demo script [thanks cynicmusic]
  • sample images will be downloaded by the demo script if not found in the Working directory [thanks PhiLho]
Back to top
View user's profile Send private message Visit poster's website
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Sun May 20, 2007 9:18 am    Post subject: Reply with quote

corrupt wrote:
sample images will be downloaded by the demo script if not found in the Working directory [thanks PhiLho][/list]
The first time I saw the trick, it was made by Skan...
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Sun May 20, 2007 3:19 pm    Post subject: Reply with quote

PhiLho wrote:
corrupt wrote:
sample images will be downloaded by the demo script if not found in the Working directory [thanks PhiLho][/list]
The first time I saw the trick, it was made by Skan...
I didn't realize that. Thanks Smile .
  • sample images will be downloaded by the demo script if not found in the Working directory [thanks Skan, PhiLho]
Back to top
View user's profile Send private message Visit poster's website
Fuco



Joined: 21 Mar 2006
Posts: 49
Location: Slovakia, Europe :)

PostPosted: Thu Jul 26, 2007 4:49 pm    Post subject: Reply with quote

well, after two years, this is still not included ;D

anyway, my question is, can i somehow disable drawing a border around button ?

here is the window:


and the button alone



as you can see its really weird Smile
_________________
RegExReplace("C:\Program Files\AutoHotkey", "(^C)(?=\W).{4}((?i)[GOD])\w{1,4}\s(\D)(?:\w+)*\\(?3)(u|o).*?(k).*" , "$3$4$l1$5$2")
Back to top
View user's profile Send private message
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Thu Jul 26, 2007 9:28 pm    Post subject: Reply with quote

It might be possible to change this in the next version. Until then, you could try to increase the size to the image (bHeight, bWidth options) to be the same size or larger than the size of the button.
Back to top
View user's profile Send private message Visit poster's website
bmcclure



Joined: 24 Nov 2007
Posts: 766

PostPosted: Sun Nov 25, 2007 7:09 pm    Post subject: Reply with quote

Is there any way to have an option to set the transparent color of the button to the top left pixel? My buttons are rounded and my backgrounds aren't always the same color, and currently that looks unseemly.

If the function supported PNGs the image itself could most likely set the transparent color, but I'm not sure how to do it with a bitmap. Thanks,

Ben
_________________
Ben

My Trac projects
My Wiki
[Broken] - My music
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6  Next
Page 3 of 6

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group