Here's a little piece of code that enables the user to create a CommandLink style Button for Windows operating systems (Vista and above). If the OS doesn't support the control, it will fall back and create a ButtonGadget with tooltip.
Preview

- Support Ansi/Unicode build. (Converts string automatically)
- Fallback for non-compliant operating systems/OS versions.
- Handles events like a normal ButtonGadget.
- Supports #PB_Any parameter.
- Apply custom icons to the buttons.
- Enable/disable shield icon.
- Set the appearance to look like a default button.
Code: Select all
;------------------------------------------------------------------------------------------
; Title: CommandLinkButton Implementation
; Description: Provides access to the Command Link Button on Windows Systems (Vista+)
; Author(s): Michael R. King (mrking2910@gmail.com)
; Revision: 6 April 2012
; Notes: Will fall back to a ButtonGadget with ToolTip if control not available.
;------------------------------------------------------------------------------------------
; - Required Constants -
CompilerIf Defined(BCM_FIRST, #PB_Constant) = #False
#BCM_FIRST = $1600
CompilerEndIf
CompilerIf Defined(BCM_SETNOTE, #PB_Constant) = #False
#BCM_SETNOTE = (#BCM_FIRST + $0009)
CompilerEndIf
CompilerIf Defined(BCM_SETSHIELD , #PB_Constant) = #False
#BCM_SETSHIELD = (#BCM_FIRST + $000C)
CompilerEndIf
CompilerIf Defined(BS_COMMANDLINK, #PB_Constant) = #False
#BS_COMMANDLINK = $0000000E
#BS_DEFCOMMANDLINK = $000000F
CompilerEndIf
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
If OSVersion() > #PB_OS_Windows_XP
#PB_CommandLink_Available = #True
EndIf
CompilerEndIf
CompilerIf Defined(PB_CommandLink_Available, #PB_Constant) = #False
#PB_CommandLink_Available = #False
CompilerEndIf
; - Creation Procedure -
Procedure CommandLinkButton(Gadget, X, Y, Width, Height, Text$, Note$ = "", MakeDefault = #False)
Protected sNote.s
Protected retVal
CompilerIf #PB_CommandLink_Available = #True
CompilerIf #PB_Compiler_Unicode = #False
sNote = Space(255)
MultiByteToWideChar_ ( #CP_ACP, 0, @Note$, Len(Note$), @sNote, Len(sNote))
PokeL( @sNote + Len(Note$) * 2, 0)
CompilerElse
sNote = Note$
CompilerEndIf
If MakeDefault
retVal = ButtonGadget(Gadget, X, Y, Width, Height, Text$, #BS_DEFCOMMANDLINK)
Else
retVal = ButtonGadget(Gadget, X, Y, Width, Height, Text$, #BS_COMMANDLINK)
EndIf
If Gadget = #PB_Any
SendMessage_(GadgetID(retVal), #BCM_SETNOTE, 0, @sNote)
Else
SendMessage_(GadgetID(Gadget), #BCM_SETNOTE, 0, @sNote)
EndIf
ProcedureReturn retVal
CompilerElse
retVal = ButtonGadget(Gadget, X, Y, Width, Height, Text$)
If Gadget = #PB_Any
GadgetToolTip(retVal, Note$)
Else
GadgetToolTip(Gadget, Note$)
EndIf
ProcedureReturn retVal
CompilerEndIf
EndProcedure
; - Personalisation Procedures -
Procedure SetCommandLinkIcon(Gadget, *ImageID)
CompilerIf #PB_CommandLink_Available
If IsGadget(Gadget)
SendMessage_(GadgetID(Gadget), #BM_SETIMAGE, #Null, *ImageID)
EndIf
CompilerEndIf
EndProcedure
Procedure SetCommandLinkShield(Gadget, Enable = #True)
CompilerIf #PB_CommandLink_Available
If IsGadget(Gadget)
SendMessage_(GadgetID(Gadget), #BCM_SETSHIELD, #Null, Enable)
EndIf
CompilerEndIf
EndProcedure
; ---- EXAMPLE ----
; - Initialise Decoders -
UsePNGImageDecoder()
; - Open Window
OpenWindow(0, 0, 0, 790, 150, "Command Link Button Test", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
; - Load Some Icons -
Define icon = LoadImage(#PB_Any, "J:\C++ Development\Resources\Icons\crystal_project\16x16\apps\amor.png")
; - Create Gadgets -
Define btnGadget = CommandLinkButton(#PB_Any, 10, 10, 380, 60, "Command Link Button", "Just an Example")
Define btnGadget2 = CommandLinkButton(#PB_Any, 10, 80, 380, 60, "Command Link Button", "With custom icon!")
Define btnGadget3 = CommandLinkButton(#PB_Any, 400, 10, 380, 60, "Command Link Button", "With 'MakeDefault' set to #True!", #True)
Define btnGadget4 = CommandLinkButton(#PB_Any, 400, 80, 380, 60, "Command Link Button", "With a Shield!")
; - Personalise Gadgets -
SetCommandLinkIcon(btnGadget2, ImageID(icon))
SetCommandLinkShield(btnGadget4, #True)
Repeat
Select WindowEvent()
Case #PB_Event_CloseWindow
End
Case #PB_Event_Gadget
Select EventGadget()
Case btnGadget
Debug "Pressed!"
EndSelect
EndSelect
ForEver
Todo
- Implement procedure to change text margins.
- Detect version of CommCtrl32.dll to ensure it can support this control style. (Any suggestions would be grand!)