Mike Williams
12/20/2011 6:00:00 PM
"Phil Hunt" <aaa@aaa.com> wrote in message
news:jcqeor$58v$1@speranza.aioe.org...
> Never say never.
Agreed. Basically you can do whatever you want as long as you believe that
your users will find it acceptable for that specific app. One way to prevent
the user from accidentally normalizing your Form is to subclass its Mouse
events and "eat" its NC Double Click event (a double click in the non client
area of the Form). The following code does exactly that, using the more or
less standard subclassing technique. With this method you need to make sure
that you obey the normal subclassing "rules" whilst developing your app (the
safest way is to simply comment out the Subclass code in the Load event
while you are developing the app in the VB IDE). There are other slightly
more complex "safer subclassing" ways of doing it, but this simple method
should be fine for your purposes. First set the WindowState to Maximized and
the MaxButton to False in the IDE properties and then paste the following
code into the Form's Load and Unload events:
Private Sub Form_Load()
SetMouseHook Me.hwnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
ReleaseMouseHook
End Sub
Then create a new standard .bas module and paste the following code into the
module:
Option Explicit
Private Declare Function SetWindowsHookEx _
Lib "user32" Alias "SetWindowsHookExA" _
(ByVal idHook As Long, ByVal lpfn As Long, _
ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx _
Lib "user32" (ByVal hHook As Long) As Long
Private Const WH_MOUSE As Long = 7
Private Declare Function CallNextHookEx _
Lib "user32" (ByVal hHook As Long, _
ByVal nCode As Long, ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (Destination As Any, _
Source As Any, ByVal Length As Long)
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type MOUSEHOOKSTRUCT
pt As POINTAPI
hwnd As Long
wHitTestCode As Long
dwExtraInfo As Long
End Type
Private mousedata As MOUSEHOOKSTRUCT
Private Hook As Long
Private HookedControl As Long
Private Const HC_ACTION = 0
Private Const WM_NCLBUTTONDBLCLK = &HA3
Public Sub SetMouseHook(Window As Long)
HookedControl = Window
If Hook = 0 Then
Hook = SetWindowsHookEx(WH_MOUSE, _
AddressOf MouseProc, App.hInstance, App.ThreadID)
End If
End Sub
Public Sub ReleaseMouseHook()
UnhookWindowsHookEx Hook
Hook = 0
End Sub
Private Function MouseProc(ByVal nCode As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
If (nCode = HC_ACTION) And (wParam = WM_NCLBUTTONDBLCLK) Then
CopyMemory mousedata, ByVal lParam, Len(mousedata)
If mousedata.hwnd = HookedControl Then
MouseProc = -1 ' "eat" the message
Else
MouseProc = CallNextHookEx _
(0, nCode, wParam, ByVal lParam)
End If
Else
MouseProc = CallNextHookEx _
(0, nCode, wParam, ByVal lParam)
End If
End Function