win32下窗口程序的创建

来源:互联网 发布:淘宝商家换头像 编辑:程序博客网 时间:2024/04/29 10:34
 

消息的结构体

typedef struct tagMSG {

    HWND   hwnd;       //窗口的句柄

    UINT   message;         //具体消息

    WPARAM wParam;    //消息的附加参数

    LPARAM lParam;

    DWORD  time;

    POINT  pt;

} MSG, *PMSG;

 

Wndclass类的结构体

typedef struct _WNDCLASS {

    UINT       style;

    WNDPROC    lpfnWndProc;    //回调函数

    int        cbClsExtra;               

    int        cbWndExtra;

    HINSTANCE  hInstance;

    HICON      hIcon;

    HCURSOR    hCursor;

    HBRUSH     hbrBackground;

    LPCTSTR    lpszMenuName;

    LPCTSTR    lpszClassName;

} WNDCLASS, *PWNDCLASS;

Window程序的运行机制:主要是通过消息队列来完成。

 

我们运行一个窗口时,比如说鼠标按下,或键盘按下,操作系统就会捕获到这个消息,就调用postmessage函数将消息送到消息列队。应用程序调用getmessage函数从消息队列中取出消息,然后调用TranslateMessage函数对消息进行转化,将虚拟按键消息转化为字符消息,如果能转换,TranslateMessage函数就调用postmessage函数再将转换后的消息发送到消息列队中。下次执行getmessage时将其取出并返回。然后调用dispatchmessage函数获取的消息回传给操作系统。接下来操作系统就可以调用回掉函数进行响应(这里的回调函数是要自己写的)

       MSG msg;

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

       {

              TranslateMessage(&msg);

              DispatchMessage(&msg);     //一直进行循环取消息

       }

       return 0;

 

 

要创建一个windows下的出口要进行四步操作:

(1)要设计窗口的类

(2)要注册你创建的窗口类

(3)创建你注册的窗口类的窗口

(4)显示和更新你创建的出口

 

 

#include <windows.h>    //我们用的windows编程,所以要添加这个头文件

#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

);  

//WinMain函数,也就是主函数的入口点

int WINAPI WinMain(

  HINSTANCE hInstance,      // handle to current instance

  HINSTANCE hPrevInstance,  // handle to previous instance

  LPSTR lpCmdLine,          // command line

  int nCmdShow              // show state

)

{

       WNDCLASS wndcls;    //结构体wndclass,用来设计窗口类

       wndcls.cbClsExtra=0;

       wndcls.cbWndExtra=0;

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

       wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);

       wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);

       wndcls.hInstance=hInstance;

       wndcls.lpfnWndProc=WinSunProc;

       wndcls.lpszClassName="Weixin2003";

       wndcls.lpszMenuName=NULL;

       wndcls.style=CS_HREDRAW | CS_VREDRAW;

       RegisterClass(&wndcls);  //注册窗口类

 

       HWND hwnd;      //定义句柄,用来接收创建窗口返回的句柄。

       hwnd=CreateWindow("Weixin2003","北京维新科学技术培训中心",WS_OVERLAPPEDWINDOW,

              0,0,600,400,NULL,NULL,hInstance,NULL);

 

       ShowWindow(hwnd,SW_SHOWNORMAL);  //显示和更新窗口

       UpdateWindow(hwnd);

 

       MSG msg;       //开始了在消息列队去消息的循环

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

       {

              TranslateMessage(&msg);

              DispatchMessage(&msg);

       }

       return 0;

}

 

//具体实现回调函数

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",0);

              break;

       case WM_LBUTTONDOWN:

              MessageBox(hwnd,"mouse clicked","weixin",0);

              HDC hdc;

              hdc=GetDC(hwnd);

              TextOut(hdc,0,50,"计算机编程语言培训",strlen("计算机编程语言培训"));

              ReleaseDC(hwnd,hdc);

              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;

}

原创粉丝点击