正文

第一个win32程序的学习笔记2006-11-18 12:36:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/12567/20447.html

分享到:

#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
);
//wParam lParam 为消息的附加信息,均为整型。例如按下A键,附加信息为A的ASCII码


//主函数
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(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="hehe";
 //菜单名
 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;

/*BOOL GetMessage(
  LPMSG lpMsg,         // address of structure with message
  HWND hWnd,           // handle of window
  UINT wMsgFilterMin,  // first message
  UINT wMsgFilterMax   // last message
  第三个和第四个参数设置为零,则返回所有消息。
  第二个参数为空,为可以得到所有窗口的信息。
  If the function retrieves the WM_QUIT message, the return value is zero.
);*/

/*------------消息循环-----------*/
 while(GetMessage(&msg,NULL,0,0))
 {
  TranslateMessage(&msg);
  //把WM_KEYUP WM_KEYDOWN消息转换为WM_CHAR消息,并放入队列中。
  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
)//窗口处理过程,函数名可以修改,LRESULT为long类型
{
 switch(uMsg)
 {
 case WM_CHAR:
  char szChar[20];
  sprintf(szChar,"char is %d",wParam);
  //格式化字符串到字符数组中,wParam里面存放的是ASCII码
  MessageBox(hwnd,szChar,"weixin",0);
        /*
        int MessageBox(
        HWND hWnd,          // handle of owner window
        LPCTSTR lpText,     // address of text in message box
        LPCTSTR lpCaption,  // address of title of message box
        UINT uType          // style of message box
        0为MB_OK
        );
        */
  break;
 case WM_LBUTTONDOWN:
  MessageBox(hwnd,"mouse clicked","weixin",0);
  HDC hdc;
  //DC设备上下文
  hdc=GetDC(hwnd);//设定与哪个窗口相关
  TextOut(hdc,0,50,"计算机编程语言培训",strlen("计算机编程语言培训"));
  /*
  BOOL TextOut(
        HDC hdc,           // handle to device context
        int nXStart,       // x-coordinate of starting position
        int nYStart,       // y-coordinate of starting position
        LPCTSTR lpString,  // pointer to string
        int cbString       // number of characters in string
        );
        */
  ReleaseDC(hwnd,hdc);//释放资源
  break;
 case WM_PAINT://重绘
  HDC hDC;
  PAINTSTRUCT ps;
  hDC=BeginPaint(hwnd,&ps);
  TextOut(hDC,0,0,"维新培训",strlen("维新培训"));
  EndPaint(hwnd,&ps);//释放DC
  //BeginPaint EndPaint只能用在WM_PAINT消息中使用
  break;
 case WM_CLOSE:
  if(IDYES==MessageBox(hwnd,"是否真的结束?","weixin",MB_YESNO))
  //“是”按纽选中
  {
   DestroyWindow(hwnd);//发送WM_DESTROY消息
  }
  break;
 case WM_DESTROY://窗口已销毁
  PostQuitMessage(0);//发送WM_QUIT消息
  break;
 default:
  return DefWindowProc(hwnd,uMsg,wParam,lParam);//缺省处理
 }
 return 0;
}

//LPCTSTR为常量字符串
/*typedef struct tagMSG {     // msg
    HWND   hwnd;    
    UINT   message;
    WPARAM wParam;
    LPARAM lParam;
    DWORD  time;
    POINT  pt;
} MSG;
窗口处理函数与消息结构体中变量一致*/

阅读(1879) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册