• MFC版的Hello World


    MFC版的Hello World


    使用MFC类库写个Hello示例程序比直接用Win32 API写要简单的多了。因为MFC用几个类封装了应用程序的创建,消息循环等等东东。

    闲话少说,先给来一个最简单的MFC版Hello World.


    //Hello.h
    
    #ifndef Hello1_H_
    #define Hello1_H_ 
    
    #include <afxwin.h>
    
    // Declare the application class
    class CTestApp : public CWinApp
    {
    public:
    	virtual BOOL InitInstance();
    };
    
    // Create an instance of the application class
    CTestApp TestApp;  
    
    #endif //Hello1_H_ 
    
    //Hello.cpp
    
    #include <afxwin.h>
    #include "Hello.h"
    
    
    // The InitInstance function is called
    // once when the application first executes
    BOOL CTestApp::InitInstance()
    {
    	MessageBox(0,"Hello world!", "信息", MB_ICONASTERISK);
    	
    	return FALSE;
    }
    


            这个是最简单的,它只用到了应用程序类(CWinApp),至于说它是怎么做到的?你去看一下CWinApp类的实现,我在这里只能简单的告诉你,应用程序类(CWinApp)实现了WinMain()函数的调用,消息循环等等。

    下面再来个稍微复杂点的Hello World,这次不用系统的消息框实现。这次加入了一个框架窗口类(CFrameWnd),并且在其内部创建了一个CStatic控件,用于显示"Hello World"字串。


    //static1.h
    #ifndef static1_H_
    #define static1_H_ 
    
    #include <afxwin.h>
    
    // Declare the application class
    class CTestApp : public CWinApp
    {
    public:
    	virtual BOOL InitInstance();
    };
    
    // Create an instance of the application class
    CTestApp TestApp;  
    
    // Declare the main window class
    class CTestWindow : public CFrameWnd
    {
    private:
    	CStatic* cs;
    public:
    	CTestWindow();
    	~CTestWindow();
    };
    
    #endif //static1_H_
    


    //static1.cpp
    
    #include "static1.h"
    
    // The InitInstance function is called
    // once when the application first executes
    BOOL CTestApp::InitInstance()
    {
    	m_pMainWnd = new CTestWindow();
    	m_pMainWnd->ShowWindow(m_nCmdShow);
    	m_pMainWnd->UpdateWindow();
    	return TRUE;
    }
    
    // The constructor for the window class
    CTestWindow::CTestWindow()
    { 
    	CRect r;
    
    	// Create the window itself
    	Create(NULL, 
    		"CStatic Tests", 
    		WS_OVERLAPPEDWINDOW,
    		CRect(0,0,100,100));
    	
    	// Get the size of the client rectangle
    	GetClientRect(&r);
    	r.InflateRect(-10,-10);
    	
    	// Create a static label
    	cs = new CStatic();
    	cs->Create("hello world",
    		WS_CHILD|WS_VISIBLE|WS_BORDER|SS_CENTER,
    		r,
    		this);
    }
    
    
    CTestWindow::~CTestWindow()
    {
    	delete cs; 
    }
    


              不知道你们注意到了没有?这一版的程序比上一版的消息对话框程序的InitInstance()函数中的返回值一个是True,一个是False。其实这一点区别,差异是很大的。有什么区别大家看一下AfxWinMain()函数的实现就明白了。对了,AfxWinMain()函数在VC安装目录下的WinMain.CPP文件中实现。

  • 相关阅读:
    PHP MVC运用
    17个方法防止dedeCMS织梦网站被黑挂木马
    少有人走的路
    你有选择的自由
    产品经理需要掌握哪些能力
    产品经理的工作流程
    Weave实现跨主机容器互联
    转载【docker】CMD ENTRYPOINT 的使用方法
    Docker:发布镜像问题denied: requested access to the resource is denied的解决方法
    Docker Dockerfile基本配置
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3270948.html
Copyright © 2020-2023  润新知