CFrameWnd派生类中调用CDC::SetBkColor、CDC::SetDCBrushColor无效

LandyTan 2019-07-27 03:58:36
新人请教:
SetBkColor函数返回值为0xFFFFFFFF,MSDN提示出错返回值为0x8000000. GetLastError返回0.
证明我SetBkColor调用应该是没有问题的。既然没有调用没有问题那么为什么背景色还是默认的呢?
搜索别人的帖子看到有说视图下的SetBkColor函数是无效的,真的吗?如果是真的,那么视图应该调用哪个function改变背景颜色呢?
SetBrushColor也无法改变任何颜色。

代码如下:
// .h
class CWindow :
public CFrameWnd
{
public:
CWindow(__in LPCTSTR pszWindowName);
~CWindow();

protected:
afx_msg virtual void OnPaint();

DECLARE_MESSAGE_MAP()
public:

};

//.cpp
CWindow::CWindow(__in LPCTSTR pszWindowName)
{
Create(NULL, pszWindowName);
}


CWindow::~CWindow()
{
}

BEGIN_MESSAGE_MAP(CWindow, CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()

void CWindow::OnPaint()
{
CRect rect;
GetClientRect(&rect);

//CDC* dc = GetDC();
CClientDC dc(this);
dc.SetDCBrushColor(RGB(155,205,155));
dc.SetBkColor(RGB(155, 205, 155));
dc.MoveTo(rect.left, rect.top);
dc.LineTo(rect.right, rect.bottom);
dc.MoveTo(rect.right, rect.top);
dc.LineTo(rect.left, rect.bottom);
//ReleaseDC(dc);
}
...全文
309 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
schlafenhamster 2019-08-11
  • 打赏
  • 举报
回复
BOOL CxxxxDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
RECT rc;
GetClientRect(&rc);
HDC hdc=::GetDC(this->m_hWnd);
::SetDCBrushColor(hdc,RGB(0,255,0));
FillRect(hdc,&rc,(HBRUSH)GetStockObject(DC_BRUSH));
return TRUE;
//
// return CDialog::OnEraseBkgnd(pDC);
}
LandyTan 2019-08-10
  • 打赏
  • 举报
回复
引用 9 楼 Eleven 的回复:
你是想要改变框架窗口的背景色?WM_ERASEBKGND ??
我想改变客户区背景颜色
Eleven 2019-07-29
  • 打赏
  • 举报
回复
你是想要改变框架窗口的背景色?WM_ERASEBKGND ??
schlafenhamster 2019-07-28
  • 打赏
  • 举报
回复
:SetBkColor 应该这样用
Sets the current background color to the specified color. If the background mode is OPAQUE, the system uses the background color to fill the gaps in styled lines, the gaps between hatched lines in brushes, and the background in character cells. The system also uses the background color when converting bitmaps between color and monochrome device contexts.
schlafenhamster 2019-07-28
  • 打赏
  • 举报
回复

// Ellipse(hdc,x-100,y-100,x+100,y+100);
FillRect(hdc,&rect,(HBRUSH)GetStockObject(DC_BRUSH));
schlafenhamster 2019-07-28
  • 打赏
  • 举报
回复
"我要设置整个客户区的背景颜色" 用 SetDCBrushColor 后 fill 客户区
LandyTan 2019-07-27
  • 打赏
  • 举报
回复
引用 2 楼 schlafenhamster 的回复:
1 。class CMainFrame : public CFrameWnd 2 . LineTo 使用pen Draws a line from the current position up to, but not including, the point specified by x and y (or point). The line is drawn with the selected pen.
CDC::SetDCPenColor只能设置线条的颜色。 MSDN说CDC::SetBkColor可以设置客户区背景颜色,但是我调用了没有反应。
LandyTan 2019-07-27
  • 打赏
  • 举报
回复
引用 3 楼 schlafenhamster 的回复:
正确代码是 #define _WIN32_WINNT 0x0500 //

	RECT rect;
	GetClientRect(&rect);
    HDC hdc=::GetDC(this->m_hWnd);
    ::SelectObject(hdc,GetStockObject(DC_PEN));
// Solid pen color. The default color is white. 
    ::SelectObject(hdc,GetStockObject(DC_BRUSH));
//Solid color brush. The default color is white.
    SetDCPenColor(hdc,RGB(255,0,0));
    SetDCBrushColor(hdc,RGB(255,123,123));
    int x,y;
    x=y=200;
    Ellipse(hdc,x-100,y-100,x+100,y+100);
按你说的确实可以设置Ellipse的颜色。那么我要设置整个客户区的背景颜色呢?GetStockObject没有DC_BK。
schlafenhamster 2019-07-27
  • 打赏
  • 举报
回复
正确代码是
#define _WIN32_WINNT 0x0500
//

RECT rect;
GetClientRect(&rect);
HDC hdc=::GetDC(this->m_hWnd);
::SelectObject(hdc,GetStockObject(DC_PEN));
// Solid pen color. The default color is white.
::SelectObject(hdc,GetStockObject(DC_BRUSH));
//Solid color brush. The default color is white.
SetDCPenColor(hdc,RGB(255,0,0));
SetDCBrushColor(hdc,RGB(255,123,123));
int x,y;
x=y=200;
Ellipse(hdc,x-100,y-100,x+100,y+100);
schlafenhamster 2019-07-27
  • 打赏
  • 举报
回复
1 。class CMainFrame : public CFrameWnd
2 . LineTo 使用pen
Draws a line from the current position up to, but not including, the point specified by x and y (or point). The line is drawn with the selected pen.
zgl7903 2019-07-27
  • 打赏
  • 举报
回复
SDI MID , 修改客户区颜色 应该在 View 类中处理, 非客户区在WM_ERASEBKGND 和 WM_NCPAINT 消息中处理

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧