From: Joshua N. <jo...@jo...> - 2010-01-21 23:56:52
|
When I first set out to add a BS_COMMANDLINK style button to my GUI I didn't know how hard it was going to be and how many pitfalls I would encounter. As a result of about two days of poking around I finally figured out how to get it all working. If you're new to all of this it wasn't obvious how to do it. I hope this helps anyone getting started with Win32::GUI to do some more advanced things. If this is the wrong place for this, please let me know. My System and Perl setup: ActiveState Perl 5.10.0.1004 (32-bit installed from MSI installer using all defaults) Win32::GUI 1.06 Windows 7 (64-bit but that shouldn't matter) ################################## 1. The perl.exe has to be "patched" to add a resource to it. This resource is the manifest file that tells perl to use the newer Common Controls available in Windows Vista and Windows 7 1a. NOTE: This is a very important step as the BS_COMMANDLINK style will not work and you will see a blank where the button should be and when you click you will see a checkbox appear where the far left side of the button should've been. 1b. Create a text file with the following contents: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity version="0.0.0.0" processorArchitecture="X86" name="Perl" type="win32" /> <description>Perl</description> <dependency><dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="X86" publicKeyToken="6595b64144ccf1df" language="*" /> </dependentAssembly></dependency> </assembly> 1c. Download Resource Hacker from here: http://angusj.com/resourcehacker/ 1d. Open ResHacker.exe 1e. File -> Open 1f. Select the perl binary from perlinstalldir\bin\perl.exe 1g. Action -> Add a New Resource 1h. Click "Open file with new resource" 1i. Select the file you created in Step 1b 1j. In Resource Type enter: 24 1k. In Resource Name enter: 1 1l. In Resrouce Language enter: 1033 1m. Click "Add Resource" 1n. File -> Save 1o. Exit Resource Hacker ################################## 2. Now for a code sample to display the button: use strict; use warnings; use Win32::GUI qw( BS_PUSHBUTTON BS_LEFT ); my $hex_BCM_SETNOTE = "0x00001609"; # You could use a constant here instead my $BCM_SETNOTE = cnvtHex2Int($hex_BCM_SETNOTE); my $hex_BS_COMMANDLINK = "0x0000000E"; # You could use a constant here instead my $BS_COMMANDLINK = cnvtHex2Int($hex_BS_COMMANDLINK); my $winMain = Win32::GUI::Window->new( -name => 'winMain', -text => "Main Window", -height => 300, -width => 300, ); my $btn1 = $winMain->AddButton( -name => 'btn1', -text => 'This is the larger text on the top', -align => 'left', -left => 30, -top => 30, -height => 100, -width => 200, -tabstop => 1, # This popstyle may not be necessary -popstyle => BS_PUSHBUTTON | BS_LEFT, # This is critical to getting the command link style button -pushstyle => $BS_COMMANDLINK, ); my $btn1_Note_ASCIItxt = "This is the smaller text below"; # This converts the ASCII txt string to a null terminated WCHAR string # The BCM_SETNOTE message requires that it be in this format my $btn1_Note_utf16null = pack "S*", unpack( "C*", $btn1_Note_ASCIItxt ), 0; # This sets the text that is below the larger bolded text in a command link # Command Links on MSDN # http://msdn.microsoft.com/en-us/library/bb775947(VS.85).aspx # Or just search MSDN for BS_COMMANDLINK # If you are curious about how SendMessage works, search the Win32 GUI site # BCM_SETNOTE is the message type that allows for setting the lower text my $ret = Win32::GUI::SendMessage($btn1, $BCM_SETNOTE, 0, $btn1_Note_utf16null); $winMain->Show(); Win32::GUI::Dialog(); # Convert a Hex number to an Integer # You get Hex values from msdn.microsoft.com for # various styles, messages, etc. sub cnvtHex2Int { my ($hex) = @_; my $oct = oct($hex) if $hex =~ /^0/; my $int = int $oct; return $int; } That's it. Now you have a window with a Command Link. This method can be used for the other button styles that are not included with Win32::GUI as well. Good luck! You can ask questions here: per...@jo... |