Relationship Between a C++ Window Object and an HWND The window object is an object of the C++ CWnd class (or a derived class) that your program creates directly. It comes and goes in response to your program’s constructor and destructor calls. The Windows window, on the other hand, is an opaque handle to an internal Windows data structure that corresponds to a window and consumes system resources when present. A Windows window is identified by a “window handle” (HWND) and is created after the CWnd object is created by a call to the Create member function of class CWnd. The window may be destroyed either by a program call or by a user’s action. The window handle is stored in the window object’s m_hWnd member variable. The following figure shows the relationship between the C++ window object and the Windows window. Creating windows is discussed in Creating Windows. Destroying windows is discussed in Destroying Window Objects.
cwnd 的 m_hwnd就是这个窗口对象的HWND 也可以通过GetSafeHwnd获得HWND
CWnd::CreateEx BOOL CreateEx( DWORD dwExStyle, LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hwndParent, HMENU nIDorHMenu, LPVOID lpParam = NULL );给一个窗口发消息,其中就是hwnd LRESULT SendMessage( HWND hWnd, // handle of destination window UINT Msg, // message to send WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); CWnd就是mfc中的窗口类,对应一个窗口, 也可以不对应一个具体的窗口
HWND类型的句柄:是一个由系统维护的,一个安全的,带索引的,一个代表某类资源或对象,对 句柄是更安全的,更方便的,是MS 封装了的两个都是一个地址,从形式上看没有然后区别,其实任何一个变量最后都是由地址表示 上面是我的片面之词语,目的是 抛砖引玉~
The window object is an object of the C++ CWnd class (or a derived class) that your program creates directly. It comes and goes in response to your program’s constructor and destructor calls. The Windows window, on the other hand, is an opaque handle to an internal Windows data structure that corresponds to a window and consumes system resources when present. A Windows window is identified by a “window handle” (HWND) and is created after the CWnd object is created by a call to the Create member function of class CWnd. The window may be destroyed either by a program call or by a user’s action. The window handle is stored in the window object’s m_hWnd member variable. The following figure shows the relationship between the C++ window object and the Windows window. Creating windows is discussed in Creating Windows. Destroying windows is discussed in Destroying Window Objects.
也可以通过GetSafeHwnd获得HWND
BOOL CreateEx( DWORD dwExStyle, LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hwndParent, HMENU nIDorHMenu, LPVOID lpParam = NULL );给一个窗口发消息,其中就是hwnd
LRESULT SendMessage(
HWND hWnd, // handle of destination window
UINT Msg, // message to send
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
CWnd就是mfc中的窗口类,对应一个窗口, 也可以不对应一个具体的窗口