简单MFC应用 现在我正在学习我的C++类中的MFC和GUI基础知识,我正在尝试创建一个简单的MFC程序,它有一个绿色背景,在窗口的中间只需有一个文本“hello World”。不幸的是,对于我来说,创建一个带有标题的普通jane窗口是一件困难的事情

简单MFC应用 现在我正在学习我的C++类中的MFC和GUI基础知识,我正在尝试创建一个简单的MFC程序,它有一个绿色背景,在窗口的中间只需有一个文本“hello World”。不幸的是,对于我来说,创建一个带有标题的普通jane窗口是一件困难的事情,c++,mfc,C++,Mfc,我的问题是,我从哪里开始更改背景颜色并向窗口添加文本。我应该把它放在我的代码中的什么地方 以下是我所拥有的: #include <afxwin.h> class CMainFrame : public CFrameWnd { public: CMainFrame() { Create(NULL, _T("Windows App")); } }; class CApp : public CWinApp { CMainFrame *Fr

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

以下是我所拥有的:

#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;
#包括
类别名称:公共框架
{
公众:
CMainFrame()
{
创建(空,_T(“Windows应用程序”);
}
};
类别CApp:公共CWinApp
{
CMainFrame*帧;
BOOL InitInstance()
{
Frame=新的CMainFrame();
m_pMainWnd=帧;
框架->显示窗口(SW_显示);
框架->更新窗口();
返回TRUE;
}
};
CApp-theApp;

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

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

综上所述,如果您想继续构建这个示例,您可以实现OnPaint消息处理程序:并在其中绘制


为此,还需要在窗口中实现消息映射,然后添加\u WM\u PAINT()处理程序。应用程序向导还为您添加消息映射和处理程序。

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

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

综上所述,如果您想继续构建这个示例,您可以实现OnPaint消息处理程序:并在其中绘制


为此,还需要在窗口中实现消息映射,然后添加\u WM\u PAINT()处理程序。应用程序向导还为您添加消息映射和处理程序。

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

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的Visual C++编程,了解MFC的基本知识。< /P> < P>更改背景颜色并在窗口中设置文本,首先必须处理WMXFIX消息处理程序。如果您使用的是Visual studio IDE,那么可以通过为CMInframe类选择消息处理程序WM_在类中绘制向导来完成,否则您可以直接在实现文件中编写处理程序代码。以下是您的应用程序的代码:

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的Visual C++编程来理解MFC的基本知识。

应用程序向导有其自身的问题。OP的代码更适合学习MFC。学习基本的WinAPI编程也很有用。@BarmakShemirani:“学习基本的WinAPI编程也很有用。”-不,没用。这是必需的。应用程序向导有自己的问题。OP的代码更适合学习MFC。学习基本的WinAPI编程也很有用。@BarmakShemirani:“学习基本的WinAPI编程也很有用。”-不,没用。这是强制性的。