no window appears in Visual C++ program
I am developing an Win32 application where user logins when the application starts in a login window and then the main window opens
But when I run the program nothing appears.
Here's the code
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;
}
Please help me find the solution.
return DefWindowProc(hWnd, msg, wParam, lParam );
Wrong window handle, it should be hWnd2.
To uplift this post beyond a 'debug my code for me' answer, there are two fundamental things you do wrong, they are going to get you into a lot more trouble:
- too sloppy on picking good variable names. You should never have a global variable named "hWnd". That should of course be hWndMain, you'd have never made that mistake.
- no error check whatsoever. If you had any, you would have quickly discovered that CreateWindowEx() returned NULL and GetLastError() returned 1400. Without error checking, you can only watch it misbehave but never diagnose it.
Using the raw winapi requires meticulous attention to such small details. Consider using a class library instead.
how come you are calling:
ShowWindow( hWnd, SW_MAXIMIZE ) ;
UpdateWindow(hWnd);
only after and in the scope of: case WM_DESTROY:
I would imagine you call ShowWindow in normal execution and not in the distruction :)
精彩评论