不规则窗口继承自 System::Windows::Forms::Form
在构造方法中设置
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None; // 不显示边框
this->ShowInTaskbar = false; //不显示在任务栏中
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen; // 显示在屏幕中央
this->TopMost = true; // 窗口在最前面
this->BackgroundImageLayout = ImageLayout::Center; // 背景图的位置
this->BackgroundImage = getTransparentBmpFromImg(img); // 转换为透明图片ARGB
this->Size = this->BackgroundImage->Size; // 窗口大小与背景图相同
// 背景色透明
this->BackColor = Color::Black;
this->TransparencyKey = Color::Black;
// convert Image to transparent Bitmap 你可以把img先绘到ARGB的Bitmap中,现转换。
Bitmap^ getTransparentBmpFromImg(Image^ img) {
Bitmap^ bmp = gcnew Bitmap(img);
bmp->MakeTransparent(bmp->GetPixel(0, 0));
return bmp;
}
Bitmap^ getTransparentBmpFromImg(Image^ img) {
Bitmap^ bmp = gcnew Bitmap(img);
int w = bmp->Width;
int h = bmp->Height;
Rectangle rect = Rectangle(0, 0, w, h);
::Imaging::BitmapData^ bmpData = bmp->LockBits(rect, Imaging::ImageLockMode::ReadWrite, bmp->PixelFormat);
IntPtr iPtr = bmpData->Scan0;
unsigned int* data = reinterpret_cast<unsigned int*>(iPtr.ToInt32());
int s = bmpData->Stride;
unsigned int tp = data[0];
for (int r = 0; r < h; r++)
{
unsigned int* basePtr = data + r*s/4;
for (int c = 0; c < w; c++)
{
if (basePtr[c] == tp)
{
basePtr[c] = 0;
}
}
}
bmp->UnlockBits(bmpData);
return bmp;
}
可以用System::Windows::Forms::Timer 来实现不规则窗口的淡入淡出