AutoHotkey Community

It is currently May 26th, 2012, 9:46 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 111 posts ]  Go to page 1, 2, 3, 4, 5 ... 8  Next
Author Message
 Post subject: Graphic Buttons
PostPosted: June 16th, 2005, 4:59 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2543
Here's a generic function that can be used to add graphic buttons to AutoHotkey GUI scripts that should offer a bit of flexibility. Enjoy :)

Image

Download version 2.2 (sample scripts, functions)
Download custom dll (Includes a function for extracting images in the dll - Use ResHacker to add images - type: RCDATA)

Updated to version 2.2

  • compatible with previous release (2.1)
  • added support for image types: .bmp, .gif, .jpg, .wmf, .emf, .ico when loaded from a custom dll file
  • added a download for the custom dll file that contains a function for retrieving images stored in the dll
  • added option to resize icons using custom dll method
  • added AGB.ahk file - containing optional functions for loading images for use with the AddGraphicButton function
  • added the ability to change images without having to load from source each time (specify image HWND)
  • added a second demo script
version 2.1
  • simplified usage
  • image types currently supported: .bmp, .ico
  • added the ability to change images
  • added image rollover example for SampleButton1 in the demo script
  • sample images will be downloaded by the script if not found in the Working directory


Code:
; ********************************
; Basic Demo Script (download contains additional functionality)
; ********************************

; Download sample images if necessary
IfNotExist %A_WorkingDir%\testbmp.bmp
{
    SplashTextOn, 300, 30, !, Downloading images. Please wait...
   URLDownloadToFile http://www.autohotkey.net/~corr/ahk.bmp, ahk.bmp
   URLDownloadToFile http://www.autohotkey.net/~corr/testbmp.bmp, testbmp.bmp
   URLDownloadToFile http://www.autohotkey.net/~corr/test2.bmp, test2.bmp
    SplashTextOff
}
; Create the 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

; Image rollover for SampleButton1
OnMessage(0x200, "MouseMove")
OnMessage(0x2A3, "MouseLeave")
OnMessage(0x202, "MouseLeave") ; Restore image on LBUTTONUP
Return

MouseLeave(wParam, lParam, msg, hwnd)
{
  Global
  If (hwnd = SampleButton1_hwnd)
    AddGraphicButton("SampleButton1", A_WorkingDir . "\testbmp.bmp", "h30 w140 gMyButton", 30, 140)
  Return
}
MouseMove(wParam, lParam, msg, hwnd)
{
  Global
  Static _LastButtonData = true
  If (hwnd = SampleButton1_hwnd)
    If (_LastButtonData != SampleButton1_hwnd)
      AddGraphicButton("SampleButton1", A_WorkingDir . "\ahk.bmp", "h30 w140 gMyButton", 60, 120)
  _LastButtonData := hwnd
  Return
}

MyButton:
MsgBox, Graphic button clicked :)
return

Nbutton:
MsgBox, Normal button Clicked :)
AddGraphicButton("SampleButton3", A_WinDir . "\clouds.bmp", "h30 w140 gMyButton", 20, 130)
Return

GuiClose:
ExitApp



; *******************************************************************
; AddGraphicButton.ahk
; *******************************************************************
; Version: 2.2 Updated: May 20, 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)
; *******************************************************************
; note:
; - calling the function again with the same variable name will
; modify the image on the button
; *******************************************************************
AddGraphicButton(VariableName, ImgPath, Options="", bHeight=32, bWidth=32)
{
Global
Local ImgType, ImgType1, ImgPath0, ImgPath1, ImgPath2, hwndmode
; BS_BITMAP := 128, IMAGE_BITMAP := 0, BS_ICON := 64, IMAGE_ICON := 1
Static LR_LOADFROMFILE := 16
Static BM_SETIMAGE := 247
Static NULL
SplitPath, ImgPath,,, ImgType1
If ImgPath is float
{
  ImgType1 := (SubStr(ImgPath, 1, 1)  = "0") ? "bmp" : "ico"
  StringSplit, ImgPath, ImgPath,`.
  %VariableName%_img := ImgPath2
  hwndmode := true
}
ImgTYpe := (ImgType1 = "bmp") ? 128 : 64
If (%VariableName%_img != "") AND !(hwndmode)
  DllCall("DeleteObject", "UInt", %VariableName%_img)
If (%VariableName%_hwnd = "")
  Gui, Add, Button,  v%VariableName% hwnd%VariableName%_hwnd +%ImgTYpe% %Options%
ImgType := (ImgType1 = "bmp") ? 0 : 1
If !(hwndmode)
  %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
}
 

Download previous version (2.1) working sample script (images included)


Last edited by corrupt on July 2nd, 2008, 8:38 am, edited 22 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 16th, 2005, 8:13 am 
Offline

Joined: March 28th, 2004, 3:53 pm
Posts: 1870
cool !

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 16th, 2005, 1:08 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
That's a great script. It also teaches me how to add this feature to the program, so I've made a note do so for a future version.

Thanks.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: July 3rd, 2005, 2:06 pm 
Offline

Joined: May 3rd, 2005, 1:05 pm
Posts: 44
This is a really cool feature to use in guis. But is it also possible to use it with .png instead of .bmp?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 5th, 2005, 6:07 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2543
Sorry, the current code will not support .png AFAIK


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 6th, 2005, 4:30 pm 
THX a lot!

This is the code I searched for my programm!

2 thumbs up for corrupt!

Thalon


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: July 8th, 2005, 12:30 am 
@corrupt
can u please tell me how did u find the DllCall functions u used in here because i want to do somethings with it too ,.,, but dont have any info.. or any website address where i can find it...
Thanks


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 14th, 2005, 5:53 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2543
Sorry for the delayed response. I missed your post somehow :oops: . Here's a good starting point. Feel free to ask if you get stuck with anything specific :) .


Report this post
Top
 Profile  
Reply with quote  
 Post subject: .ico ?
PostPosted: August 19th, 2005, 4:46 pm 
Offline

Joined: August 19th, 2005, 4:36 pm
Posts: 14
Location: Germany
Hello,

I've tried to change
Code:
IMAGE_BITMAP
to
Code:
IMAGE_ICON
without success.

What does
Code:
+0x80
mean ?

I would like to make my transparent icons real buttons, but if I convert them to bmps the transparancy get lost.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 21st, 2005, 1:13 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
0x80 is BS_BITMAP, which is a style that specifies that the button displays a bitmap.

By contrast, 0x40 is BS_ICON, which specifies that the button displays an icon.

By the way, thanks to corrupt's work in this topic, I hope to add support for picture buttons soon.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2005, 9:24 pm 
How do I have to adjust the code, that it shows an icon instead of a bitmap?

Replacing:

0x80 with 0x40
IMAGE_BITMAP with IMAGE_ICON

didn't do the job. I don't know anything about DllCall, so it's very hard to understand what the code does at all.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2005, 10:54 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2543
Updated the code in the first post to support using icons. Thanks for the suggestion AGU :) .


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2005, 10:56 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2543
Chris wrote:
By the way, thanks to corrupt's work in this topic, I hope to add support for picture buttons soon.

:twisted: My master plan is working... hehe... hehe... :twisted:

:lol:

Thanks Chris :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 30th, 2005, 5:03 pm 
thanks for your work concerning icon support.

And I hail to the master plan of corrupt. :wink:

btw. reading this thread I played a little bit around at these API websites and found these.

http://www.mentalis.org/apilist/SetMenu ... maps.shtml
http://msdn2.microsoft.com/en-us/library/ms647998.aspx

Is it about those icons beside menu items? How about integrating it in your evil ;) masterplan?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 4th, 2005, 3:11 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2543
Graphic menu items as requested :)


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 1, 2, 3, 4, 5 ... 8  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 6 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