PDA

Click to See Complete Forum and Search --> : Form, Menu, Lost Focus


NP001
Sep 6th, 2000, 09:33 PM
Ok. I have only been programming in VB on and off for less than a year. In my new program I wanted a new look look and one thing I really hate are the basic plain old menus. So I decided to make new menus out of a form. When I was all done with this program the menus work and everything but with one little problem. When you clicked out of the application while you had a menu open it wouldn't unload and it gets really annoying after a while. I have seen some threads about hooking and all that but it either dosn't work or I get errors that I just can't seem to find. I just would like some insite on this so I can finaly finish this program. Maby a webpage that explains this or a project that shows how to do this would help. Thanks for your time.

Sep 6th, 2000, 10:17 PM
Sublassing? This will unload the menu when the form has lost focus.

Try this:

Option Explicit

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal ndx As Long, ByVal newValue As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

' This is used with the SetWindowLong API function.
Const GWL_WNDPROC = -4

Public Const WM_KILLFOCUS = &H8

Dim saveHWnd As Long ' The handle of the subclassed window.
Dim oldProcAddr As Long ' The address of the original window procedure

Sub StartSubclassing(ByVal hWnd As Long)
saveHWnd = hWnd
oldProcAddr = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
End Sub

Sub StopSubclassing()
SetWindowLong saveHWnd, GWL_WNDPROC, oldProcAddr
End Sub

Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
' Send the message to the original window procedure, and then
' return Windows the return value from the original procedure.
WndProc = CallWindowProc(oldProcAddr, hWnd, uMsg, wParam, lParam)

Select Case uMsg
Case WM_KILLFOCUS
Unload frmMenu
End Select
End Function

Private Sub Form_Load()
StartSubclassing Me.hWnd
End Sub

Private Sub Form_Unload(Cancel As Integer)
StopSubclassing
End Sub

V(ery) Basic
Sep 7th, 2000, 02:03 PM
I would strongly advise against using a form as a menu, there are 1001 better ways.

Ok.

Maybe only 1 better way in reality.

That is to owner-draw the menu.

What is that? I hear you ask.

Well, my dear friend, what happens is you tell Windows (politely) that you are quite fed up with their menus, and you are going to draw your own.

Windows then kindly accepts this offer, and when it needs to show the menu, it asks you two questions. One is the dimensions of the menu, and the second is then sent when it could need to (re)draw one of the menu items.

Windows kindly passes you an hDC, amongst other things, and it lets you do whatever you like with it.

But this sounds mighty complicated, you might say?

Ah, but not with the new 'V(ery) Basic menu classes'

All you have to do is add about 15 lines of code in the Form in addition to the menu-adding code (another 30 lines)

Then you just add the 3 class modules and a BAS Module, and you can enjoy the benefits of luxury OD menus with icons, with different selected colours, with pizzaz and panache, but without the coding.

Yes, my friend, all this could be yours if you reply to this once in a lifetime offer.

Otherwise .. :( ... I'll go and delete this post and apologise for wasting your time

NP001
Sep 8th, 2000, 09:56 PM
I ended up going with Matthew's help. I got my menus working without errors. Thanks for your help.