Reputation: 10907
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:
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. :D It's a surprise.
Upvotes: 1
Views: 2632
Reputation: 10907
I eventually stole the window into Notepad. All I had to do was get rid of Notepad's child editor window and the paint issue goes away along with it.
Also the good styles that need to be applied are WS_CHILD
on Chrome and WS_POPUP
on Notepad, followed by UIS_INITIALIZE
WM_CHANGEUISTATE
message on both.
I really hope Chrome Dev doesn't change this behavior.
Upvotes: 1
Reputation: 47952
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:
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
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?
Upvotes: 3