【问题标题】:Simple MFC Application简单的 MFC 应用程序
【发布时间】:2016-04-12 02:31:12
【问题描述】:

我目前正在我的 C++ 课程中学习 MFC 和 GUI 基础知识,我正在尝试创建一个非常简单的 MFC 程序,它具有绿色背景,中间只有文本“Hello World”的窗户。不幸的是,对我来说,创建一个带有标题的普通窗口已经成为一项艰巨的任务。

我的问题是,我从哪里开始更改背景颜色并向窗口添加文本。我应该将它放在我的代码中的什么位置?

这是我所拥有的:

#include <afxwin.h>

class CMainFrame : public CFrameWnd
{
public:
    CMainFrame()
    {
        Create(NULL, _T("Windows App"));

    }
};
class CApp : public CWinApp
{
    CMainFrame *Frame;
    BOOL InitInstance()
    {
        Frame = new CMainFrame();
        m_pMainWnd = Frame;

        Frame->ShowWindow(SW_SHOW);
        Frame->UpdateWindow();


        return TRUE;
    }
};

CApp theApp;

【问题讨论】:

    标签: c++ mfc


    【解决方案1】:

    通常开始使用 Visual Studio 应用程序向导来实现 MFC 应用程序。它为您创建初始应用程序框架。这可以基于一个简单的对话框或 MFC 文档视图体系结构。然后设置背景颜色,例如在视图类的 OnDraw() 成员函数中。

    CMainframe 通常是包含文档窗口及其视图的主应用程序窗口。

    说了这么多,如果你想继续构建这个例子,你可以实现 OnPaint 消息处理程序:https://msdn.microsoft.com/en-us/library/01c9aaty.aspx 并在那里绘制。

    为此,您还需要在窗口https://msdn.microsoft.com/en-us/library/0x0cx6b1.aspx 中实现消息映射,然后添加 ON_WM_PAINT() 处理程序。应用程序向导还会为您添加消息映射和处理程序。

    【讨论】:

    • 应用向导有其自身的问题。 OP的代码更适合学习MFC。学习基本的 WinAPI 编程也很有用。
    • @BarmakShemirani:“学习基本的 WinAPI 编程也很有用。” - 不,没用。这是强制。
    【解决方案2】:

    要更改背景颜色并在窗口中设置文本,首先您必须处理 WM_PAINT 消息处理程序。如果您使用的是 Visual Studio IDE,则可以通过在 CMainFrame 类的类向导中选择消息处理程序 WM_PAINT 来完成,否则您可以直接在实现文件中编写处理程序代码。以下是您应用的代码:

    BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
        ON_WM_PAINT()
        ON_WM_SIZE()
    END_MESSAGE_MAP()
    void CMainFrame::OnPaint()
    {
        CString strText = _T("Hello World");    //Display String
        TEXTMETRIC textMetricObj; //Structure to get height and width of char
        CPaintDC dc(this);
        CRect clientRect;
        CBrush brush;
        dc.GetTextMetrics(&textMetricObj);  //Get char height and width info
        int nCharHeight = textMetricObj.tmHeight;
        int nStrWidth = strText.GetLength() * textMetricObj.tmMaxCharWidth; // get max string width
        brush.CreateSolidBrush(RGB(0,255,0));   //create green brush to paint background color to green
        GetClientRect(&clientRect); // get active client area of window
        dc.FillRect(clientRect,&brush); //fill client area with green color
        //find display string bounding rectangle as middle of client window area
        int top = int((clientRect.Height() - nCharHeight) / 2);
        int bottom = int((clientRect.Height() + nCharHeight) / 2);
        int left = int((clientRect.Width() - nStrWidth) / 2);
        int right = int((clientRect.Width() + nStrWidth) / 2);
        dc.DrawText(strText,CRect(left,top,right,bottom),DT_CENTER);    //draw text in specified rectangle
    }
    
    
    void CMainFrame::OnSize(UINT nType, int cx, int cy)
    {
        CFrameWnd::OnSize(nType, cx, cy);
        Invalidate();
    }
    

    我建议您阅读 David Kruglinski 的 Programming Microsoft Visual C++ 一书以了解 MFC 的基础知识。

    【讨论】:

      猜你喜欢
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多