首先看示例程序:
注意此函数的remark:
HBITMAP
hBmp
=
NULL
;
hBmp
= ::
SHLoadDIBitmap
(
filePath
);
if
(!
hBmp
)
{
PQMsgBox
(
L
"Load Message Dialog Bitmap Fail!" );
LogToFile
(
L
"CMessageDlg::OnInitDialog
:
Load Message Dialog Bitmap Fail!");
return
;
}
CBitmap
fillBmp
;
fillBmp
.
Attach
(
hBmp
);
CBrush
fillBrush
;
fillBrush
.
CreatePatternBrush
(&
fillBmp
);
CBrush
*
oldBrush
=
memDC
->
SelectObject
(&
fillBrush
);
CPen
fillPen
(
PS_NULL
, 1,
RGB
( 0,0 ,0));
CPen
*
oldPen
=
memDC
->
SelectObject
(&
fillPen
);
memDC
->
Rectangle
(
rect
);
这样子画出来的图片,可能会出现交错现象,为什么呢?由于dc在画刷默认的位置从(0,0)开始的,所以填充到指定位置的时候就可能出现交错。好在微软提供了如下函数解决此问题:
CDC::SetBrushOrg |
必须在selectobject之前调用,即选入brush之前调用它,否则无效。
修改后如下:
HBITMAP
hBmp
=
NULL
;
hBmp
= ::
SHLoadDIBitmap
(
filePath
);
if
(!
hBmp
)
{
PQMsgBox
(
L
"Load Message Dialog Bitmap Fail!" );
LogToFile
(
L
"CMessageDlg::OnInitDialog
:
Load Message Dialog Bitmap Fail!");
return
;
}
CBitmap
fillBmp
;
fillBmp
.
Attach
(
hBmp
);
CBrush
fillBrush
;
fillBrush
.
CreatePatternBrush
(&
fillBmp
);
memDC ->SetBrushOrg ( screenPoint); // 此句目的防止画刷交替
CBrush
*
oldBrush
=
memDC
->
SelectObject
(&
fillBrush
);
CPen
fillPen
(
PS_NULL
, 1,
RGB
( 0,0 ,0));
CPen
*
oldPen
=
memDC
->
SelectObject
(&
fillPen
);
memDC
->
Rectangle
(
rect
);