基本原理:
获取窗体区域,指定一个颜色为透明色,遍历位图,将图上所有该色区域从窗体区域中去除。这样最后就得到了一个异形窗体。
然后把背景图绘制在该窗体上。注意绘制的时候,作为透明的部分依然会被以原色绘制。但是由于绘制的地方并没有窗体,故而是显示不出来的。
在对话框的OnInitDialog()函数中加入以下代码:
CDC* pDC = GetDC();//获得设备上下文
CDC memDC;
CBitmap bitmap;//声明位图对象
CBitmap* bmp = NULL;
COLORREF currentColor;//当前像素颜色
COLORREF transparentColor = RGB(255, 255, 255);//定义透明颜色
CRect rc;
int x, y;
CRgn mainRgn, currentRgn;
GetWindowRect(&rc);//获得窗体区域
bitmap.LoadBitmap(IDB_BITMAP1);//装载模板位图
memDC.CreateCompatibleDC(pDC);//创建与内存兼容的设备上下文
bmp = memDC.SelectObject(&bitmap);
mainRgn.CreateRectRgn(0, 0, rc.Width(), rc.Height());//初始化区域
for (x = 0; x <= rc.Width(); x++)
{
for (y = 0; y <= rc.Height(); y++)
{
//将透明部分去掉
currentColor = memDC.GetPixel(x, y);//得到当前象素颜色
//如果当前色是透明颜色,就将该像素所在位置从窗体区域中去除
if (currentColor == transparentColor)
{
currentRgn.CreateRectRgn(x, y, x + 1, y + 1);//创建区域
mainRgn.CombineRgn(&mainRgn, ¤tRgn, RGN_XOR);//去除相互重叠的区域
currentRgn.DeleteObject();//删除区域对象
}
}
}
SetWindowRgn((HRGN)mainRgn, TRUE);//设置窗体为区域的形状
memDC.DeleteDC();
ReleaseDC(pDC);
重载对话框的OnCtlColor()函数,加入以下代码:
*注意这里的代码只是为了给对话框贴图。对话框的形状在上面的OnInitDialog()函数中就已经确定了。如果没有这里的贴图代码,对话框依然是异形的,但是是默认的白色背景。
HBRUSH CsEXDialogDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
CBitmap bmpBKGround;
bmpBKGround.LoadBitmap(IDB_BITMAP1);//加载图片资源
if (nCtlColor == CTLCOLOR_DLG)
{
CBrush m_Brush(&bmpBKGround);//定义一个位图画刷
CRect rect;
GetClientRect(rect);//获得窗体的客户区域
pDC->SelectObject(&m_Brush);//选中画刷
pDC->FillRect(rect, &m_Brush);//填充客户区域
return m_Brush;
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
编译运行。