CSDN博客

img mrdone

又一个修改对话框背景颜色

发表于2008/9/28 10:50:00  733人阅读

分类: 资源文件(UI) VC/MFC

 
BOOL CTestKEYDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnEraseBkgnd(pDC);
HBITMAP h_bitMap;
h_bitMap = SHLoadImageFile(L"bg.gif");
if(h_bitMap == NULL)
{
return false;
}
BITMAP bm;
GetObject(&h_bitMap,sizeof(HBITMAP),&bm);
/*
为一个图片赋值。
*/
HDC hDc = CreateCompatibleDC(pDC->GetSafeHdc());
/*
内存中的DC是仅仅存储在内存中的。当一个DC被创建的时候,它的外观(显示模式)只是一个象素大小。所以,在应用程序使用DC之前,
要先将一个大小合适的图片用"CreateCompatiableBitmap"“选择”到内存中,这样DC的大小就是图片的大小。
当内存中的一个DC被创建的时候,所有的属性都是默认值 ,如果想把它当做一个普通的DC,使用者必须设置它的属性,获得当前的设置,
和当前选中的画图用的画笔、画刷,的区域的大小。
CreateCompatibleDC函数只能用于支持光栅操作的设备。应用程序是否支持光栅操作,可以用"GetDeviceCaps"函数来获得。
当DC使用完后,用DeleteDC去销毁这个DC。
*/
/*
A memory DC exists only in memory. When the memory DC is created, its display surface is exactly one monochrome pixel wide and one monochrome pixel high.
Before an application can use a memory DC for drawing operations, it must select a bitmap of the correct width and height into the DC. 
To select a bitmap into a DC, use the CreateCompatibleBitmap function, specifying the height, width, and color organization required. 
When a memory DC is created, all attributes are set to normal default values. The memory DC can be used as a normal DC.
You can set the attributes; obtain the current settings of its attributes; and select pens, brushes, and regions.
The CreateCompatibleDC function can only be used with devices that support raster operations. 
An application can determine whether a device supports these operations by calling the GetDeviceCaps function. 
When you no longer need the memory DC, call the DeleteDC function. 
*/
HBITMAP hOldBmp = NULL;
hOldBmp = (HBITMAP)SelectObject(hDc,&h_bitMap);
/*
保存原先的图片信息,以便恢复。(用完后,需要对系统的信息进行恢复)
*/
/*
This function returns the previously selected object of the specified type. 
An application should always replace a new object with the original, 
default object after it has finished drawing with the new object. 
*/
CRect rect;
GetClientRect(&rect);
/*
获取当前客户区的大小。
*/
StretchBlt(pDC->GetSafeHdc(),rect.left,rect.top,rect.Width(),rect.Height(),
hDc,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
/*
Draw the Picture .
*/
SelectObject(hDc,hOldBmp);
DeleteDC(hDc);
hDc = NULL;
if(h_bitMap != NULL)
{
DeleteObject(h_bitMap);
}

return TRUE;
}
0 0

相关博文

我的热门文章

img
取 消
img