不要拿SetBkColor,SetBkMode设置整个窗口的背景色啦

不要拿SetBkColor,SetBkMode设置整个窗口的背景色啦!!
SetBkColor的作用范围
根据目前的经验,setBkColor只在两个地方好用: 
1.填充空隙(Gaps):此处空隙,指的是点式画笔或虚线画笔之间的 空隙。参见CreatePen。
空隙还可以指阴影画刷的空隙。参见,CreateHatchBrush
2.文本输出TextOut(),DrawText();
并且SetColor设置一次会对此次绘画的所有“填充空隙”“文本输出”都有效。
PS:
如果要填充背景,还是用画刷吧。FillRect()等函数吧。
代码:
case WM_CREATE:
	//m_brush = CreateSolidBrush(RGB(255, 255, 255)); // color white brush 
	m_brush = CreateHatchBrush (HS_BDIAGONAL,RGB (255, 255, 0)) ;
        return 0 ;
case WM_PAINT:
        hdc = BeginPaint (hwnd, &ps) ;       
        GetClientRect (hwnd, &rect) ;
        SetBkColor(hdc,0X0000FF);  
	//1.用m_brush填充矩形
	FillRect(hdc, &rect2,m_brush);
	//2.用虚线画笔画一条直线
	hPen= CreatePen(PS_DASH, 1, RGB(0, 255, 0));
	hOldPen = SelectObject(hdc,hPen);
        ptLeftTop.x = 10;
	ptLeftTop.y = 10;
        MoveToEx(hdc,ptLeftTop.x,ptLeftTop.y,NULL);
	ptLeftTop.x = 100;
	ptLeftTop.y = 100;
        LineTo(hdc,ptLeftTop.x,ptLeftTop.y);
        SelectObject(hdc,hOldPen);
        DeleteObject(hPen);
        //3.写文本
	DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
        DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
         
        EndPaint (hwnd, &ps) ;
        return 0 ;

效果:

以下摘抄自MSDN:

This function fills the gaps between styled lines (CreatePen的第一个参数PenStyle)drawn using a pen created by the CreatePen function; it does not fill the gaps between styled lines drawn using a pen created by theExtCreatePen function. TheSetBKColor function also sets the background colors for TextOut andExtTextOut.

If the background mode is OPAQUE, the background color is used to fill gaps between styled lines, gaps between hatched lines in brushes, and character cells. The background color is also used when converting bitmaps from color to monochrome and vice versa. 

The SetBkMode function affects the line styles for lines drawn using a pen created by theCreatePen function.SetBkMode does not affect lines drawn using a pen created by theExtCreatePen function.

The iBkMode parameter can also be set to driver-specific values. GDI passes such values to the device driver and otherwise ignores them.

reference:
《windows程序设计》5.3.7填充空隙


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值