阅读:1272回复:2
程序调试
从书上得到的程序,调不通!帮忙看一下?
头文件: //simqwin.h LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM); BOOL InitApplication(HINSTANCE); BOOL InitInstance(HINSTANCE,int); char *hello=\"窗口外的世界很精彩,窗口外的天地也很奇妙\"; 源文件: //simpwin.cpp #include <windows.h> #include <string.h> #include \"simpwin.h\" HINSTANCE hInst; HWND hWndMain; //函数WinMain(HINSTANCE,HINSTANCE,LPSTR,int) //作用:调用初始化函数,处理消息循环 int APIENTRY WinMain( HINSTANCE hInstance,//当前实例句柄 HINSTANCE hPrevInstance,//前一个实例句柄 LPSTR lpCmdLine,//命令行字符串 int nCmdShow//窗口显示方式 ) { MSG msg; //初始化窗口数据,并注册窗口类 if(!InitApplication(hInstance)) return(FALSE); //创建和显示窗口,对应用程序进行必要的初始化。 if(!InitInstance(hInstance,nCmdShow)) return(FALSE); //进入消息循环;从应用程序消息队列中检取消息; //当检取的消息是一条WM_QUIT消息时,就退出消息循环。 while(GetMessage(&msg,NULL,0,0)) { //把虚拟键消息翻译为字符消息。 TranslateMessage(&msg); //把消息分配到相应的窗口过程中。 DispatchMessage(&msg); } return(msg.wParam); } //函数InitApplication(HINSTANCE) //用途:初始化窗口数据,并注册窗口类。 BOOL InitApplication( HINSTANCE hInstance//当前实例句柄。 ) { WNDCLASS wcSimpwin; //填写窗口类结构,使得其参数描述主窗口各方面的属性。 wcSimpwin.style =0; wcSimpwin.lpfnWndProc =(WNDPROC) MainWndProc; wcSimpwin.cbClsExtra =0; wcSimpwin.cbWndExtra =0; wcSimpwin.hInstance =hInstance; wcSimpwin.hIcon =LoadIcon(NULL,IDI_APPLICATION); wcSimpwin.hCursor =LoadCursor(NULL,IDC_ARROW); wcSimpwin.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH); wcSimpwin.lpszMenuName =NULL; wcSimpwin.lpszClassName =\"SimpwinWCladd\"; //对窗口类进行注册 return(RegisterClass(&wcSimpwin)); } //函数InitInstance(HINSTANCE,int) //用途:保存实例句柄,并创建主窗口。 BOOL InitInstance( HINSTANCE hInstance,//实例句柄 int nCmdShow//窗口显示方式 ) { hInst=hInstance; hWndMain=CreateWindow( \"SimpwinWClass\", \"我的窗口\", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL); //如果创建窗口失败,则返回FALSE if(!hWndMain) return(FALSE); //让窗口显示出来;并更新其客户区;最后返回TURE ShowWindow(hWndMain,nCmdShow); UpdateWindow(hWndMain); return(TRUE); } //MainWndProc(HWND,UINT,WPARAM,LPARAM) //用途:处理主窗口消息 LRESULT CALLBACK MainWndProc( HWND hWnd,//窗口句柄 UINT message,//消息类型 WPARAM wParam,//消息附带信息 LPARAM lParam//消息附带信息 ) { HDC hdc; PAINTSTRUCT ps; switch(message) { case WM_PAINT:hdc=BeginPaint(hWnd,&ps); TextOut(hdc,20,10,hello,lstrlen(hello)); EndPaint(hWnd,&ps); break; case WM_DESTROY://消息:本窗口正将被销毁。 //请求退出窗口和应用程序 PostQuitMessage(0); break; default: //调用默认窗口过程对未处理的消息进行必要的处理 return(DefWindowProc(hWnd,message,wParam,lParam)); } return(0); } |
|
沙发#
发布于:2002-03-21 11:56
你看看WINDOWS编程这本书吧,上面讲的就是使用SDK编程。
给你一个例子 /*------------------------------------------------------------ HELLOWIN.C -- Displays \"Hello, Windows 98!\" in client area (c) Charles Petzold, 1998 ------------------------------------------------------------*/ #include <windows.h> LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT (\"HelloWin\") ; 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 (\"This program requires Windows NT!\"), szAppName, MB_ICONERROR) ; return 0 ; } hwnd = CreateWindow (szAppName, // window class name TEXT (\"The Hello Program\"), // window caption WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position CW_USEDEFAULT, // initial x size CW_USEDEFAULT, // initial y size NULL, // parent window handle NULL, // window menu handle hInstance, // program instance handle NULL) ; // creation parameters ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc ; PAINTSTRUCT ps ; RECT rect ; 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) ; DrawText (hdc, TEXT (\"Hello, Windows 98!\"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ; EndPaint (hwnd, &ps) ; return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; } [编辑 - 3/21/02 作者: guardee] |
|
板凳#
发布于:2002-03-21 15:00
是调不通还是编译不过?如果是编译不过则是编译器或编译选项的设置问题,也有可能是源程序中有语法错误。
|
|
|