异型窗体绘制方法有两种,一种是updatelayerwindow,将png图整体贴到窗体上,缺点就是窗体没有paint消息响应,无法看到子控件,需要手动绘制出来子控件。另外一种是windowregion方式。但是效率稍低。
一:整图绘制方法
bool UpdateWindowDisplay(int Transparent/* =255 */)
{
HDC hdcTemp=GetDC(m_hWnd);
HDC m_hdcMemory=CreateCompatibleDC(hdcTemp);
HBITMAP hBitMap=CreateCompatibleBitmap(hdcTemp,m_BakWidth,m_BakHeight);//图大小
SelectObject(m_hdcMemory,hBitMap);
Graphics graph(m_hdcMemory);
if(Transparent<0||Transparent>100)
Transparent=100;
//BLENDFUNCTION
m_Blend.SourceConstantAlpha=int(Transparent*2.55);//1~255
HDC hdcScreen=GetDC (m_hWnd);
RECT rect;
GetWindowRect(m_hWnd,&rect);
POINT ptWinPos={rect.left,rect.top};
Point points[] = { Point(0, 0), Point(m_BakWidth, 0), Point(0, m_BakHeight) };
SIZE sizeWindow={m_BakWidth,m_BakHeight};
POINT ptSrc={0,0};
//窗体必须有WS_EX_LAYERED(0x80000)属性
DWORD dwExStyle=GetWindowLong(m_hWnd,GWL_EXSTYLE);
if((dwExStyle&0x80000)!=0x80000)
SetWindowLong(m_hWnd,GWL_EXSTYLE,dwExStyle^0x80000);
bool bRet = false;
if ( m_pImageBackgd != NULL)
{
graph.DrawImage(m_pImageBackgd,points,3);
bRet= UpdateLayeredWindow( m_hWnd,hdcScreen,&ptWinPos,&sizeWindow,m_hdcMemory,&ptSrc,0,&m_Blend,2);
}
graph.ReleaseHDC(m_hdcMemory);
::ReleaseDC(m_hWnd,hdcScreen);
hdcScreen=NULL;
::ReleaseDC(m_hWnd,hdcTemp);
hdcTemp=NULL;
DeleteObject(hBitMap);
DeleteDC(m_hdcMemory);
m_hdcMemory=NULL;
return bRet;
}
二:windowregion方法
a,绘制异型窗体
DrawWindow(HWND m_hFrameWnd)
{
RECT rect;
GetClientRect(m_hFrameWnd,&rect);
int width = rect.right - rect.left;
int heigh = rect.bottom - rect.top;
HRGN hRgn;
//根据图片,计算异型窗体点点坐标
//如三角形,则计算三个点,类推。本例8个点,矩形的rect,top,right,bottom。
tagPOINT pot1;
pot1.x = 4;
pot1.y = 0;
tagPOINT pot2;
pot2.x = 0;
pot2.y = 4;
tagPOINT pot3;
pot3.x = 0;
pot3.y = rect.bottom - rect.top - 5;
tagPOINT pot4;
pot4.x = 5;
pot4.y = rect.bottom - rect.top;
tagPOINT pot5;
pot5.x = width - 5;
pot5.y = rect.bottom - rect.top;
tagPOINT pot6;
pot6.x = width;
pot6.y = rect.bottom - rect.top - 5;
tagPOINT pot7;
pot7.x = width;
pot7.y = 4;
tagPOINT pot8;
pot8.x = width - 5;
pot8.y = 0;
tagPOINT lppt[]={pot1,pot2,pot3,pot4,pot5,pot6,pot7,pot8};
hRgn = CreatePolygonRgn(lppt,8,ALTERNATE);
SetWindowRgn(m_hFrameWnd,hRgn,TRUE);
}
b.使用GDI+贴图
void IWindow::DrawDialog(HDC hdc)
{
RECT WinRC;
GetWindowRect(m_hFrameWnd,&WinRC);
Graphics g(hdc);
Status s ;
//Create an Image object.
if ( m_pImageBackgd == NULL)
{
m_pImageBackgd = Gdiplus::Image::FromFile(L"DeskEngine//skin//default.png");//图片
//Draw the original source image.
s = g.DrawImage(m_pImageBackgd,(REAL)m_pImageBackgd->GetWidth(),(REAL)m_pImageBackgd->GetHeight());
if ( s != 0)
{
printf("Failed to DrawImage(%d)./n",s);
}
}
// Define the portion of the image to draw.
REAL srcX = 0.0f;
REAL srcY = 0.0f;
REAL srcWidth = 0.0f;
REAL srcHeight = 0.0f;
RectF destRect(0, 0, srcWidth, srcHeight);
//绘制左上角
srcX = 185.0f;
srcY = 255.0f;
srcWidth = 7.0f;
srcHeight = 28.0f;
// Create a RectF object that specifies the destination of the image.
destRect.X = 0.0f;
destRect.Y = 0.0f;
destRect.Width = srcWidth;
destRect.Height = srcHeight;
// Create an ImageAttributes object that specifies a recoloring from red to blue.
ImageAttributes remapAttributes;
ColorMatrix colorMatrix = {
1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 1.0f };
remapAttributes.SetColorMatrix(&colorMatrix);
//remapAttributes.SetColorKey(
// Color(255, 255, 255),
// Color(255, 255, 255),
// ColorAdjustTypeBitmap);
s = g.DrawImage(m_pImageBackgd,destRect,srcX,srcY,srcWidth,srcHeight,UnitPixel, &remapAttributes,NULL,NULL);
if ( s != 0)
{
#ifdef _DEBUG
printf("error = %d/n",s);
#endif
}
//绘制左边线
srcX = 185.0f;
srcY = 282.0f;
srcWidth = 7.0f;
srcHeight = WinRC.bottom - WinRC.top - 35.0f;
// Create a RectF object that specifies the destination of the image.
destRect.X = 0.0f;
destRect.Y = 28.0f;
destRect.Width = srcWidth;
destRect.Height = srcHeight;
//Y轴拉伸
s = g.DrawImage(m_pImageBackgd,destRect,srcX,srcY,srcWidth,100,UnitPixel, &remapAttributes,NULL,NULL);
//绘制左下角
srcX = 185.0f;
srcY = 761.0f;
srcWidth = 7.0f;
srcHeight = 7.0f;
// Create a RectF object that specifies the destination of the image.
destRect.X = 0.0f;
destRect.Y = WinRC.bottom - WinRC.top-srcWidth;
destRect.Width = srcWidth;
destRect.Height = srcHeight;
s = g.DrawImage(m_pImageBackgd,destRect,srcX,srcY,srcWidth,srcHeight,UnitPixel, &remapAttributes,NULL,NULL);
//底边线
srcX = 200.0f;
srcY = 761.0f;
srcWidth = WinRC.right - WinRC.left - srcWidth*2;
srcHeight = 7.0f;
// Create a RectF object that specifies the destination of the image.
destRect.X = 7.0f;
destRect.Y = WinRC.bottom - WinRC.top - srcHeight;
destRect.Width = srcWidth;
destRect.Height = srcHeight;
s = g.DrawImage(m_pImageBackgd,destRect,srcX,srcY,200,srcHeight,UnitPixel, &remapAttributes,NULL,NULL);
//右下角
srcX = 832.0f;
srcY = 761.0f;
srcWidth = 7.0f;
srcHeight = 7.0f;
// Create a RectF object that specifies the destination of the image.
destRect.X = WinRC.right - WinRC.left - srcWidth ;
destRect.Y = WinRC.bottom-WinRC.top - srcHeight;
destRect.Width = srcWidth;
destRect.Height = srcHeight;
s = g.DrawImage(m_pImageBackgd,destRect,srcX,srcY,srcWidth,srcHeight,UnitPixel, &remapAttributes,NULL,NULL);
//右边线
srcX = 832.0f;
srcY = 282.0f;
srcWidth = 7.0f;
srcHeight = WinRC.bottom - WinRC.top - 35.0f;
// Create a RectF object that specifies the destination of the image.
destRect.X = WinRC.right-WinRC.left - srcWidth;
destRect.Y = 28;
destRect.Width = srcWidth;
destRect.Height = srcHeight;
s = g.DrawImage(m_pImageBackgd,destRect,srcX,srcY,srcWidth,200,UnitPixel, &remapAttributes,NULL,NULL);
//右上角
srcX = 832.0f;
srcY = 255.0f;
srcWidth = 7.0f;
srcHeight = 28.0f;
// Create a RectF object that specifies the destination of the image.
destRect.X = WinRC.right - WinRC.left - srcWidth;
destRect.Y = 0.0f;
destRect.Width = srcWidth;
destRect.Height = srcHeight;
s = g.DrawImage(m_pImageBackgd,destRect,srcX,srcY,srcWidth,srcHeight,UnitPixel, &remapAttributes,NULL,NULL);
//中间标题栏
srcX = 210.0f;
srcY = 255.0f;
srcWidth = WinRC.right - WinRC.left - srcWidth*2;
srcHeight = 28.0f;
// Create a RectF object that specifies the destination of the image.
destRect.X = 7.0f;
destRect.Y = 0.0f;
destRect.Width = srcWidth;
destRect.Height = srcHeight;
s = g.DrawImage(m_pImageBackgd,destRect,srcX,srcY,200,srcHeight,UnitPixel, &remapAttributes,NULL,NULL);