在另一个应用程序中创建一个子窗口 [英] Create a child window inside another application

查看:31
本文介绍了在另一个应用程序中创建一个子窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Windows API 为 PC 游戏编写 C++ 机器人.我设法找到了进程 HANDLE、它的 ID 和最后它的 HWNDEnumWindows.我还设法将消息发送到其窗口(使用 SendMessage)以模拟击键.由于游戏全屏运行,我想在其中创建一个带有自定义控件的子窗口,以便轻松打开/关闭机器人,但我遇到了问题.我尝试注册一个窗口类并使用游戏的 HWND 作为父窗口创建一个窗口,但我什么也看不到.这是我用来创建子窗口的代码(不起作用):

I'm trying to write a bot in C++ for a PC game using the windows API. I've managed to find the process HANDLE, its ID and finally its HWNDwith EnumWindows. I've managed also to send messages to its window (using SendMessage) in order to simulate keystrokes. Since the game runs full screen I'd like to create a child window inside it with custom controls to switch on/off the bot easily but i'm having problems with it. I've tried to register a window class and create a window with the game's HWND as a parent but I can't see anything. This is the code I've used to create the child window (not working):

// ... Window class registering etc...

HWND hChild = CreateWindowEx(0, 
                             "MyWindowClass", 
                             "Title", 
                             WS_CHILD | WS_VISIBLE, 
                             0, 0, 100, 100, 
                             hParent, 
                             NULL, 
                             AHInstance, 
                             NULL); // Parent is a valid window handle from another process

老实说,我不是一个熟练的 Windows API 程序员,这一切对我来说都是新的.我什至不确定这是否是做这种事情的正确方法.

Honestly I'm not a skilled windows API programmer, this whole thing is new to me. I'm not even sure if this is the right way to do such thing.

当我最小化/最大化游戏时,我的窗口会正确显示一两秒钟.似乎游戏的渲染是在我的窗口渲染之后发生的.任何的想法?消息呢?我应该管理它们吗?游戏是否会将消息转发到我的窗口?

When i minimize/maximize the game my window shows correctly for a second or two. It seems that the game's rendering is happening after the rendering of my window. Any idea? And what about messages? Should i manage them or not? Is the game going to forward messages to my window?

推荐答案

Windows 有 线程关联.这意味着创建窗口的线程将处理该窗口的消息.此外,父子链中的所有窗口都应该由同一个线程处理.否则,坏事发生.

Windows have thread affinity. This means that the thread that creates the window will be processing the messages for that window. Furthermore, all windows in a parent-child chain should be processed by the same thread. Otherwise, bad things happen.

那么 - 您的代码是如何运行的?如果它是一个单独的可执行文件,那么最好将窗口保留为顶级窗口,并简单地监视目标窗口的移动事件(使用 UI 自动化,例如)并让您的窗口适当移动.

So - how is your code being run? If it's a separate executable, you're going to be much better off leaving your window as a top-level window, and simply monitoring the target window for move events (using UI Automation, for example) and having your window move appropriately.

否则,您将需要将代码注入游戏可执行文件中,并让它在适当的线程上创建窗口.我不建议这样做.

Otherwise, you're going to need to inject your code into the game executable, and have it create the windows on the appropriate thread. I wouldn't recommend that.

这篇关于在另一个应用程序中创建一个子窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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