How can i get handle for separate windows using MFC? - c++

I'm new to MFC. Can anyone please tell me how can I get handle separate windows using MFC. My task is to take screenshot of separate windows and I want to display it. By using CWnd::GetDesktopWindow I’ll take the handle for desktop. If I want to get handle for other windows how can I get it. Now i got the handle for desktop if I want to display the desktop which I captured how I can do it. Please anyone help me.

It depends on what types of window do you want to get. To retrieve some window has specified class name or caption, please use API FindWindow(...) with class&caption as input; to get all the child windows under desktop or some top level window, you could use EnumWindows(...) in a recursive function.

In order to get the handle to all the windows on your desktop, you need the function EnumWindows.
You provide it with a callback function and it will call it with a handle to each window it finds.
To get an MFC CWnd* from a HWND, you can do this:
CWnd *const window = CWnd::FromHandle(hWnd);

Related

How to Add a Window in ConsoleApplication Project C++

Hi i Want to Add Window(Gui) to my ConsoleApplication in C++
How to do this?? I need something called Hwnd? I tried to use it and it does not work for me
Basically what I want to do is a MessageBox that will be every time somewhere else on the screen .. but I realized that you have to do it with HWND.
I need Tutorial How to create windows with hwnd
I try:
HWND GetWindow(HWND hWnd, UINT uCmd);
Check if the following code meet your requirement:
#include <windows.h>
int main()
{
MessageBox(nullptr, TEXT("Hello World!"), TEXT("Message"), MB_TOPMOST);
}
It will like this:
You've used multiple tags, I have a winapi solution, not a winforms one.
You can use a std. MessageBox by calling the function, but they can't be „evolved“ into your custom windows for any reasonable „price“ so they can only notify user and ask simple yes/no questions. For „real“ windows, let's forget MessageBox.
The simplest custom window is a std. windows Dialog. It uses the pre-defined window class #32770, so you don't have to register your window class, create a message loop etc. The simplest way to open it is DialogBoxParam function. You must make a .rc dialog script (there are lots of visual editors), compile it with a resource compiler, link into your .exe and pass it's name as the parameter for DialogBoxParam.
Here's an example of a window inside a console application (C++ part only), using this DialogBoxParam call
DialogBoxParam(GetModuleHandle(NULL),"EXAMPLE",NULL,ExampleWindowFunction,NULL);
It's here: https://pastebin.com/Crkdy5FB
It also contains image drawing (you probably don't need it yet, it's left from another winapi example). Use it as a sandbox, you'll probably quickly understand how it works and what role the hWnd plays here.
I understand your problems. GUI got so overcomplicated so it's hard to understand what exactly you don't understand. It prevents from asking a good question.

Spy++ Win32 API Getting Window instance from Spy++ Information

I am using Spy++ to find windows, I am doing this as a test and realise the Handles change fequently. However, here is the information I get from Spy++. Can I use these handles to grab that window in C++
Here's how I get it from the name.
HWND main_window_handle = FindWindowA(NULL, WINDOW_NAME);
How can I get it using either the Window Handle or Instance Handle.
The window handle is the HWND and their values are not stable, it will probably change every time you run the program.
The instance handle (HINSTANCE) is also not stable and has little to do with finding a specific window in another application, it is the load address of the module (.exe or .dll) that created the window.
To find a window you will generally call FindWindow with a specific class name. If the class name of the window you are looking for is not really unique then you should probably use EnumWindows and try to look for other specific attributes and/or child windows to identify the top level window you are looking for.
It is also possible (and often the best approach) to use UI Automation to find and manipulate windows in 3rd-party applications.
Try using
HINSTANCE myInstance = (HINSTANCE)&__ImageBase;

How to know the name/ID of window in focus

How to know the name/ID of window in focus specifically in OpenCV.
Is there any event handling/callback or windows api that make this possible if it is not possible with OpenCV.
I am working on Windows 7 and 8.1.
I want to do something similar to (but not limited to):
If a window is in focus, and some event like mouse or key press happens,
then update that particular window
.
As suggested by Kris, there exists a solution on window. Use windows api to take handle of active window as using:
HWND WINAPI GetActiveWindow(void);
Now use GetWindowText() function to extract out title, if any. It may not be portable. There should exist similar api for other OS.
first of all, I don't know if windows or any other api can help.
I have an idea to take screen-shot, somehow, and then use opencv itself for image processing. Assumption is that the focused window will be on the top and most focused so we can see the name easily. We can process color to separate focused window from others, if any. And using OCR to extract windows name.

mfc c++ set HWND address of another application through edit control

I know how to get and set the handle of another applications window in the code, but I don't know how to set the applications handle at runtime through an edit control. the problem is is that I have to keep compiling my application every time I want to use it, because the handle of the window in application 2 is dynamic.
does anyone have any ideas?
To find current HWND of application window you are interested in, you can enumareate windows using EnumWindows:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633497%28v=vs.85%29.aspx
or maybe easier is to use FindWindow, if you know its name/class:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633499%28v=vs.85%29.aspx
you can also use GetWindow to iterate windows:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633515%28v=vs.85%29.aspx
...lots of possibilities

Find a window using c++ and modifying controls

I would like to use c++ without mfc(and not clr) in order to modify textbox's and activate a button on a form outside of my project. I don't know where to start. I've done a lot of searching but can only find information for VB. A starting point would help.
Thanks.
I tried this and it doesn't seem to work.
HWND fWindow = FindWindow(NULL ,(LPCWSTR)"title");
and I also tried this
HWND fWindow = FindWindow(NULL ,LPCWSTR("title"));
I ALSO tried using LPTSTR instead of LPCWSTR, incase it was a unicode deal.
Maybe I don't understand this microsoft LPCWSTR and LPTSTR crap.
I also tried
HWND fWindow = FindWindow(NULL,TEXT("title"));
and that didn't work.
I guess the windows api must just be broken.
I tried the function on other programs...I'm using xp and I tried catching the calculator, and an explorer window, and something else. But I got nothing.
Heres some of the exact code I'm using to try and figure this out.
HWND face = NULL;
face = FindWindow(NULL,TEXT("My Computer"));
LPSTR title = TEXT("");
GetWindowText(face,title,250);
if(face != NULL)
{
MessageBox(NULL,title,TEXT("WOOP"),1);
}
face = nothing.
title = ""
Bear in mind, I'm not actually trying to hook explorer, I just want to figure out how to get it to work.
Use spy++ or winspector to see the actual "text" of the window.
(Strictly speaking, the caption of the window need not match it's window text. Especially true of "fancy" windows which paint their own caption.)
The following works fine for me (using Calc.exe to test).
HWND hwnd = NULL;
hwnd = FindWindow(NULL,_T("Calculator"));
TCHAR title[251];
if(hwnd != NULL)
{
GetWindowText(hwnd,title,250);
MessageBox(NULL,title,_T("WOOP"),MB_OK);
}
else
MessageBox(NULL,_T("No such window."),_T("OOPS"),MB_OK);
Edit: You should have used _TEXT instead of TEXT.
One way to do this is to use FindWindow to get a handle to the form. Then if you know the button and edit window Ids, you can use GetDlgItem to get their window handles. If you dont know the ids, you can use EnumChildWindows to examine all of the controls on the form.
Once you have the window handles for the controls, you can use SetWindowText to set the text on the edit control, and send a WM_COMMAND message to the form window with the button ID as the command value to make the form think that the button has been clicked.
There are a lot of ways to go about this once you have the correct window handles. There are security issues when you use the window handles of another process, but if the process isn't secured, then inter-process use of window handles just works. For a secured process, you won't be able to find out the window handles.
The windows API provides Methods for this. These should be independent of MFC and CLR, as they are plain win32. I had a project once accessing the Form fields of an Applictation from a loaded DLL (don't ask why).
you might want to look here (Codeproject)
or here (msdn)
At first, you need to obtain a handle to the process you want to access.
When have this, you can use GetDlgItem() (search msdn for that) to retrieve a handle to the desired textbox.
With this handle, you should be able to modify the control in question.
If your trying to get big (and do some more UI automation), you sould have a closer look at these:
Microsoft Active Accessibility
IAccessible2
Microsoft UI Automation
Windows Automation API (Win7)