回 帖 发 新 帖 刷新版面

主题:一个很简单的窗口程序,编译没错,可运行结果不对,请各位大侠帮帮我这个小菜鸟.

#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK WinsunProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);


int WINAPI WinMain(
  HINSTANCE hInstance,  // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,      // pointer to command line
  int nCmdShow          // show state of window
)
{
//    1.设计窗口( 1个类, 4个函数 )
    WNDCLASS    wndclass ;                                                //窗口类
    wndclass.cbClsExtra = 0 ;
    wndclass.cbWndExtra = 0 ;
    wndclass.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH ) ;    //背景函数
    wndclass.hCursor = LoadCursor( NULL , IDC_ARROW ) ;                    //光标函数            
    wndclass.hIcon = LoadIcon( NULL , IDI_ERROR ) ;                        //图标函数
    wndclass.hInstance = hInstance ;
    wndclass.lpfnWndProc = WinsunProc ;                                    //窗口过程函数
    wndclass.lpszClassName = "WeiXin2003" ;
    wndclass.lpszMenuName = NULL ;
    wndclass.style = CS_HREDRAW | CS_VREDRAW ;

//    2.注册窗口(1个函数)
    RegisterClass(&wndclass) ;                                            //类注册函数

//    3.创建窗口(1个类, 1个函数)                                            //窗口句柄
    HWND hwnd ;
    hwnd = CreateWindow ( "WeiXin2003" , "计算机编程" ,                //窗口创建函数
                        WS_OVERLAPPEDWINDOW , 0 , 0 , 600 , 400 ,
                        NULL , NULL , hInstance , NULL ) ;

//    4.显示窗口(2个函数)

    ShowWindow( hwnd , SW_SHOWNORMAL ) ;                                //窗口显示函数
    UpdateWindow( hwnd ) ;                                                //窗口更新函数

//    5.消息循环(1个类, 3个函数)
    MSG        msg ;                                                        //消息类
    while ( GetMessage( &msg , NULL , 0 , 0 ) );                        //消息说明函数
    {                                                        
        TranslateMessage( &msg ) ;                                        //消息转换函数            
        DispatchMessage( &msg ) ;                                        //消息发送函数
    }

    return 0 ;

}

回复列表 (共2个回复)

沙发

//定义窗口过程函数
LRESULT CALLBACK WinsunProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
     switch ( uMsg )
     {
         
     case WM_CHAR ://按下键盘字母
         char szChar[20] ;
         sprintf( szChar , " char is %d " , wParam ) ;
         MessageBox( hwnd , szChar , "weixin" , MB_OK );                    //消息弹出函数
         break ;
     case WM_LBUTTONDOWN ://按下鼠标左键                                                    
         MessageBox( hwnd , "mouse click " , "weixin" , MB_YESNOCANCEL ) ;    
         HDC hdc ;                                            //设备上下文句柄device context
         hdc = GetDC( hwnd ) ;                                //得到DC(配对,只能用在窗口重绘外)
         TextOut(hdc , 0 , 50 , "计算机编程" , strlen("计算机编程") ) ;
         ReleaseDC(hwnd , hdc ) ;                            //释放DC(配对,只能用在窗口重绘外)
         break ;
     case WM_PAINT ://窗口重绘
         HDC hDC ;
         PAINTSTRUCT ps ;
         hDC = BeginPaint( hwnd , &ps ) ;            //重绘开始函数(配对,只能用在窗口重绘里)
         TextOut( hDC , 0 , 0 , "计算机编程" , strlen("计算机编程") ) ;
         EndPaint(hwnd, &ps ) ;                        //重绘结束函数(配对,只能用在窗口重绘里)
         break ;
     case WM_CLOSE :
         if( IDYES == MessageBox( hwnd , "是否真的关闭" , "weixin" , MB_YESNO ) )
         {
             DestroyWindow( hwnd ) ;                //窗口销毁函数
         }
         break ;
     case WM_DESTROY :
         PostQuitMessage(0) ;                        //消息结束函数
         break ;
     default :
         return DefWindowProc( hwnd , uMsg , wParam , lParam ) ;        //窗口默认函数    
     }

     return 0 ;
}



运行结果无法修改,麻烦高人指点一下.

板凳

我找到错误了,不用劳驾各位大侠了,封贴,谢谢!

我来回复

您尚未登录,请登录后再回复。点此登录或注册