我正在开发一个Win32应用程序,用户在登录窗口中启动应用程序时登录,然后打开主窗口
但是当我运行该程序时,没有任何显示。
这是代码
HINSTANCE ghInstance;
HWND hWnd;
HWND hWndPopUp;
LRESULT WINAPI PopUpWndProc( HWND hWnd2, UINT msg, WPARAM wParam, LPARAM lParam );
LRESULT WINAPI MainWndProc( HWND hWnd , UINT msg , WPARAM wParam , LPARAM lParam );
int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpszCmdLine , int nCmdShow){
WNDCLASS wc;
WNDCLASS wcPopUp;
MSG msg;
//HWND hWnd;
BOOL bRet;
if( !hPrevInstance )
{
wc.lpszClassName = L"MainAppClass" ;
wc.lpfnWndProc = MainWndProc ;
wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW ;
wc.hInstance = hInstance ;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ) ;
wc.hCursor = LoadCursor( NULL, IDC_ARROW ) ;
wc.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 ) ;
wc.lpszMenuName = NULL ;
wc.cbClsExtra = 0 ;
wc.cbWndExtra = 0 ;
RegisterClass( &wc ) ;
wcPopUp.lpszClassName = L"PopUpAppClass" ;
wcPopUp.lpfnWndProc = PopUpWndProc ;
wcPopUp.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW ;
wcPopUp.hInstance = hInstance ;
wcPopUp.hIcon = LoadIcon( NULL, IDI_APPLICATION ) ;
wcPopUp.hCursor = LoadCursor( NULL, IDC_ARROW ) ;
wcPopUp.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 ) ;
wcPopUp.lpszMenuName = NULL ;
wcPopUp.cbClsExtra = 0 ;
wcPopUp.cbWndExtra = 0 ;
RegisterClass( &wcPopUp );
}
ghInstance = hInstance;
hWndPopUp = CreateWindowEx(WS_EX_CONTEXTHELP,
wcPopUp.lpszClassName,
L"Stock Ticker Login",
WS_OVERLAPPEDWINDOW,
0,
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
bool show = ShowWindow( hWndPopUp, SW_MAXIMIZE ) ;
UpdateWindow(hWndPopUp);
hPE = NULL;
/** While there is no WM_QUIT message in the Message Queue
Fetch Message from the queue and Dispatch it to WindowProc()
**/
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0 )
{
if (bRet == -1)
{
// handle the error and possibly exit
int nerror = GetLastError();
MessageBox(hWnd,L"Window Error",L"Window error", MB_ICONERROR);
exit(1);
}
else {
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
return (int)msg.wParam;
}
LRESULT WINAPI PopUpWndProc( HWND hWnd2, UINT msg, WPARAM wParam, LPARAM lParam ){
HDC hdc;
PAINTSTRUCT ps;
RECT rc;
GetClientRect(hWnd, &rc);
int height = rc.bottom - rc.top;
int width = rc.right -rc.left;
switch( msg )
{
case WM_CREATE:
hWndStaticUsername = CreateWindowEx( 0
, L"static"
, L"Username"
, WS_CHILD | WS_VISIBLE
, rc.left + width/8
, rc.top + height/4
, 100
, 30
, hWnd2
, 0
, ghInstance
, 0
);
hWndEditUsername = CreateWindowEx( WS_EX_CLIENTEDGE
, L"edit"
, L""
, WS_CHILD | WS_VISIBLE
, rc.left + width/8 + 150
, rc.top + height/4
, 150
, 30
, hWnd2
, 0
, ghInstance
, 0
);
hWndBtnLogin = CreateWindowEx( 0
, L"button"
, L"Login"
, WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON
, rc.left + width/8
, rc.top + height/4 + 80
, 50
, 30
, hWnd2
, 0
, ghInstance
, 0
);
break;
case WM_COMMAND:
if(HIWORD(wParam)== BN_CLICKED && lParam==(LPARAM)hWndBtnLogin )
{
TCHAR* uname =new TCHAR;
Edit_GetText(hWndEditUsername, uname, 20);
if(wcscmp(uname,L"")==0)
return 0;
else
{
StartTicker(uname);
DestroyWindow(hWnd);
}
}
break;
case WM_DESTROY:
hWnd = CreateWindowEx(WS_EX_CONTEXTHELP,
L"MainAppClass",
L"Assignment 2",
WS_OVERLAPPEDWINDOW,
0,
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
ghInstance,
NULL
);
ShowWindow( hWnd, SW_MAXIMIZE ) ;
UpdateWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam );
}
return 0;
}
请帮我找到解决方案。
答案 0 :(得分:3)
return DefWindowProc(hWnd, msg, wParam, lParam );
错误的窗口句柄,应该是hWnd2。
要将这篇文章提升为“为我调试我的代码”的答案,有两件事你做错了,它们会给你带来更多麻烦:
使用原始的winapi需要一丝不苟地关注这些细节。请考虑使用类库。
答案 1 :(得分:1)
你怎么称呼:
ShowWindow( hWnd, SW_MAXIMIZE ) ;
UpdateWindow(hWnd);
仅在case WM_DESTROY:
我想你会在正常执行中调用ShowWindow,而不是在解构中调用:)