1、windows程序运行原理

来源:互联网 发布:百战天虫java下载 编辑:程序博客网 时间:2024/05/22 16:38

1、Window程序设计是基于消息的事件驱动的程序设计模式。

typedef struct tagMSG {
    HWND        hwnd;                       //窗口句柄
    UINT        message;      // 消息标识符
    WPARAM      wParam;
    LPARAM      lParam;
    DWORD       time;       //时间
    POINT       pt;     //鼠标位置
#ifdef _MAC
    DWORD       lPrivate;
#endif
} MSG, *PMSG, NEAR *NPMSG, FAR *LPMSG;

创立窗口后,程序会循环检测消息队列中是否有消息,然后再解析 响应各种消息。

2、创建一个Win32程序步骤:

(1)WinMain函数 

int WINAPI WinMain(

HINSTANCE hInstance//

HINSTANCE hPrevInstance//

LPSTR lpcmdLine//

int nCmdShow)

(2)创建一个窗口:

1、设计窗口类WNDCLASS wndclass

typedef struct _WNDCLASS {
    UINT    style;        //窗口的类型  
    WNDPROC lpfnWndProc;  //窗口过程函数指针(回调函数)
    int     cbClsExtra; //窗口类附加字节,为该类窗口所共享。通常0。
    int     cbWndExtra; //窗口附加字节。通常设为0。
    HANDLE  hInstance;  //当前应用程序事例句柄。
    HICON   hIcon;      //图标句柄 LoadIcon();
    HCURSOR hCursor;    //光标句柄 LoadCursor();
    HBRUSH  hbrBackground; //画刷句柄 (HBRUSH)GetStockObject();
    LPCTSTR lpszMenuName;  //菜单名字
    LPCTSTR lpszClassName; //类的名字  在创建时用到
} WNDCLASS;

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

3、创建窗口      CreateWindow   CreateWindowEX

HWND CreateWindow(
  LPCTSTR lpClassName,  // pointer to registered class name
  LPCTSTR lpWindowName, // pointer to window name
  DWORD dwStyle,        // window style
  int x,                // horizontal position of window
  int y,                // vertical position of window
  int nWidth,           // window width
  int nHeight,          // window height
  HWND hWndParent,      // handle to parent or owner window
  HMENU hMenu,          // handle to menu or child-window identifier
  HANDLE hInstance,     // handle to application instance
  LPVOID lpParam        // pointer to window-creation data
);

4、显示窗口      showwindow

(3)消息循环

(4)窗口过程函数(设计窗口类中作为参数传入)






0 0