zoukankan      html  css  js  c++  java
  • 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文件中实现。

  • 相关阅读:
    BZOJ 3205 [Apio2013]机器人 ——斯坦纳树
    BZOJ 3782 上学路线 ——动态规划 Lucas定理 中国剩余定理
    HDU 1423 Greatest Common Increasing Subsequence ——动态规划
    BZOJ 3309 DZY Loves Math ——莫比乌斯反演
    POJ 1038 Bugs Integrated, Inc. ——状压DP
    POJ 3693 Maximum repetition substring ——后缀数组
    POJ 2699 The Maximum Number of Strong Kings ——网络流
    POJ 2396 Budget ——有上下界的网络流
    BZOJ 4650 [Noi2016]优秀的拆分 ——后缀数组
    源码安装python
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3270948.html
Copyright © 2011-2022 走看看