gpt4 book ai didi

c++ - 在mfc中绘制背景

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:19:19 29 4
gpt4 key购买 nike

我正在尝试使用 MFC 库在 C++ 中绘制窗口的背景。我必须使用这个框架,因为我正在处理 MFC 应用程序。我尝试了几种不同的方法,但无法正常工作。所以我最近打开了一个空白项目,只想弄清楚如何绘制背景,但它不起作用。任何帮助都会很棒。这是我的代码...

class CExerciseApp : public CWinApp
{
//a pointer to our window class object
Basic_Window *bwnd;

BOOL InitInstance()
{
bwnd = new Basic_Window();
m_pMainWnd = bwnd;
bwnd->ShowWindow(1);

HWND hWnd = GetActiveWindow();

CRect drawing_area;
GetClientRect(hWnd, &drawing_area);

CBrush newBrush;
newBrush.CreateSolidBrush(RGB(255,255,255));

CDC* dc = bwnd->GetDC();
dc->FillRect(&drawing_area, &newBrush);
bwnd->RedrawWindow();
return TRUE;
}
};

最佳答案

来 self 自己的帖子 https://stackoverflow.com/a/22875542/383779 ,我可以保证我已经完成了这项工作。我使用这种方法在商业应用程序上实现主题/皮肤。

您需要将 OnCtlColor 方法添加到 Basic_Window 类。在您的 .h 文件中,添加到 Basic_Window 类:

const CBrush m_BackgroundBrush;

afx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor);

在 .cpp 文件中,构造函数将初始化新变量

Basic_Window::Basic_Window()
: m_BackgroundBrush(RGB(255,255,255))
{
//...
}

并实现

HBRUSH Basic_Window::OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
if(some_exception)
return __super::OnCtlColor( pDC, pWnd, nCtlColor);

return (HBRUSH) m_BackgroundBrush.GetSafeHandle();
}

some_exception 在这里表示您需要默认行为而不是您自己的绘画的情况。也许它是某种类型的控件,为此存在 nCtlColor 参数。

不要忘记将 ON_WM_CTLCOLOR() 添加到您的消息映射。

关于c++ - 在mfc中绘制背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28859754/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com