Have a popup menu on a borderless form - vb6

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.

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!

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

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:

Related

'Close' option is not available when program minimized to the taskbar?

I am programming VB6 in Win7. I have a program with a borderless window, no caption, no icon, no control box, etc. just a window. Using a command button, I can minimize the window to the Task Bar, and from there return it back.
My problem is, when minimized to the Task Bar, I right-click on the icon, and I wish to close the program from there. Win7 won't let me close the program via the pop-up menu. The close option is on the menu, but it does nothing.
How can I close this program from the task bar menu?
This seems to be a bug in VB6 Forms subsystem -- when form's BorderStyle is set to none Close menu on the taskbar and Alt+F4 shortcut as well just stop working as there is no system menu on the form.
Unfortunately a workaround involves subclassing and here is one way to deal with the issue:
Option Explicit
Private Const WM_SYSCOMMAND As Long = &H112
Private Const SC_CLOSE As Long = &HF060&
Private m_pSubclass As IUnknown
Private Property Get pvAddressOfSubclassProc() As Form1 '-- change Form1 to current form name
Set pvAddressOfSubclassProc = InitAddressOfMethod(Me, 5)
End Property
Private Sub Form_Load()
Set m_pSubclass = InitSubclassingThunk(hWnd, Me, pvAddressOfSubclassProc.SubclassProc(0, 0, 0, 0, 0))
End Sub
Public Function SubclassProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long, Handled As Boolean) As Long
Select Case wMsg
Case WM_SYSCOMMAND
If wParam = SC_CLOSE Then
Unload Me
Handled = True
End If
End Select
End Function
This will need mdModernSubclassing.bas from Moderen Subclassing Thunk repository added to your project for the IDE-safe subclassing implementation.

vba How to control message boxes from another program?

So I want to use
pid = Shell(MyApp, 1)
with VBA to automate the use of a crappy external program. This program unfortunately has all sorts of annoying dialog boxes and popups that must be clicked. Is there any way to directly control each dialog box to ensure that the "OK" button is pressed rather than "Cancel"?
Currently I am using
AppActivate pid
Application.SendKeys ("%R")
But this command can only guarantee the "OK" of the initial pop-up dialog. Subsequent pop-ups may not be clicked, especially because I'm not sure how to guarantee focus on the new popup. Is there a way to find the child process ID's of any new popups? Is there any way to directly "click" a particularly labeled control button?
How to Interact with a Button on a Dialog
Well, there is a way of sending a WM_COMMAND message to common buttons on a dialog box effectively closing it:
Declare Function SendMessageA Lib "user32" (ByVal hWnd As Long, ByVal Msg As Integer, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Const WM_COMMAND As Integer = 273
Const WM_CLOSE As Integer = 16
Const IDOK As Integer = 1
Const IDCANCEL As Integer = 2
Sub ShowDialogs()
If Shell("C:\Users\myself\dialogs.exe") <> 0 Then
Dim hWnd As Long
hWnd = FindWindowA("#32770", "Info")
Debug.Print hWnd
SendMessageA hWnd, WM_COMMAND, IDOK, 0
End If
End Sub
Important Notes:
I had to use Spy++ to find out what class name Windows gives to message box dialogs.
The Windows SDK headers contained useful constants, especially the value for WM_COMMAND which was obtained from WinUser.h.
Each time a dialog is a created, you will need to obtain the handle for the window again using FindWindowA, as shown in the preceding code.

Placeholder Text in VB6

I know this is an odd one, but is there a way to emulate the placeholder text functionality in VB6? If not, does anyone know of a good OCX control I could get somewhere that will do this? I'm sure it can be programmed in with a set of functions to do this, just looking for something already done.
The placeholder I'm asking about isn't the "formatting" in VB6, but like the text you see on a webform instead of a label for instance.
The text inside of a text box that tells you what information goes in that box, or provides and example of the information you want the user to enter into that particular box.
Any help is greatly appreciated, as always.
It sounds like you want Cue Banners. These also work on ComboBox controls.
Private Const CBM_FIRST As Long = &H1700&
Private Const CB_SETCUEBANNER As Long = CBM_FIRST + 3
Private Const ECM_FIRST As Long = &H1500&
Private Const EM_SETCUEBANNER As Long = ECM_FIRST + 1
Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Sub SetCueBannerTXT(ByVal TextBox As TextBox, ByVal CueText As String)
SendMessage TextBox.hWnd, EM_SETCUEBANNER, 0, StrPtr(CueText)
End Sub
Private Sub SetCueBannerCBO(ByVal ComboBox As ComboBox, ByVal CueText As String)
SendMessage ComboBox.hWnd, CB_SETCUEBANNER, 0, StrPtr(CueText)
End Sub
Note To use this API, you must provide a manifest specifying
Comclt32.dll version 6.0.
Let's see if I understand what you want correctly. You want a textbox that says (for example): "First Name" inside of it to show users what to enter?
This can be accomplished by setting the text value to "First Name" in design mode. Then, on the GotFocus event, you delete the text inside giving the user a blank textbox to enter their info.
To make it more user friendly, you can have grey text when it's just a label, and black text when its the users entry. You can also test for the text color so you don't delete the user's info if they reenter a textbox.

Form sizing to fill screen dimensions minus taskbar

How do you size your form in vb6 so that form lower border is at top of taskbar
Is there a reason you cannot just maximise the form? That would be my first impression.
If that's not a runner, you could try getting the taskbar height in the following way:
Private Const ABM_GETTASKBARPOS = &H5
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type APPBARDATA
cbSize As Long
hwnd As Long
uCallbackMessage As Long
uEdge As Long
rc As RECT
lParam As Long
End Type
Private Declare Function SHAppBarMessage Lib "shell32.dll" (ByVal dwMessage As Long, pData As APPBARDATA) As Long
Function GetTaskBarSize()
Dim ABD As APPBARDATA
SHAppBarMessage ABM_GETTASKBARPOS, ABD
MsgBox "Width:" & ABD.rc.Right - ABD.rc.Left
MsgBox " Height:" & ABD.rc.Bottom - ABD.rc.Top
End Sub
and then setting your form's height to the screen's height less the taskbar's height.
Minus only the taskbar? That might not really be what you want. There can be other windows on the edges of the screen that are meant to "carve out" regions of the desktop. Also, note that sometimes the height of the taskbar is irrelevant, such as when it's docked to the left or right side of the screen.
Galwegian has shown how to get the height of the taskbar, but if you're really looking for the usable area of the desktop, use the SystemParametersInfo function with the spi_GetWorkArea flag instead. It will tell you the area of the desktop excluding all desktop toolbars. MSDN advises that if you're interested in the space available on something other than the primary monitor, you should call GetMonitorInfo instead; it fills a record, and one of the fields is for the monitor's work area.
I'm going to second the idea that you might really just want to maximize your window. If you've already done that, and you want to know how much space you're taking up, then get the current size of your window, and then subtract the dimensions of your window's frame (which get "tucked under the edges" of the desktop when a window is maximized). You can use GetSystemMetrics with the sm_CXFrame and sm_CYFrame flags for that.
I'm going to agree you probably want to maximize your window.
But if you really do want to know the area of the desktop excluding all desktop toolbars (taskbar, Microsoft Office toolbar, etc), here's some VB6 declarations for the SystemParametersInfo call and a sample function that centres forms on the screen, allowing for the toolbars. This is borrowed from 101 tech tips (PDF) from the old Visual Basic Programmers Journal.
Private Const SPI_GETWORKAREA = 48
Private Declare Function SystemParametersInfo& Lib "User32" Alias "SystemParametersInfoA" ( _
ByVal uAction As Long, _
ByVal uParam As Long, lpvParam As Any, _
ByVal fuWinIni As Long)
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Function CenterForm32 (frm As Form)
Dim ScreenWidth&, ScreenHeight&, ScreenLeft&, ScreenTop&
Dim DesktopArea As RECT
Call SystemParametersInfo (SPI_GETWORKAREA, 0, DesktopArea, 0)
ScreenHeight = (DesktopArea.Bottom - DesktopArea.Top) * Screen.TwipsPerPixelY
ScreenWidth = (DesktopArea.Right - DesktopArea.Left) * Screen.TwipsPerPixelX
ScreenLeft = DesktopArea.Left * Screen.TwipsPerPixelX
ScreenTop = DesktopArea.Top * Screen.TwipsPerPixelY
frm.Move (ScreenWidth - frm.Width) / 2 + ScreenLeft, _
(ScreenHeight - frm.Height) / 2 + ScreenTop
End Function

How do I click a button on a vb6 form?

I have a vb6 form with an ocx control on it. The ocx control has a button on it that I want to press from code. How do I do this?
I have:
Dim b As CommandButton
Set b = ocx.GetButton("btnPrint")
SendMessage ocx.hwnd, WM_COMMAND, GetWindowLong(b.hwnd, GWL_ID), b.hwnd
but it doesn't seem to work.
I believe the following will work:
Dim b As CommandButton
Set b = ocx.GetButton("btnPrint")
b = True
CommandButtons actually have two functions. One is the usual click button and the other is a toggle button that acts similar to a CheckBox. The default property of the CommandButton is actually the Value property that indicates whether a button is toggled. By setting the property, the Click event is generated. This is done even if the button is not styled as a ToggleButton and therefore doesn't change its state.
If you have access to the OCX code, you could expose the associated event handler and invoke it directly.
Don't know if an equivalent of .Net Button's Click() method existed back in VB6 days
For keypress you can also use sendmessage sending both keydown and keyup:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Const WM_KEYDOWN As Integer = &H100
Const WM_KEYUP As Integer = &H101
Const VK_SPACE = &H20
Private Sub cmdCommand1_Click()
Dim b As CommandButton
Set b = ocx.GetButton("btnPrint")
SendMessage b.hWnd, WM_KEYDOWN, VK_SPACE, 0&
SendMessage b.hWnd, WM_KEYUP, VK_SPACE, 0&
End Sub
This:
Dim b As CommandButton
Set b = ocx.GetButton("btnPrint")
b = True
does work. Completely unintuitive. I'd expect it to throw an error since a bool is not a valid CommandButton, but it is because of the default property thing.
WM_LBUTTONDOWN would be a mouse click, what I want is a button click (button as in a hwnd button, not a mouse button).
I don't have access to the source of the ocx (it's a 3rd party control). If I did, I would expose the function that I wanted to call (the original writer of the ocx should have exposed it).
Do you have access to the OCX code? You shouldn't really be directly invoking the click of a button. You should refactor the code so that the OCX button click code calls a function, e.g.
CMyWindow::OnLButtonDown()
{
this->FooBar();
}
Then from your VB6 app, directly call the FooBar method. If you can't directly call functions from VB6 you can wrap the FooBar() method with a windows message proc function, e.g.
#define WM_FOOBAR WM_APP + 1
Then use SendMessage in the VB6, like SendMessage(WM_FOOBAR, ...)

Resources