Form1 code
[code]
'===================================================
' Sticky button (toggle) example.
' You need one form with a button named "btnSticky".
' You need one module "Module1" (code as second part)
' This is the code for "Form1"
'---------------------------------------------------
Option Explicit
Private Sub btnSticky_Click()
' If you ONLY need the button to keep state and the state to be available
' in this form you can declare it here. Otherwise declare it in a global
' part of your program (as done in this example).
'Static Downstate As Long
'Whatever state the button has, it will now become the opposite.
Downstate = Not Downstate
'I change what the button reads and the color depending on state.
If Downstate Then
btnSticky.Caption = "btnSticky On"
btnSticky.BackColor = &HFF&
Else
btnSticky.Caption = "btnSticky Off"
btnSticky.BackColor = &H8000000F
End If
Call SendMessageBynum(btnSticky.hwnd, BM_SETSTATE, Downstate, 0)
End Sub
[/code]
Module1 code
[code]
'===================================================
' Sticky button (toggle) example.
' This is the code for "Module1"
'---------------------------------------------------
Option Explicit
' To create a "sticky" button, you need to send a message to the Command Button
' using the SendMessageBynum Windows API function.
' Without the declaration and the public const the button won't work.
' This must be in a global part of your program
Declare Function SendMessageBynum Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
' This must be in a global part of your program
Public Const BM_SETSTATE = &HF3
' If this isn't in a global part of your program your button will lose
' state as soon as you leave the form where it resides.
' This way you can test the buttons state everywhere in your program too.
Public Downstate As Long
[/code]