1.
#define TRANS_COLOR (ALPHA_MASK | RGB(255, 0, 255))
BOOL CMyDialog::OnInitDialog()
{
CDialog::OnInitDialog();
CRgn rgn;
CreateRgnByImage(_T("MyImage.png"), rgn);
SetWindowRgn(rgn, TRUE);
return TRUE;
}
void CMyDialog::CreateRgnByImage(const CString &imageName, CRgn &rgn)
{
Bitmap img(imageName);
ASSERT(PixelFormat32bppARGB == img.GetPixelFormat());
Rect rect(0, 0, img.GetWidth(), img.GetHeight());
BitmapData data;
img.LockBits(&rect, ImageLockModeRead, PixelFormat32bppARGB, &data);
UINT *pData = static_cast<UINT*>(data.Scan0);
rgn.CreateRectRgn(0, 0, 0, 0);
CRgn tempRgn;
for (UINT h = 0; h < data.Height; ++ h)
{
UINT w = 0;
while(w < data.Width)
{
UINT leftX;
while (w ++ < data.Width && TRANS_COLOR == *pData ++);
leftX = w;
while (w ++ < data.Width && TRANS_COLOR != *pData ++);
tempRgn.CreateRectRgn(leftX, h, w - 1, h + 1);
rgn.CombineRgn(&rgn, &tempRgn, RGN_OR);
tempRgn.DeleteObject();
}
}
img.UnlockBits(&data);
}
2.
如果使用一幅位图,通过挖图的方式来做成一个不规则的窗体,是很累人的一件事。
而使用GDI+,可以直接用PNG图片,通过图片本身的透明度,自动创建不规则窗体。
比如,你手中有个美女图,通过PhotoShop等工具,把美女的身体抠出来,保存为PNG格式的图片,除了美女的身体,图片的其他部门都是透明的。后面的工作就是,写一个windows小程序,加载这幅美女图,让她成为我们程序的界面。
Win32程序的框架我就懒得贴了,直接贴关键代码了。
view plain#include<GdiPlus.h>
- using namespace Gdiplus;
- #define MAX_LOADSTRING 100
- //Gdiplus start up params
- ULONG_PTR m_gdiplusToken;
- GdiplusStartupInput m_gdiplusStartupInput;
- //rendering prarams
- BLENDFUNCTION m_Blend;
- //images
- Bitmap * m_BackgoundImage;
- int WINAPI WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow)
- {
- //....
- }