VC++ 第三课

来源:互联网 发布:淘宝88元xbox360手柄 编辑:程序博客网 时间:2024/06/11 01:35

讲述MFC AppWizard的原理与MFC程序框架的剖析。

AppWizard是一个源代码生成工具,是计算机辅助程序设计工具,WinMain在MFC程序中是如何从源程序中被隐藏的,theApp全局变量是如何被分配的,MFC框架中的几个类的作用与相互关系,MFC框架窗口是如何产生和销毁的,对窗口类的PreCreateWidow和OnCreate两个函数的着重分析,Windows窗口与C++中的CWnd类的关系。

 

在许多函数中转来转去的,也是似懂非懂的~

 

#include "iostream"
#include "windows.h"
class CWnd
{
public:
 BOOL CreateEx(DWORD dwExStyle,      // extended window style
    LPCTSTR lpClassName,  // registered class name
    LPCTSTR lpWindowName, // 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,          // menu handle or child identifier
    HINSTANCE hInstance,  // handle to application instance
    LPVOID lpParam);        // window-creation data
 BOOL ShowWindow(int nCmdShow);
 BOOL UpdateWindow();
public:
 HWND m_hWnd;//句柄
};

BOOL CWnd::CreateEx(DWORD dwExStyle,      // extended window style
    LPCTSTR lpClassName,  // registered class name
    LPCTSTR lpWindowName, // 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,          // menu handle or child identifier
    HINSTANCE hInstance,  // handle to application instance
    LPVOID lpParam)        // window-creation data
{//调用内部函数,完成创建
 m_hWnd=::CreateWindowEx(dwExStyle,lpClassName,dwStyle,x,y,
     nWidth,nHeight,hWndParent,hMenu,hInstance,
     lpParam);//保存所创建的窗口
 if(m_hWnd!=NULL)
  return TRUE;
 else
  return FALSE;
}

BOOL CWnd::ShowWindow(int nCmdShow)
{
 return ::ShowWindow(m_hWnd,nCmdShow);//全局函数 win32 api函数
 
 
}

BOOL CWnd::UpdateWindow()
{
 return ::UpdateWindow(m_hWnd);
}

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;
 
 RegisterClass(&wndcls);

 CWnd wnd;//CWnd的成员变量中有句柄,不需再重新定义,也不需传递。。
 //wnd不仅仅是一个窗口,窗口销毁时,对象并未销毁,他们之间联系的纽带是变量句柄;但是对象销毁时,窗口必定销毁
 //
 wnd.CreateEx(...);
 wnd.ShowWindow(SW_SHOWNORMAL);
 wnd.UpdateWindow();

 //显示、更新完窗口后 消息循环
 /*
 HWND hwnd;
 hwnd=CreateWindowEx();
 ::ShowWindow(hwnd,SW_SHOWNORMAL);
 ::UpdateWindow(hwnd);
 */
 ......
}