CreateWindow用法

#include <windows.h>

#include "stdafx.h"

#include  <stdio.h>     // sprintf(szChar,"char is %d",wParam)

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,          // command line

  int nCmdShow              // show state

)

{

       WNDCLASS wndcls;  // 定义窗口类

       wndcls.cbClsExtra=0; //以下初始化定义的窗口类

       wndcls.cbWndExtra=0;

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

       wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);

       wndcls.hIcon=LoadIcon(NULL,IDI_WINLOGO);

       wndcls.hInstance=hInstance;

       wndcls.lpfnWndProc=WinSunProc;

       wndcls.lpszClassName="Weixin2003";

       wndcls.lpszMenuName=NULL;

       wndcls.style=CS_HREDRAW | CS_VREDRAW;

       RegisterClass(&wndcls); // 注册初始化的窗口类

       HWND hwnd; //定义一个句柄,把生成的窗口句柄赋给hwnd;

       hwnd=CreateWindow("Weixin2003","This is my test ! ",WS_OVERLAPPEDWINDOW,

              0,0,600,400,NULL,NULL,hInstance,NULL); // 根据初始化的窗口类,创建该窗口。并把句柄赋给hwnd。

       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];

              memset(szChar,'\0',20); //初始化szChar[20];

              sprintf(szChar,"char is %d",wParam); // wParam 中存放的是按下的键盘的ASIC码值。Check  sprintf ?

              MessageBox(hwnd,szChar,"MSG标题",0);

              break;

       case WM_LBUTTONDOWN: //鼠标左键按下

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

              HDC hdc;

              hdc=GetDC(hwnd);

              TextOut(hdc,0,50,"This is my test ! ",strlen("This is my test ! "));

              ReleaseDC(hwnd,hdc);

              break;

       case WM_PAINT: //窗口重绘

              HDC hDC;

              PAINTSTRUCT ps;

              hDC=BeginPaint(hwnd,&ps);

              TextOut(hDC,200,200,"这里每次都会刷新",strlen("这里每次都会刷新"));

              EndPaint(hwnd,&ps);

              break;

       case WM_CLOSE: // 关闭窗口

                  // WM_DESTROY 是关闭程序的

                  // WM_CLOSE 是关闭窗口的

                  // WM_QUIT 是关闭消息环的

              if(IDYES==MessageBox(hwnd,"是否真的结束?","weixin",MB_YESNO))

              {

                     DestroyWindow(hwnd);

              }

              break;

       case WM_DESTROY: // 单击Close按钮

              PostQuitMessage(0);

              break;

       default:

              return DefWindowProc(hwnd,uMsg,wParam,lParam); // 其他的交给系统默认处理。 

       }

       return 0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值