How to add popup menu to a borderless form
-
Saturday, February 18, 2012 4:14 PM
Hey Guys!
I have a form without border and i want to add popup menu to it. I got the codes below from somewhere on net which call up a popup menu when you click on the form but when i click on the menu of the popup menu nothing happens.
Can some one help me such that when i click a particular menu it execute a command. please go through the code below.
'General declaration
Const MF_CHECKED = &H8&
Const MF_APPEND = &H100&
Const TPM_LEFTALIGN = &H0&
Const MF_DISABLED = &H2&
Const MF_GRAYED = &H1&
Const MF_SEPARATOR = &H800&
Const MF_STRING = &H0&Private Type POINTAPI
x As Long
y As Long
End TypePrivate Declare Function CreatePopupMenu Lib "user32" () As Long
Private Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hwnd As Long, ByVal lprc As Any) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Dim hMenu As Long
Private Sub Command1_Click()
End
End Sub
Private Sub Form_Load()
'Create an empty popupmenu
hMenu = CreatePopupMenu()
'Append a few menu items
AppendMenu hMenu, MF_STRING, ByVal 0&, "Hello !"
AppendMenu hMenu, MF_STRING, ByVal 0&, "Black Screen"
AppendMenu hMenu, MF_GRAYED Or MF_DISABLED, ByVal 0&, "Testing ..."
AppendMenu hMenu, MF_SEPARATOR, ByVal 0&, ByVal 0&
AppendMenu hMenu, MF_CHECKED, ByVal 0&, "TrackPopupMenu"
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim Pt As POINTAPI
'Get the position of the mouse cursor
GetCursorPos Pt
If Button = 1 Then
'Show our popupmenu
TrackPopupMenu hMenu, TPM_LEFTALIGN, Pt.x, Pt.y, 0, Me.hwnd, ByVal 0&
Else
'Show our form's default popup menu
TrackPopupMenu GetSystemMenu(Me.hwnd, False), TPM_LEFTALIGN, Pt.x, Pt.y, 0, Me.hwnd, ByVal 0&
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Destroy our menu
DestroyMenu hMenu
End Sub
All Replies
-
Monday, February 20, 2012 3:30 AMModerator
Hi Programmermja,
Welcome to the MSDN Forum.
Could you please tell us which programing language, Vb.net or Vb6?
Apparently, this code is written by VB6 which is not supported.
If you are using Vb.net, then add the Context menu to a borderless form is same to add the Context menu to a regular form. To add the Context menu, see the following two links.
http://msdn.microsoft.com/en-us/library/aa984254(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.contextmenustrip.aspx
Please let us know the result.
Best Regards,Bob Wu [MSFT]
MSDN Community Support | Feedback to us
- Marked As Answer by Bob Wu-MTMicrosoft Contingent Staff, Moderator Thursday, February 23, 2012 2:48 AM
-
Tuesday, February 21, 2012 10:29 PM@ Bob Wu-MT Thanks for that. I am still new to VB.NET.


