在Win32 console平台下创建窗体应用程序

过程如下:

     (1)设计窗体类

     (2)注册窗体类

     (3)创建窗体

     (4)显示窗体和更新窗体

     (5)消息循环

     (6)窗口过程(又称回调函数)

代码如下:

#include <windows.h>

#include <stdio.h>

 

LRESULT CALLBACK WinSunPro(      //declare the function of WinSumPro

         HWNDhwnd,

         UINTuMsg,

         WPARAMwParam,

         LPARAMlParam

);

 

int WINAPI WinMain(

         HINSTANCEhInstance,      //handle to current instance

         HINSTANCEhPrevInstance,  //handle to previousinstance

         LPSTRlpCmdLine,               //command line

         intnCmdshow                      //show state

)

{

         WNDCLASSwndcls;         //1、desingedwindow class

         wndcls.cbClsExtra=0;

         wndcls.cbWndExtra=0;

         wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

         wndcls.hCursor=LoadCursor(NULL,IDC_HELP);

         wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);

         wndcls.hInstance=hInstance;

         wndcls.lpfnWndProc=WinSunPro;

         wndcls.lpszClassName="Sunlei2015";

         wndcls.lpszMenuName=NULL;

         wndcls.style=CS_HREDRAW|CS_VREDRAW;

 

         RegisterClass(&wndcls);     //2、register windowclass

 

         HWNDhwnd;                                        //3、createwindow

         hwnd=CreateWindow("Sunlei2015","feiyan",WS_OVERLAPPEDWINDOW,400,200,600,300,NULL,NULL,hInstance,NULL);

         ShowWindow(hwnd,SW_NORMAL);  //4、show window

         UpdateWindow(hwnd);                      //5、updatewindow

                  

         MSGmsg;                                              //6、messagecircumstance

         while(GetMessage(&msg,NULL,0,0))

         {

                   TranslateMessage(&msg);

                   DispatchMessage(&msg);   //将消息分配给窗口过程来处理

         }

         return0;

}

 

LRESULT CALLBACK WinSunPro(               //7、window process

         HWNDhwnd,

         UINTuMsg,

         WPARAMwParam,

         LPARAMlParam

)

{

         switch(uMsg)                              //handle themessage by message's type

         {

         caseWM_CHAR:  

                   charszChar[20];

                   sprintf(szChar,"charis %d",wParam);

                   MessageBox(hwnd,szChar,"Sunlei2015",0);

                   break;

         caseWM_LBUTTONDOWN:

                   MessageBox(hwnd,"mouseclick","Sunlei2015",0);

                   HDChdc;

                   hdc=GetDC(hwnd);

                   TextOut(hdc,0,50,"Mouseclick seccessfully!",strlen("Mouse click seccessfully!"));

                   ReleaseDC(hwnd,hdc);

                   break;

         caseWM_PAINT:

                   HDChDC;

                   PAINTSTRUCTps;

                   hDC=BeginPaint(hwnd,&ps);

                   TextOut(hDC,0,0,"C++programming ",strlen("C++ programming "));

                   EndPaint(hwnd,&ps);

                   break;

         caseWM_CLOSE:

                   if(IDYES==MessageBox(hwnd,"真的要关闭吗?","Sunlei2015",MB_YESNO))

                   {

                            DestroyWindow(hwnd);

                   }

                   break;

         caseWM_DESTROY:

                   PostQuitMessage(0);

                   break;

         default:

                   returnDefWindowProc(hwnd,uMsg,wParam,lParam);

         }

         return0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值