110,572
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class Form1 : Form
{
public Form1()
{
this.TopMost = true;
this.MouseClick += delegate { MessageBox.Show("quiting..."); this.Close(); };
}
static void Main()
{
Application.Run(new Form1());
}
protected override CreateParams CreateParams
{
get
{
CreateParams parameter = base.CreateParams;
parameter.ExStyle |= WS_EX_LAYERED;
return parameter;
}
}
protected override void OnLoad(EventArgs e)
{
Bitmap mask = GetMaskImage();
this.Size = mask.Size;
byte blendOp = 0; // AC_SRC_OVER
byte blendFlags = 0; // 0
byte sourceConstantAlpha = 255; // 0 ... 255
byte alphaFormat = 1; // AC_SRC_ALPHA
int blend = BitConverter.ToInt32(new byte[] { blendOp, blendFlags, sourceConstantAlpha, alphaFormat }, 0);
Size size = mask.Size;
Point point = Point.Empty;
IntPtr desktopDC = IntPtr.Zero;
IntPtr memDC = IntPtr.Zero;
IntPtr imgObject = IntPtr.Zero;
try
{
desktopDC = GetDC(IntPtr.Zero);
memDC = CreateCompatibleDC(desktopDC);
imgObject = mask.GetHbitmap(Color.Black);
IntPtr oldObject = SelectObject(memDC, imgObject);
UpdateLayeredWindow(this.Handle, desktopDC, ref point, ref size, memDC, ref point, 0, ref blend, ULW_ALPHA);
SelectObject(memDC, oldObject);
}
finally
{
DeleteObject(imgObject);
DeleteDC(memDC);
ReleaseDC(IntPtr.Zero, desktopDC);
}
}
Bitmap GetMaskImage()
{
Bitmap mask = new Bitmap(250, 250);
string text = "Click me";
using (Font font = new Font(FontFamily.GenericSerif, 30))
using (Graphics g = Graphics.FromImage(mask))
{
g.FillEllipse(Brushes.Purple, 0, 0, 200, 300);
g.DrawString(text, font, Brushes.DarkBlue, 12, 102);
g.DrawString(text, font, Brushes.LightBlue, 10, 100);
}
for (int y = 0; y < mask.Height; y++)
{
for (int x = 0; x < mask.Width; x++)
{
Color c = mask.GetPixel(x, y);
if (c.B > 0) mask.SetPixel(x, y, Color.FromArgb( (255-y) & 0xff, c));
}
}
return mask;
}
const int ULW_ALPHA = 0x02;
const int WS_EX_LAYERED = 0x80000;
[DllImport("user32")]
static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, int crKey, ref int pblend, int dwFlags);
[DllImport("gdi32")]
static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("user32")]
static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32")]
static extern bool DeleteDC(IntPtr hdc);
[DllImport("gdi32")]
static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
[DllImport("gdi32")]
static extern bool DeleteObject(IntPtr hObject);
}