windows api sdk 程序 看不见窗口
来看看这段程序:
- C/C++ code
//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}
这个程序运行之后能听见声音,也能看见文本。但是就是不显示窗口。给人的感觉就是有人在你电脑上贴了个小纸条,还拿不下来(任务栏右击不显示“关闭”选项,win7环境虽然有关闭选项,但是点了没用)。想关闭这个程序只能用任务管理器。这个程序的问题在哪里?我刚接触SDK编程,请多说一点。
[解决办法]
需要调用InitCommonControls注册一下。
[解决办法]
InitCommonControls
InitCommonControls() is required on Windows XP if an application manifest specifies use of ComCtl32.dll version 6 or later to enable visual styles. Otherwise, any window creation will fail.
The text below is form MSDN:
Registers and initializes certain common control window classes. This function is obsolete. New applications should use the InitCommonControlsEx function
Under Comctl32.dll version 5.x, only Microsoft Windows 95 classes (ICC_WIN95_CLASSES) can be registered through InitCommonControls. Programs which require additional common control classes must use the InitCommonControlsEx function.
Under Comctl32.dll version 6.0 and later, InitCommonControls does nothing. Applications must explicitly register all common controls through InitCommonControlsEx.
因为通用控件的数量非常多,把它们全部装入内存并注册它们是非常浪费内存的。除了“RTF文本编辑”控件外其他控件的可执行代码都放在comctl32.dll中,这样其他的应用程序就可以使用它们了。“RTF文本编辑”控件在 richedXX.dll中,由于该控件非常的复杂,所以也比其它控件大。 要加载comctl32.dll可以在您的应用程序中调用函数InitCommonControls。InitCommonControls函数是动态链接库comctl32.dll中的一个函数,只要在您的程序中的任意地方引用了该函数就、会使得WINDOWS的程序加载器PE Loader加载该库。函数InitCommonControls其实只有一条指令“ret”,它的唯一目的是为了使得在调用了个该函数的应用程序的可执行文件的PE头中的“引入”段中包含有comctl32.dll,这样无论什么时候该应用程序都会为您加载该库。所以真正初始化的工作是在该库的入口点处做的,在这里会注册所有的通用控件类,然后所有的通用控件就可以在这些类上进行创建,这就象创建其它的子窗口控件一样。
[解决办法]
不好意思,看错了。
最后加这一句就可以了:
- C/C++ code
fileI.close();//close the input streamreturn DefWindowProc (hwnd, message, wParam, lParam);}