将窗口窃取到 CreateWindow 窗口会创建一个“冻结"的窗口.窗户? [英] Stealing a window into a CreateWindow window creates a "frozen" window?

查看:28
本文介绍了将窗口窃取到 CreateWindow 窗口会创建一个“冻结"的窗口.窗户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是从屏幕上窃取一个窗口并使其成为我自己创建的窗口的子窗口.当我的程序关闭时,被盗的窗口也随之消失,可能与它的进程一起消失.

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.

这里是我的问题:

  1. 创建的窗口被冻结,它不允许我操作它的控件.控制台是否阻止它被操作?如果是这样,我该如何解决这个问题?
  2. (下面的代码)仅在第二次运行时窃取窗口,在第一次运行时不会这样做,并且窗口仍留在任务栏中.
  3. 我尝试做同样的事情,只是我把 Chrome 窗口偷到了记事本窗口.同样的问题,当它确实偷走了窗口时,一切看起来都被完全撕裂了,使浏览器几乎无法运行.

这是我使用的代码(Win32 控制台应用程序):

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();
}

哦,顺便说一句,请不要问我想要达到什么目标.:D 这是一个惊喜.

Oh BTW, please don't ask me what I'm trying to achieve. :D 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.)

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

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).

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

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?

这篇关于将窗口窃取到 CreateWindow 窗口会创建一个“冻结"的窗口.窗户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆