What I’m trying to do is steal a window from the screen and make it a child of my own created window. When my program closes, the stolen windows goes away too, probably along with its process.
So here are my questions:
- The created window is frozen, it won’t let me operate its controls. Does the console prevent it from being operated? If so, how can I fix this?
- (code below) only steals the window on its second run, it doesn’t do it in the first run and the window is still left in the taskbar.
- I tried doing the same except I’ve stolen a Chrome window into a notepad window. Same problems and when it did stole the window, everything looked completely torn apart, rendering the browser virtually inoperable.
Here’s the code I used (Win32 Console Application):
#include <conio.h>
#include <stdio.h>
#include <Windows.h>
#include <winuser.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LPCWSTR sClassName = L"MyClass";
HWND CreateTheWindow(LPCWSTR WindowTitle) {
// Create & register the class
WNDCLASSEX WndClass;
WndClass.cbSize = sizeof(WNDCLASSEX); WndClass.style = NULL; WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0; WndClass.cbWndExtra = 0; WndClass.lpszClassName = sClassName;
WndClass.hInstance = NULL; WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WndClass.lpszMenuName = NULL; WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&WndClass);
// Create & show the window
HWND hwnd = CreateWindowEx(WS_EX_STATICEDGE, sClassName, WindowTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 320, 240, NULL, NULL, NULL, NULL);
ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd);
return hwnd;
}
// No idea what's this for, back in JS we simply had to do window.open
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_CLOSE: DestroyWindow(hwnd); break;
case WM_DESTROY: PostQuitMessage(0); break;
default: return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
// start
void main()
{
HWND chrome = FindWindow(L"Chrome_WidgetWin_1", NULL);
HWND mywin = CreateTheWindow(L"HELLO BOSS");
if(chrome!=0) printf("Got Chrome\r\n"); else printf("Chrome not found\r\n");
if(mywin!=0) printf("Got yours\r\n"); else printf("Your window not found\r\n");
SetParent(chrome, mywin);
SetWindowLong(chrome, GWL_STYLE, WS_CHILDWINDOW | WS_VISIBLE );
UpdateWindow(chrome);
UpdateWindow(mywin);
_getch();
}
Oh BTW, please don’t ask me what I’m trying to achieve. 😀 It’s a surprise.
You don’t seem to be running a message loop, which is necessary for your own window, and is probably necessary for pumping messages that go between the child and the parent. That seems the most likely reason why the stolen window seems to be locked up. (There may be other problems, but I’d start here.)
Try adding a basic message loop where you have the getch call:
There may be additional difficulties. Having a child window in another process is tricky because of the per-thread message queues. Contrary to myth, it can be made to work: (multi-process browsers do it).
You might be grabbing the wrong window from Chrome. Remember that Chrome also plays this game, creating child windows in separate processes. Are you grabbing one of the children or the main frame window?