Ho do I get hWnd of the current window/form in VB6?
If you're on the form: Me.hWnd. If you don't know which form is the current form: Screen.ActiveForm.hWnd
Using Windows API, GetForegroundWindow() will get the handle of the topmost window regardless of which application it is from, and GetActiveWindow() will get the handle of your application's active window. The Declare statements you will need:
Declare Function GetForegroundWindow Lib "user32.dll" () As Long
Declare Function GetActiveWindow Lib "user32.dll" () As Long
Calling either function will return a window handle as described above.
It's been a long time since I used VB6, but this is what I remember:
You'll want to open the API Viewer, which should be in the Start Menu around the VB6 entry. When you open it, you want to select win32api.txt, and you'll get a list of all of the Win32 API functions. This is the easiest way to not mess up the function signatures. Copy and paste the function declaration into one of your VB6 modules.
I always "cheated" and just looked for my window by caption name, instead of looping over all of the available windows with GetWindow. If you're okay with this, you want to use FindWindow and pass the caption name as the second parameter.
Related
In an MFC program I am trying to access controls that are in one window (class) from another (sibling or daughter) window with code in a different .cpp file. Typically you access a control with a DDX_Control variable defined in the class .cpp file.
DDX_Control(pDX, IDC_STATUS, m_Status);
The code for the other window is in a different file, so I don’t have access to the control variable (m_Status). What I have done is used FindWindow to find the main window, then GetDlgItem to find the control I am interested in:
CWnd * tmpWnd = CWnd::FindWindow(NULL,"MainWindow"); // find the main dialog box
CStatic * tmpStatus = (CStatic*) tmpWnd->GetDlgItem(IDC_Status);
tmpStatus->SetWindowText(“Status Report);
This works fine in the debugger, but fails when executed outside the debugger. FindWindow gets the window ID correctly, but GetDlgItem returns null.
Ideally I’d like to access the control variable (m_Status) from the other window, but I don’ know how to do so. I understand the GetDlgItem is less than ideal under any circumstance.
One solution would be to send a message to the main window class and tell it what to do, but I’d have to have a routine to handle each control and know how to handle whatever kind of data I am sending.
Is there a “right” way to do this?
Thank you
The ultimate answer is to cast to the original class:
((CspDlg *)AfxGetMainWnd())->m_Status.SetWindowText("Report");
Since you created the "main" window you have an object or pointer for it. Or, you can call AfxGetMainWnd() to get a pointer to it. Then you can use that to access a public member such as m_Status. All of your windows are interconnected and it should not be necessary to use FindWindow to find any window in your own program.
The fact that some variables may be defined in another file is not relevant. That can be handled with suitable use of #include "theotherfile.h" and object pointers/references.
I have a plugin which is primarily used for screen-capture. It loads with the browser. Now on click of the button a small window appears notifying the screen capture has started.
Here, I also want the browser to get minimized.
I tried the following approaches:
Approach 1.
HWND parentWH = ::FindWindow(L"Chrome_WidgetWin_1", L"test - SC1 - Google Chrome");
::ShowWindow(parentWH,SW_MINIMIZE);
It works! But the question is that how should I take the parameters of FindWindow dynamically.
like, parameter1 being 'WindowClassName' and 2 being 'WindowTitle'. for different browsers.
Approach 2.
HWND parentWH = ::GetAncestor(this->pluginWindowHandle,GA_ROOTOWNER);
::ShowWindow(parentWH,SW_MINIMIZE);
Does Not Work!
To minimize the browser window containing the plugin you've used to start a screen capture, I'd suggest the following steps:
Pass the document.title (JavaScript) of the page which embeds your plugin to the plugin.
Create an EnumWindowsProc callback function in your WinApi dll. This function will be given a HWND every time it is called. Inside this function you should use the GetWindowText function to check if the document.title from the browser is a substring of the title corresponding to the current HWND. If it is, then you can minimize the current HWND using ShowWindow, and if you want to restore it after a capture, you can store the HWND and use it again later. Return FALSE if you have found a match, and TRUE otherwise.
Use the EnumWindows function to enumerate through the windows on your system. Into this, you will pass a pointer to the callback function above. EnumWindows will run until the callback function returns FALSE, or it has enumerated through all of the windows.
Here's what worked for me for minimizing the browser window.
Approach 1:
::ShowWindow(pluginWindowHandle,SW_HIDE);
parentWH = ::GetForegroundWindow(); //for obtaining parent window handle
::ShowWindow(parentWH,SW_MINIMIZE);
Approach 2:
Before showing the plugin window, i'm getting the browser window handle
void NotificationWindow::showNotification()
{
parentWH = ::GetForegroundWindow();
::ShowWindow(parentWH,SW_MINIMIZE);
this->close();
this->displayWindow();
}
Here, till the displayWindow() function is not called, we can get the browser window handle directly by using GetForegroundWindow() function.
I'm using Excel(2003) VBA. I have "UserForm1" with a command button, "bHide". I'm using FindWindow to get the hwnd for UserForm1. With that I'm successfully changing the window style (removing the form border). I also want to modify "bHide" but I need to get its handle.
What's the best way to get the handle for "bHide"? Thanks.
I have a DLL that I want to play sounds using Direct Sound. In order to play sounds, I need the HWND of the executable. I don't have a HWND of the executable that loads the DLL. How do I get that in the DLL without passing it in from the executable?
You could use GetCurrentProcessId to get the current process Id.
You could then call EnumWindows, and check each window with GetWindowThreadProcessId to find a window associated with your process.
However, an easier option might be to just generate your own Window. You can create a 1x1 pixel window that is not visible, and use it with Direct Sound.
This has the advantage of working even if your calling process doesn't have a usable window (or deletes window handles regularly).
Call GetGUIThreadInfo on the main thread. This gets you a bunch of HWNDs. If you need a top-level HWND, pick any valid one (not all values may be filled) and find its top level ancestor with GetAncestor(GA_ROOT).
How can I get the icon of a running application provided I know the Hwnd?
If you have the handle to the window, you can use GetClassLong:
HICON icon = (HICON)GetClassLong(window, GCL_HICON);
I you have the hwnd you can get the process ID by using the WINAPI GetWindowThreadProcessId.
With that you can create a C# Process object. Next, you can iterate through the process' ProcessModule Collection to get the filename of the executable. Finally, you can use the WINAPI function ExtractIconEx to get the icon from the path
Pinvoke has information on the two WINAPI methods
http://www.pinvoke.net/default.aspx/user32/GetWindowThreadProcessId.html
http://www.pinvoke.net/default.aspx/shell32/ExtractIconEx.html