C/C ++-Windows-如何跟踪HWND上下文数据 [英] C/C++ - Windows - how to keep track of HWND context data

查看:30
本文介绍了C/C ++-Windows-如何跟踪HWND上下文数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows中,假设您打开了同一窗口类的多个窗口(HWND).您如何在窗口过程中跟踪上下文数据,例如,当用户尝试在窗口2中键入内容时,窗口1不会被修改?

In Windows, suppose you have multiple windows (HWNDs) of the same window class open. How do you keep track of the context data in the window procedure so that, for example, window 1 does not get modified when the user tried to type in window 2?

CreateWindow()不会返回,因此您不能简单地将结果HWND设置为上下文数据并在WndProc()中进行查找;您确实需要在WndProc()中进行设置.

CreateWindow() does not return until after the WndProc() has been called several times, so you can't simply set the resulting HWND to the context data and do a lookup in the WndProc(); you do need to set it in the WndProc().

WndProc()没有直接将上下文信息传递给它,但是不幸的是,窗口创建消息并不是要传递给WndProc()的第一条消息.不,我发现诸如WM_SIZE,WM_NCSIZE之类的东西,甚至其他一些东西,在我看到WM_CREATE之前都已通过.

WndProc() doesn't directly have the context information passed to it except on window creation messages, but unfortunately window creation messages aren't exactly the first messages to be passed to WndProc(). Nay, I find things such as WM_SIZE, WM_NCSIZE, and even some others are passed before I ever see WM_CREATE.

在具有大量窗口的情况下,将HWND存储在链表类型的存储机制中会效率低下:一个窗口中的每个控件只是另一种类型的窗口,因此,您需要对其进行跟踪的另一个HWND;经过几百个控件之后,在短时间内将几十条消息传递给程序后,在链接列表中搜索HWND将成为程序的主要瓶颈!

Storing the HWND in a linked list type of storage mechanism would be inefficient with large amounts of windows: each control in a window is simply another type of window and therefore another HWND of which you need to keep track; after a few hundred controls, searching the linked list for the HWND will be a major bottleneck in the program after a few dozen messages are passed to the program in a short amount of time!

据我所知,有些人使用SetWindowLong()-但我也听说有些库也喜欢使用SetWindowLong()将自己的上下文信息与程序分开存储,并且有时会发生窗口数据冲突.如何避免这种情况?

From what I hear, some people use SetWindowLong() - but I also hear that some libraries like to use that too to store their own context information separate from the program and that window data collisions can sometimes occur. How can that be avoided?

推荐答案

如果我对您的理解正确,则希望避免一个窗口捕获来自另一个窗口的消息.避免这种情况的一种方法是使用 this中提出的解决方案 线程,该线程跟踪由您创建的窗口,并通过将调用者的指针存储在 GWL_USERDATA .

if I'm understanding you correctly, you want to avoid one window to catch the messages from another. One way to avoid this is to use the solution proposed in this thread, which keeps track of the windows that is created by you and makes sure that the correct windows gets the messages associated to it by storing the pointer for the caller in the GWL_USERDATA .

// ...
m_hWnd = CreateWindowEx(0,"Classname","Title",WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT,CW_USEDEFAULT,
                        320,200,NULL,NULL,hInstance, /*the magic pointer*/ this);

// ...

if(uMsg == WM_CREATE)
{
    // collected here..
    pParent = (CWindow*)((LPCREATESTRUCT)lParam)->lpCreateParams;
    // .. and then stored for later lookup
    SetWindowLongPtr(hWnd,GWL_USERDATA,(LONG_PTR)pParent); 
}
// ...

您还可以按照 Moo-Juice .
而且我认为您不必担心 CREATE
之前的消息,因为此时窗口还没有完全初始化.如果需要设置文本,则可以在调用 CreateWindow(Ex)之后执行此操作,无论是用户输入还是 SendMessage 调用.

You can also catch the WM_NCCREATE message, as proposed by Moo-Juice.
And I don't think you should worry about the messages pre-WM_CREATE, because the window isn't even fully initialized at that point. If you need to set text you do that after the call to CreateWindow(Ex), be it user input or a SendMessagecall.

这篇关于C/C ++-Windows-如何跟踪HWND上下文数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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