Windows内部运行机制

来源:互联网 发布:河南鹤壁 网络诈骗 编辑:程序博客网 时间:2024/05/29 13:49

一:Windows是事件驱动方式的程序设计、

二:消息队列

三:Windows程序入口 WinMain函数

四:创建一个窗口要经过下面四个步骤

1。设计一个窗口类。WNDCLASS  wndclass

2。注册窗口类。RegisterClass(&wndclass)

3。创建窗口。CreateWindow()

4。显示及更新窗口。ShowWindow(),UpdateWindow()

销毁窗口用DestroyWindow();

一个小例子:

 

#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK WinMyProc(
 HWND hwnd,
 UINT uMsg,
 WPARAM wParam,
 LPARAM lParam
 );      //回调函数声明

int WINAPI WinMain(
 HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
 LPSTR lpCmdLine,
 int nShowCmd
 )
{
 WNDCLASS wndclass;  //定义一个窗口类变量,设计一个窗口类
 wndclass.cbClsExtra=0;
 wndclass.cbWndExtra=0;
 wndclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
 wndclass.hCursor=LoadCursor(NULL,IDC_CROSS);
 wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
 wndclass.hInstance=hInstance;
 wndclass.lpfnWndProc=WinMyProc;
 wndclass.lpszClassName="LIGHT";
 wndclass.lpszMenuName=NULL;
 wndclass.style=CS_HREDRAW | CS_VREDRAW;
 
 RegisterClass(&wndclass);  //注册窗口类

 HWND hwnd;
 hwnd=CreateWindow("LIGHT","星光",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 WinMyProc(     //窗口过程的回调函数
 HWND hwnd,
 UINT uMsg,
 WPARAM wParam,
 LPARAM lParam
 )
{
 switch(uMsg)
 {
  case WM_CHAR:    //字符消息
   char lgChar[20];
   sprintf(lgChar,"char is %d",wParam);
   MessageBox(hwnd,lgChar,"Light",0);
   break;
  case WM_LBUTTONDOWN:  //左键按下的消息
   MessageBox(hwnd,"Left Button Clicked!","LIGHT",0);
   HDC hdc;
   hdc=GetDC(hwnd);
   TextOut(hdc,0,60,"WINDOWS窗口设计",strlen("WINDOWS窗口设计"));
   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,"是否真的结束?","LIGHT",MB_YESNO))
   {
    DestroyWindow(hwnd);
   }
   break;
  case WM_DESTROY:   //销毁
   PostQuitMessage(0);
   break;
  default:
   return DefWindowProc(hwnd,uMsg,wParam,lParam);  //默认消息
 }
 return 0;
}

 

原创粉丝点击