windows api sdk 程序 看不见窗口
来看看这段程序:
//main.cpp#include <windows.h>#include <fstream>#include <string>LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int CmdShow){ static TCHAR AppName[] = TEXT("HappyBirthday!"); HWND hwnd; MSG msg; WNDCLASS winclass; winclass.style = CS_HREDRAW | CS_VREDRAW; winclass.lpfnWndProc = WndProc; winclass.cbClsExtra = 0; winclass.cbWndExtra = 0; winclass.hInstance = hInstance; winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION); winclass.hCursor = LoadCursor(NULL,IDC_ARROW); winclass.hbrBackground = (HBRUSH) GetStockObject( GRAY_BRUSH); winclass.lpszMenuName = AppName; winclass.lpszClassName = AppName; if( !RegisterClass(&winclass) ) { MessageBox(NULL,TEXT(""),AppName,MB_ICONERROR); return 0; } hwnd = CreateWindow(AppName, TEXT("The hello Program"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd,CmdShow); UpdateWindow(hwnd); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam;}//this function is write to play a sound and show a line of textLRESULT CALLBACK WndProc(HWND hwnd, UINT message,WPARAM wParam, LPARAM lParam){ std::ifstream fileI;//input stream std::string text;//to store the first line of the file above wchar_t *convert; HDC hdc; PAINTSTRUCT ps; RECT rect; fileI.open("String.txt");//this is a string stored in a text file getline(fileI,text);//load the first line of the file convert = (wchar_t*) text.c_str();//convert the text code into union code wchar_t const *PCWText = convert;//ande then change it to a const pointer so that compiler won't give a warning switch( message ) { case WM_CREATE: PlaySound(TEXT("hellowin.wav"),NULL,SND_FILENAME|SND_ASYNC); return 0; case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; GetClientRect (hwnd, &rect) ; DrawTextW (hdc, PCWText, -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER) ; EndPaint (hwnd, &ps) ; return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } fileI.close();//close the input stream}
[解决办法]
不好意思,看错了。
最后加这一句就可以了:
fileI.close();//close the input streamreturn DefWindowProc (hwnd, message, wParam, lParam);}