Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing menu items using API 1

Status
Not open for further replies.

dazzer123

IS-IT--Management
Nov 24, 2003
128
GB
Hi I've got a vb form that pulls a microsoft mappoint window into a subform using API, i've also then used some further API to remove the caption bar of the MapPoint window, however I cant seem to work out how to remove the menu items from the Mappoint Window.

Does anyone have any ideas?

The code is posted below


General Declerations-
Public gappmp As New MapPoint.Application

'Handle to MapPoint Window
Public ghwndMP As Long

'Used to get window style bits.
Public Const GWL_EXSTYLE = -20
Public Const GWL_STYLE = -16

'Force total redraw that shows new styles.
Public Const SWP_FRAMECHANGED = &H20
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOZORDER = &H4
Public Const SWP_NOSIZE = &H1
'********************************************************
'* Window's API Function Prototypes
'********************************************************


' Enter each of the following Declare statements as one, single line:

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Declare Function SetParent Lib "user32" _
(ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hWnd As Long, ByVal nIndex As Long) As Long

Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Public Declare Function SetWindowPos Lib "user32" _
(ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Public Declare Function GetWindowThreadProcessId Lib "user32.dll" _
(ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function apiGetFocus Lib "user32" _
Alias "GetFocus" _
() As Long


Public Sub FlipBit(hWnd As Long, ByVal lngStyleBit As Long, ByVal bValue As Boolean)
'Windows Style Manipulation Function
Dim lngStyle As Long

'Retrieve current style bits
lngStyle = GetWindowLong(hWnd, GWL_STYLE)
'Set requested bit On or Off
If bValue Then
lngStyle = lngStyle Or lngStyleBit
Else
lngStyle = lngStyle And Not lngStyleBit
End If
SetWindowLong hWnd, GWL_STYLE, lngStyle
'Redraw the window
Redraw hWnd
End Sub

Public Sub Redraw(hWnd As Long)
'Redraw window with new style
Const swpFlags As Long = SWP_FRAMECHANGED Or SWP_NOMOVE Or SWP_NOZORDER Or SWP_NOSIZE
SetWindowPos hWnd, 0, 0, 0, 0, 0, swpFlags
End Sub

Code on SubForm -

Private Const WS_CAPTION = &HC00000
Private Sub Form_Open(Cancel As Integer)

Dim objMap As MapPoint.Map
Dim oToolbar As MapPoint.Toolbar

Set gappmp = CreateObject("MapPoint.Application")

gappmp.NewMap "c:\TestMap.ptt"
gappmp.Visible = False
gappmp.UserControl = True
gappmp.PaneState = geoPaneNone

For Each oToolbar In gappmp.Toolbars
If oToolbar.Name = "Navigation" Then
oToolbar.Visible = True
MsgBox "True"
Else
oToolbar.Visible = False
MsgBox "False"
End If
Next

'Get the handle of the MapPoint Window
ghwndMP = FindWindow(vbNullString, "Map - Microsoft MapPoint Europe")

'Remove MapPoint Title Bar

FlipBit ghwndMP, WS_CAPTION, False

'Pull in Window
SetParent ghwndMP, Me.hWnd
 
Hi Dazzer,

I don't do VB, but hope this helps you

hMenu = GetMenu( hWnd )
MenuCounter = GetMenuItemCount( hMenu )
For i = 0 to MenuCounter-1
RemoveMenu( hMenu, 0, MF_BYPOSITION )
Next

-- AirCon --
 
This didn't work but I see the idea. I can get the MenuCount Ok but its not removing the menus
 
I'm not sure why it doesn't work. Try use DeleteMenu

-- AirCon --
 
By the way, what is the return value from RemoveMenu? is it 0? try call GetLastError and see why it failed

-- AirCon --
 
Do you mean you don't get any error value from GetLastError ?

I'm sorry, I don't have Mappoint so I can't test it myself.


-- AirCon --
 
If I do

hMenu = GetMenu(hWnd)
menuCounter = GetMenuItemCount(hMenu)

For i = 0 To menuCounter - 1
Call RemoveMenu(hMenu, 0, MF_BYPOSITION)
Next

Then
MenuCounter = -1
Therefore no return value for remove menu as it nver happens

If I do

hMenu = GetSystemMenu(hWnd, 0)
menuCounter = GetMenuItemCount(hMenu)

For i = 0 To menuCounter - 1
Call RemoveMenu(hMenu, 0, MF_BYPOSITION)
Next

Then
MenuCounter = 7
RemoveMenu return value = 1

MenuCounter = 7 is what I would expect, whats the difference between GetMenu and GetSystemMenu

I haven't done GetLastError yet I'll give it a go
 
There is no error code with the GetSystemMenu one

With GetMenu I get Error code 1401 (if I remove the for loop, obviously if I don't remove the for loop and menucounter = -1 (which it does, read previous post) then its not going to work anyway)
 
Just to recap on the code I now have

General Declerations-
Public gappmp As New MapPoint.Application

'Handle to MapPoint Window
Public ghwndMP As Long

'Used to get window style bits.
Public Const GWL_EXSTYLE = -20
Public Const GWL_STYLE = -16

'Force total redraw that shows new styles.
Public Const SWP_FRAMECHANGED = &H20
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOZORDER = &H4
Public Const SWP_NOSIZE = &H1
'********************************************************
'* Window's API Function Prototypes
'********************************************************


' Enter each of the following Declare statements as one, single line:

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Declare Function SetParent Lib "user32" _
(ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hWnd As Long, ByVal nIndex As Long) As Long

Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Public Declare Function SetWindowPos Lib "user32" _
(ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Public Declare Function GetWindowThreadProcessId Lib "user32.dll" _
(ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function apiGetFocus Lib "user32" _
Alias "GetFocus" _
() As Long


Public Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu As Long) As Long

Public Declare Function GetSystemMenu Lib "user32" _
(ByVal hWnd As Long, _
ByVal bRevert As Long) As Long

Public Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long) As Long

Public Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As Long

Public Const MF_BYPOSITION = &H400


Public Sub FlipBit(hWnd As Long, ByVal lngStyleBit As Long, ByVal bValue As Boolean)
'Windows Style Manipulation Function
Dim lngStyle As Long

'Retrieve current style bits
lngStyle = GetWindowLong(hWnd, GWL_STYLE)
'Set requested bit On or Off
If bValue Then
lngStyle = lngStyle Or lngStyleBit
Else
lngStyle = lngStyle And Not lngStyleBit
End If
SetWindowLong hWnd, GWL_STYLE, lngStyle
'Redraw the window

hMenu = GetMenu(hWnd) 'Or GetSystemMenu(hWnd,0) not sure what to use
menuCounter = GetMenuItemCount(hMenu)

For i = 0 To menuCounter - 1
Call RemoveMenu(hMenu, 0, MF_BYPOSITION)
Next

errorCode = GetLastError()
MsgBox errorCode & "!"

Redraw hWnd
End Sub

Public Sub Redraw(hWnd As Long)
'Redraw window with new style
Const swpFlags As Long = SWP_FRAMECHANGED Or SWP_NOMOVE Or SWP_NOZORDER Or SWP_NOSIZE
SetWindowPos hWnd, 0, 0, 0, 0, 0, swpFlags
End Sub

Code on SubForm -

Private Const WS_CAPTION = &HC00000
Private Sub Form_Open(Cancel As Integer)

Dim objMap As MapPoint.Map
Dim oToolbar As MapPoint.Toolbar

Set gappmp = CreateObject("MapPoint.Application")

gappmp.NewMap "c:\TestMap.ptt"
gappmp.Visible = False
gappmp.UserControl = True
gappmp.PaneState = geoPaneNone

For Each oToolbar In gappmp.Toolbars
If oToolbar.Name = "Navigation" Then
oToolbar.Visible = True
MsgBox "True"
Else
oToolbar.Visible = False
MsgBox "False"
End If
Next

'Get the handle of the MapPoint Window
ghwndMP = FindWindow(vbNullString, "Map - Microsoft MapPoint Europe")

'Remove MapPoint Title Bar

FlipBit ghwndMP, WS_CAPTION, False

'Pull in Window
SetParent ghwndMP, Me.hWnd
 
GetMenu is use to get the menu handle for the particular window. GetSystemMenu is use to get the predefined control system menu handle, and it always return 7

You should use GetMenu instead. Because this handle is the real one.

Error message 1401 is "Invalid Menu Handle". So GetMenu can't get the handle. Try to check from WinSpy utility. Is the menu inside that hWnd, or it might has a child hWnd. And the menu belongs to that child window.


-- AirCon --
 
Thanks for all your help.

Using Extended Task Manager I have discovered that this menu does indeed belong to a child window.

How can I now reference this child window
Here are the details from Extended Task Manager

Handle - 0x000E058E
Thread ID - 172
ClassName - ToolBarWindow32
 
I've come up with a quick work around

hWndChild = FindWindowEx(hwnd, 0, "ToolBarWindow32", vbNullString)

Call EnableWindow(hWndChild, 0)

to disable the menus

It doesn't do exactly what I want, i've tried DestroyWindow but I get an Access Denied (5) error and the remove menus thing fails to work.

I'm going to use this EnableWindow idea until I can figure out how to remove it completly
 
Glad you found it :)

Ok. RemoveMenu cannot be used to remove the toolbar menu. DestroyWindow can not be use to destroy a window that was created by another thread. Send/Post WM_CLOSE message instead.

SendMessage( hWndChild, WM_CLOSE, 0, 0 )

One more thing, are you sure that ToolbarWindow32 is a direct child from hWndMP ? Because usually ToolbarWindow32 is another child from ReBarWindow32. So just to make sure, call GetParent( hWndChild ) and compare the return handle with hWndMP.

Good luck

-- AirCon --
 
DONE IT!!!, thanks for all your help.

SendMessage( hWndChild, WM_CLOSE, 0, 0 )

Worked, the menu has now dissapered!

Once again thanks for all your help, I might actually meet this deadline after all!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top