customizing a system menu: the RemoveMenu API call is broken

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

Apparently customizing the system menu in a Form requires API calls... that's
fine, but RemoveMenu doesn't work. Will I have to pop up my own menu to get rid
of certain selections?

Bob
 
Bob said:
Apparently customizing the system menu in a Form requires API calls... that's
fine, but RemoveMenu doesn't work. Will I have to pop up my own menu to get rid
of certain selections?

Please show us some code...
 
Herfried,
I give you an answer tomorrow, it is saterday night, and I look at the
answers, but dont answer them, you are from my Country, you know why.
Cor
 
Bob,
Not mine, but here is an example.

http://tinyurl.com/ovfc

The RemoveMenu declaration at that page is incorrect (not
surprisingly, since it's hosted at Planet Crap Code), it should be

Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As
IntPtr, ByVal nPosition As Integer, ByVal wFlags As Integer) As
Boolean



Mattias
 
Hi Herfried,The answer on this is, that I was real laughing when I saw the answer from
the sender.
My thoughts
- he answers how Herfried does it when he is in his quick run style (10%
here and I have never seen it in the German groups).
- he gives a Link and want to do Herfried the rest.

When you do it, it is for the one who gets the link, always information on
his route to his solution.
So don't think I want to say: "don't do it" or something like that, no in
opposite stay doing it I collect them.

But I was real laughing for a long time.
That is all.
:-)
Cor
 
Konnten Sie das in Ihrer Muttersprache bekanntgeben, also ich kann Sie besser
verstehen?

Bob
 
Hmmm guess I have to stop using the API guide as a resource. They list the wrong
declaration as well.

I still can't get this to work. Here is what I have for code, maybe you can tell
me what I'm doing wrong?

Thanks,
Bob

Public Class Form1
Inherits System.Windows.Forms.Form

Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As _
IntPtr, ByVal nPosition As Integer, ByVal wFlags As Integer) As _
Boolean

''''old, bad declareation
'''Private Declare Function RemoveMenu Lib "user32" _
''' (ByVal hMenu As Long, ByVal nPosition As Long, _
''' ByVal wFlags As Long) As Long

'Private Declare Function GetSystemMenu Lib "user32" _
' (ByVal hwnd As Integer, ByVal bRevert As Boolean) As Integer

'Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" _
' (ByVal hMenu As Integer, ByVal wFlags As Integer, _
' ByVal wIDNewItem As Integer, ByVal lpNewItem As String) As Integer

'Private Const MF_BYPOSITION As Long = &H400
Private Const MF_REMOVE As Long = &H1000
'Private Const WM_SYSCOMMAND As Long = &H112

Private Sub Form1_Load _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim hSysMenu As Integer
'hSysMenu = GetSystemMenu(Me.Handle.ToInt32, False)

RemoveMenu(Me.Handle, 6, MF_REMOVE)

'AppendMenu(hSysMenu, 0, 1000, "New Item1")
'AppendMenu(hSysMenu, 0, 1001, "New Item2")

End Sub

'Protected Overrides Sub WndProc(ByRef m As Message)
' MyBase.WndProc(m)
' If m.Msg = WM_SYSCOMMAND Then
' Select Case m.WParam.ToInt32
' Case 1000
' MsgBox("Hello1")
' Case 1001
' MsgBox("Hello2")
' End Select
' End If
'End Sub

End Class
 
Bob,
Hmmm guess I have to stop using the API guide as a resource. They list the wrong
declaration as well.

Which API guide? The declarations it contains are probably for VB6.

I still can't get this to work. Here is what I have for code, maybe you can tell
me what I'm doing wrong?

First of all I suggest you turn on Option Strict.

RemoveMenu(Me.Handle, 6, MF_REMOVE)

Make that

RemoveMenu(GetSystemMenu(Me.Handle, False), 6, MF_REMOVE)

GetSystemMenu should be declared to take an IntPtr as its first
parameter and also return an IntPtr.



Mattias
 
Hmmm guess I have to stop using the API guide as a resource. They list the
wrong
declaration as well.
It's not the wrong declaration, its the VBClassic declaration.
In DotNet those Longs become Integers and Integers become Shorts, with the
exception that Handles and DC's become IntPtrs.
Use MSDN to get the info for your API Calls.
RemoveMenu(Me.Handle, 6, MF_REMOVE)
RemoveMenu's hMenu variable is looking for the Menu Handle not the Form
Handle.
So try:
RemoveMenu(hSysMenu, 6, MF_REMOVE)

Bob said:
Hmmm guess I have to stop using the API guide as a resource. They list the wrong
declaration as well.

I still can't get this to work. Here is what I have for code, maybe you can tell
me what I'm doing wrong?

Thanks,
Bob

Public Class Form1
Inherits System.Windows.Forms.Form

Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As _
IntPtr, ByVal nPosition As Integer, ByVal wFlags As Integer) As _
Boolean

''''old, bad declareation
'''Private Declare Function RemoveMenu Lib "user32" _
''' (ByVal hMenu As Long, ByVal nPosition As Long, _
''' ByVal wFlags As Long) As Long

'Private Declare Function GetSystemMenu Lib "user32" _
' (ByVal hwnd As Integer, ByVal bRevert As Boolean) As Integer

'Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" _
' (ByVal hMenu As Integer, ByVal wFlags As Integer, _
' ByVal wIDNewItem As Integer, ByVal lpNewItem As String) As Integer

'Private Const MF_BYPOSITION As Long = &H400
Private Const MF_REMOVE As Long = &H1000
'Private Const WM_SYSCOMMAND As Long = &H112

Private Sub Form1_Load _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim hSysMenu As Integer
'hSysMenu = GetSystemMenu(Me.Handle.ToInt32, False)

RemoveMenu(Me.Handle, 6, MF_REMOVE)

'AppendMenu(hSysMenu, 0, 1000, "New Item1")
'AppendMenu(hSysMenu, 0, 1001, "New Item2")

End Sub

'Protected Overrides Sub WndProc(ByRef m As Message)
' MyBase.WndProc(m)
' If m.Msg = WM_SYSCOMMAND Then
' Select Case m.WParam.ToInt32
' Case 1000
' MsgBox("Hello1")
' Case 1001
' MsgBox("Hello2")
' End Select
' End If
'End Sub

End Class
 
The RemoveMenu declaration at that page is incorrect (not
surprisingly, since it's hosted at Planet Crap Code),

It's not the Planet thats crap, there is a lot of useful information buried
in there. That example was just a poor translation of a VBClassic example.
 
Which API guide?

Why, *the* API Guide. :-)
http://www.mentalis.org/
The declarations it contains are probably for VB6.

True but one must find information on API's somewhere, and I haven't found a
source for that specifically for .Net yet.
First of all I suggest you turn on Option Strict.

The code I posted works under Option Strict. It's always on in my IDE as
default.
Make that

RemoveMenu(GetSystemMenu(Me.Handle, False), 6, MF_REMOVE)

GetSystemMenu should be declared to take an IntPtr as its first
parameter and also return an IntPtr.

I know it's likely I should have been able to get this to work with the help
you've already given me... sorry if I'm being thick.

The following code still fails to remove any system menu items. I've tried
changing out the Integers for Longs and Shorts, too.

Bob

Public Class Form1
Inherits System.Windows.Forms.Form

Private Declare Function RemoveMenu Lib "user32" ( _
ByVal hMenu As IntPtr, _
ByVal nPosition As Integer, _
ByVal wFlags As Integer) _
As Boolean

Private Declare Function GetSystemMenu Lib "user32" ( _
ByVal hwnd As IntPtr, _
ByVal bRevert As Boolean _
) As IntPtr

Private Const MF_REMOVE As Integer = &H1000

Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Load

RemoveMenu(GetSystemMenu(Me.Handle, False), 6, MF_REMOVE)

End Sub

End Class
 
Sorry, should have said Change MF_REMOVE for MF_BYPOSITION

Mick Doherty said:
Hmmm guess I have to stop using the API guide as a resource. They list
the
wrong
declaration as well.
It's not the wrong declaration, its the VBClassic declaration.
In DotNet those Longs become Integers and Integers become Shorts, with the
exception that Handles and DC's become IntPtrs.
Use MSDN to get the info for your API Calls.
RemoveMenu(Me.Handle, 6, MF_REMOVE)
RemoveMenu's hMenu variable is looking for the Menu Handle not the Form
Handle.
So try:
RemoveMenu(hSysMenu, 6, MF_REMOVE)

Bob said:
Hmmm guess I have to stop using the API guide as a resource. They list
the
wrong
declaration as well.

I still can't get this to work. Here is what I have for code, maybe you can tell
me what I'm doing wrong?

Thanks,
Bob

Public Class Form1
Inherits System.Windows.Forms.Form

Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As _
IntPtr, ByVal nPosition As Integer, ByVal wFlags As Integer) As _
Boolean

''''old, bad declareation
'''Private Declare Function RemoveMenu Lib "user32" _
''' (ByVal hMenu As Long, ByVal nPosition As Long, _
''' ByVal wFlags As Long) As Long

'Private Declare Function GetSystemMenu Lib "user32" _
' (ByVal hwnd As Integer, ByVal bRevert As Boolean) As Integer

'Private Declare Function AppendMenu Lib "user32" Alias
"AppendMenuA"
 
Bob said:

Are you sure the declares are valid in the API guide? VB6 declares
cannot be used directly with VB.NET, you will have to make
modifications. The best way is to have a look at the documentation of
the platform function in the MSDN library and translate it by hand.
 
Mick Doherty said:
It's not the Planet thats crap, there is a lot of useful information buried
in there. That example was just a poor translation of a VBClassic example.

Samples on Planet Source Crap will give you a basic idea on how to solve the
problem, but often they are really "buggy".
 
Back
Top