1

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:

  1. 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?
  2. (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.
  3. 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. :D It's a surprise.

4

2 回答 2

3

您似乎没有运行消息循环,这对于您自己的窗口是必需的,并且对于在子级和父级之间传递消息可能是必需的。这似乎是被盗窗户似乎被锁定的最可能原因。(可能还有其他问题,但我会从这里开始。)

尝试在你有 getch 调用的地方添加一个基本的消息循环:

MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0) > 0) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

可能会有额外的困难。由于每个线程的消息队列,在另一个进程中有一个子窗口很棘手。与神话相反,它可以工作:(多进程浏览器这样做)。

您可能从 Chrome 中获取了错误的窗口。请记住,Chrome 也玩这个游戏,在不同的进程中创建子窗口。您是在抓住其中一个孩子还是主框架窗口?

于 2012-06-11T23:48:38.430 回答
1

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.

enter image description here

于 2012-06-12T07:29:05.063 回答