在mfc中绘制背景 [英] paint background in mfc
问题描述
我正在尝试使用MFC库在c ++中绘制窗口的背景.我必须使用此框架,因为我正在开发MFC应用程序.我尝试了几种不同的方法,但无法使其正常工作.因此,我最近打开了一个空白项目,只想弄清楚如何绘制背景,但无法正常工作.任何帮助将是巨大的.这是我的代码...
I am experimenting with painting the background of a window in c++ using the MFC library. It is mandated that i use this framework because I am working on an MFC application. I have tried several different methods but cannot get it to work. So i recently opened a blank project and just want to figure out how to paint the background but it is not working. Any help would be great. Here is my code...
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;
}
};
推荐答案
来自我自己的帖子 https://stackoverflow.com/a /22875542/383779 ,我可以保证我已经完成了这项工作.我使用这种方法在商业应用程序上实现主题/皮肤.
From my own post https://stackoverflow.com/a/22875542/383779 , I can guarantee I had made that work. I used that approach for implementing themes/skins on a commercial application.
您需要将OnCtlColor方法添加到Basic_Window类. 在您的.h文件中,添加到Basic_Window类:
You need to add a OnCtlColor method to your Basic_Window class. In your .h file, add to the Basic_Window class:
const CBrush m_BackgroundBrush;
和
afx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor);
在.cpp文件中,构造函数将初始化新变量
In .cpp file the constructor will initialize the new variable
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
参数.
some_exception
here means a situation where you will want the default behavior, instead of your own painting. Maybe it is a certain type of control, and for that exists the nCtlColor
parameter.
别忘了在邮件地图中添加ON_WM_CTLCOLOR()
.
Do not forget to add ON_WM_CTLCOLOR()
to your message map.
这篇关于在mfc中绘制背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!