MFC中C++类如何封装API

本文介绍C++类如何与API函数和窗口关联。通过CWinApp类中的CFrameWnd指针关联窗口,利用窗口句柄hWnd实现C++类与窗口的双向映射。CFrameWnd类管理窗口hWnd并提供接口调用API。
class CFrameWnd
{
public:
  HWND hWnd;

public:
  int RegisterWindow();
  void Create(LPCTSTR lpClassName, LPCTSTR lpWindowName);
  void ShowWindow(int nCmdShow);
  void UpdateWindow();

};

class CWinApp
{
public:
  CFrameWnd* m_pMainWnd;
public:
  BOOL InitInstance(int nCmdShow);
  int Run();
  ~CWinApp();
};
      C++类如何和API函数和窗口关联。   CWinApp中封装了一个CFrameWnd类型指针,通过这个指针可以指向一个CFrameWnd实体,而CFrameWnd实体中又封装了窗口句柄hWnd,并且提供了一些供用户使用的接口,这些接口中其实都是调用了系统提供的API函数。

      CFramWnd并不是窗口本身,而是一个C++类,该类中保存了窗口句柄hWnd,这就将该C++类和窗口关联起来了,纽带就是这个窗口句柄hWnd.简单的说,可以将CFrameWnd类看做管理窗口hWnd的对象,这是一个双射关系。MFC中的C++类都要与某一个资源(如窗口,位图等...)相关联,关联方式可以通过窗口句柄,位图的ID等...


使用C++代码封装的win32操作, 与MFC相似,对于学习SDK与C++是巨好的参考 Tutorials Menu of tutorials Tutorial 1: The Simplest Window Tutorial 2: Using Classes and Inheritance Tutorial 3: Using Messages to Create a Scribble Window Tutorial 4: Repainting the Window Tutorial 5: Wrapping a Frame around our Scribble Window Tutorial 6: Customising Window Creation Tutorial 7: Customising the Toolbar Tutorial 8: Loading and Saving Files Tutorial 9: Printing Tutorial 10: Finishing Touches Tutorial 1: The Simplest Window The following code uses Win32++ to create a window. This is all the code you need (in combination with Win32++) to create and display a simple window. Note that in order to add the Win32++ code to our program, we use an #include statement as shown below. #include "../Win32++/Wincore.h" INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPTSTR, int) { //Start Win32++ CWinApp MyApp; //Create a CWnd object CWnd MyWindow; //Create (and display) the window MyWindow.Create(); //Run the application return MyApp.Run(); } This program has four key steps: Start Win32++. We do this here by creating a CWinApp object called MyApp. Create a CWnd object called MyWindow. Create a default window by calling the Create function. Start the message loop, by calling the Run function. If you compile and run this program, you'll find that the application doesn't end when the window is closed. This is behaviour is normal. An illustration of how to use messages to control the windows behaviour (including closing the application) will be left until tutorial 3.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值