我用下面的代码获取HWND->i
但是当我开始它对话框终止程序“的Program.exe遇到了问题,需要关闭”:是否可以在MessageBox()中显示HWND-> i?
#include <windows.h>
#include <sstream>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Program") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("Error"), szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName,
TEXT ("Program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL) ;
ShowWindow (hwnd, iCmdShow) ;
std::ostringstream oss;
oss << hwnd->i;
//MessageBox(NULL, oss.str().c_str(), TEXT("message"), MB_OK);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
当我注释掉
oss << hwnd->i;
程序正常运行。
那么,有没有办法在MessageBox()
中显示hwnd->i
?
'HWND'是一个不透明的指针给你,我通常不会被用户解除引用。 – legends2k
你能解释一下你在哪里得到'HWND'指向一个拥有'i'成员的结构的想法吗? – Kaz
@Kaz它在MinGW安装文件夹的include \ windef.h中。有一个宏'DECLARE_HANDLE(HWND);'扩展为'typedef struct HWND __ {int i;} * HWND'。 'DECLARE_HANDLE()'宏在include \ winnt.h中。 – user1257