您的位置:首页 > 其它

Windows程序内部运行原理

2014-11-01 17:01 309 查看
WinMain.cpp

#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,          // 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=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);  //触发WM_QUIT消息结束while(GetMessage(&msg,NULL,0,0))消息循环

  break;

 default:

  return DefWindowProc(hwnd,uMsg,wParam,lParam); //交由系统处理其他消息,若无此句则消息链中断可能产生异常

 }

 return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: