AutoHotkey Community

It is currently May 24th, 2012, 9:03 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 111 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6 ... 8  Next
Author Message
PostPosted: March 27th, 2007, 5:47 pm 
Offline

Joined: October 5th, 2004, 5:03 pm
Posts: 121
Location: Brasília, Brazil
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%
      }
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 27th, 2007, 6:45 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
hmh... you created 60 global variables for tooltips ...

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 27th, 2007, 7:26 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2537
It's good to see that there's still an interest in this functionality :) .

@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: ;)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 27th, 2007, 11:55 pm 
Offline

Joined: October 5th, 2004, 5:03 pm
Posts: 121
Location: Brasília, Brazil
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."


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2007, 7:59 am 
Offline
User avatar

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

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2007, 9:10 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2537
Updated the first post and included a screenshot and download :) .


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 29th, 2007, 1:58 am 
Offline

Joined: October 5th, 2004, 5:03 pm
Posts: 121
Location: Brasília, Brazil
Corrupt,

Much simplier and much better!!!

Thanks!!
Lingoist


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 30th, 2007, 2:07 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2537
lingoist wrote:
Corrupt,

Much simplier and much better!!!

Thanks!!
Lingoist
Thank you :) . 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...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 30th, 2007, 3:04 am 
Offline

Joined: October 5th, 2004, 5:03 pm
Posts: 121
Location: Brasília, Brazil
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 20th, 2007, 9:49 am 
Offline
User avatar

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 20th, 2007, 10:18 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
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...

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 20th, 2007, 4:19 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2537
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 :) .
  • sample images will be downloaded by the demo script if not found in the Working directory [thanks Skan, PhiLho]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 26th, 2007, 5:49 pm 
Offline

Joined: March 21st, 2006, 8:31 pm
Posts: 49
Location: Slovakia, Europe :)
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:
Image

and the button alone

Image

as you can see its really weird :)

_________________
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")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 26th, 2007, 10:28 pm 
Offline
User avatar

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 25th, 2007, 8:09 pm 
Offline

Joined: November 24th, 2007, 9:07 pm
Posts: 774
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


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 ... 8  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Cerberus, Exabot [Bot], Google Feedfetcher, Uberi and 27 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