Mick,
Where are you using BM_GETSTATE? My IsPressed (ergo BM_GETSTATE) returns
True when called from the Control.MouseMove event when the mouse is down,
and the mouse did not leave the control. If you think about it, the
"button" is never really pressed otherwise.
Try the following:
Private WithEvents CheckBox1 As CheckBoxEx
Private Sub CheckBox1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles CheckBox1.MouseDown
Debug.WriteLine(CheckBox1.IsPushed, "CheckBox1_MouseDown")
End Sub
Private Sub CheckBox1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles CheckBox1.MouseMove
Debug.WriteLine(CheckBox1.IsPushed, "CheckBox1_MouseMove")
End Sub
Private Sub CheckBox1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles CheckBox1.MouseUp
Debug.WriteLine(CheckBox1.IsPushed, "CheckBox1_MouseUp")
End Sub
Private Sub CheckBox1_MouseEnter(ByVal sender As Object, ByVal e As
System.EventArgs) Handles CheckBox1.MouseEnter
Debug.WriteLine(CheckBox1.IsPushed, "CheckBox1_MouseEnter")
End Sub
Private Sub CheckBox1_MouseLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles CheckBox1.MouseLeave
Debug.WriteLine(CheckBox1.IsPushed, "CheckBox1_MouseLeave")
End Sub
Private Sub CheckBox1_MouseHover(ByVal sender As Object, ByVal e As
System.EventArgs) Handles CheckBox1.MouseHover
Debug.WriteLine(CheckBox1.IsPushed, "CheckBox1_MouseHover")
End Sub
Hope this helps
Jay
"Mick Doherty"
Hi Jay,
I can confirm Jeffs results. BM_GETSTATE always returns 0 or 8 (focused
or not_focused) and nothing else.
I tested your class out and IsPushed always returns False, although it
should definately give the results that you are expecting.
I prefer to use WndProc to Sendmessage but the result is the same either
way.
\\\
Dim m As Message
m = Message.Create(Handle, BM_GETSTATE, IntPtr.Zero, IntPtr.Zero)
WndProc(m)
Dim state As ButtonState = CType(m.Result.ToInt32(), ButtonState)
Return ((state And ButtonState.Pushed) = ButtonState.Pushed)
///
My System specs:
WindowsXP Pro SP2
VS2003
--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
Jay B. Harlow said:
Jeff,
Lets back up to the beginning:
How are you defining, and when are you using SendMessage with
BM_GETSTATE & BST_PUSHED?
The following is a sample of a CheckBox that has an IsPushed property.
The IsPushed property returns true then the mouse is down & the control
has focus, in other words when the control is Pressed.
It uses BM_GETSTATE & SendMessage to check for the BST_PUSHED state. The
same code works for CheckBox, RadioButton, & Button class in .NET.
Remember under .NET an Integer is a 32bit value, while a Long is a 64bit
value. Defining the function with IntPtr as I did, ensures it will
continue working as expected in the 64-bit version of Windows. Also
remember that BM_GETSTATE returns a set of bit flags, you need to
isolate the specific bits you are interested in.
Public Class CheckBoxEx
Inherits CheckBox
Private Enum ButtonMessage As Integer
GetCheck = &HF0
SetCheck = &HF1
GetState = &HF2
SetState = &HF3
SetStyle = &HF4
Click = &HF5
GetImage = &HF6
SetImage = &HF7
End Enum
<Flags()> _
Private Enum ButtonState As Integer
UnChecked = &H0
Checked = &H1
Indeterminate = &H2
StateMask = &H3
Pushed = &H4
Focus = &H8
End Enum
Private Declare Auto Function SendMessage Lib "user32" (ByVal hWnd As
IntPtr, ByVal msg As ButtonMessage, ByVal wParam As IntPtr, ByVal lParam
As IntPtr) As IntPtr
Public ReadOnly Property IsPushed() As Boolean
Get
Dim rc As IntPtr
rc = SendMessage(Me.Handle, ButtonMessage.GetState,
IntPtr.Zero, IntPtr.Zero)
Dim state As ButtonState = CType(rc.ToInt32(), ButtonState)
Return ((state And ButtonState.Pushed) = ButtonState.Pushed)
End Get
End Property
End Class
Hope this helps
Jay
Jay,
Thanks for the suggestion. I tried using both the CheckBox and
RadioButton. Neither of them work because their Checked property is not
set until the user releases the left mouse button. If you create a
button, click it with the left mouse button and hold the mouse button
down you will see that the button stays "pushed down". The CheckChanged
event is not fired, OnClick event is not fired, and Checked property is
False until you release the mouse button. I am looking to be able to
tell when the button is actually depressed (pushed down)
I am doing my own painting on my button and want it to "push down" with
the button by offsetting it when the button is depressed. Currently the
button gets pushed down but what I painted on it does not.
Jeff
Jay B. Harlow [MVP - Outlook] wrote:
Jeff,
Have you considered using a CheckBox or RadioButton control with the
Appearance property set to Button?
This causes the CheckBox or RadioButton to look like a normal Button,
but
continue to function (toggle) as a CheckBox or RadioButton.
You can set the AutoCheck property of the CheckBox or RadioButton if
you
don't want them to stay down.
Hope this helps
Jay
Hello,
I am trying to find a way to tell if an .NET windows forms Button
(System.Windows.Forms.Button) is "depressed" (pushed down). For my
application, I can not use a check box control set to button style,
I
must use a System.Windows.Forms.Button. I can not find a way to
tell
when it is momentaraly pressed.
I tried calling the API SendMessage with the button handle and
BM_GETSTATE to get the state of the button. This will only return
when
the button has focus (BST_FOCUS). It will never return a result
with
BST_PUSHED even when the button is in fact pressed down.
Any ideas?
Jeff