第一步:建立一个窗体,设置其属性:
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Opacity = 0.5;
第二步:设置窗体的OnLoad事件:
private void Form2_Load(object sender, EventArgs e)
{
this.Top = 20;
this.Left = Screen.PrimaryScreen.Bounds.Width - 80;
this.Width = 60;
this.Height = 60;
}
第三步:修改窗体的Paint事件,美化界面,这里做一个渐变背景,需要引入System.Drawing.Drawing2D;
private void Form2_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Color FColor = Color.Red;
Color TColor = Color.Yellow;
Brush b = new LinearGradientBrush(this.ClientRectangle, FColor, TColor, LinearGradientMode.ForwardDiagonal);
g.FillRectangle(b, this.ClientRectangle);
}
第四步:需要实现鼠标拖动悬浮窗体,
const int WM_NCHITTEST = 0x0084;
const int HTCLIENT = 0x0001;
const int HTCAPTION = 0x0002;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch(m.Msg)
{
case WM_NCHITTEST:
base.WndProc(ref m);
if (m.Result==(IntPtr)HTCLIENT)
m.Result=(IntPtr)HTCAPTION;
break;
default: