Question

How to add a popup menu on a VB 6.0 borderless form?

Every time I add a menu, the border reappears, even when BorderStyle is set to vbBSNone and the menu is hidden.

Was it helpful?

Solution

It's doable, but somewhat unsatisfying (to me). By having any menu properties in a form, the border will default back to visible. There are, however, a few workarounds:

1) The method I think you'll prefer involves making a second form that you'll never really "use" or see. Put the menu on that second form, and then call that menu from the form you actually want to use. Assuming you're using Form_MouseDown to call this, here's the code:

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button And vbRightButton Then PopupMenu Form2.mnuYourMenu
End sub

You will have to remember to unload this second form from memory, however.

2) Another way, only using the first form, would be to set the form's ControlBox to False and to leave the Caption property blank. This "removes" the border when BorderStyle is set to 0... I put removes in quotes because it will unfortunately leave behind a 1-pixel black line. It doesn't look bad, but it might not be a viable solution for you.

3) The final way, which I read about but haven't done anything with myself, would be to use the CreatePopupMenu API, found at http://allapi.mentalis.org/apilist/CreatePopupMenu.shtml

Hope this helps!

OTHER TIPS

This is possible. Set the form's BorderStyle to None, Caption to an empty string, ControlBox, MaxButton MinButton to False. Then, using VB6's menu editor, create a top-level menu named "mnuPopup," and set its Visible property to False. Create the rest of the menu as submenus to that top-level menu, setting their Visible properties to True. Then, in the code for the form, you can display the menu with PopupMenu menuPopup. It looks like this:

enter image description here

For the benefit of anyone else who comes here looking for an answer to this problem, here is a very simple API method that works:

Declarations:

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Const GWL_STYLE = -16, WS_BORDER = &H800000

In Form_Load:

SetWindowLong Me.hwnd, GWL_STYLE, GetWindowLong(Me.hwnd, GWL_STYLE) And Not WS_BORDER
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top