110,536
社区成员
发帖
与我相关
我的任务
分享
public Bitmap MainImage = null;
public Bitmap HeadImage = null;
Bitmap NowDisImage = null;
public ShakingForm()
{
base.MouseDown += ShakingForm_MouseDown;
}
void ShakingForm_MouseDown(object sender, MouseEventArgs e)
{
if (new Rectangle(0, 0, base.Width, HeadImage == null ? 30 : HeadImage.Height).Contains(e.Location))
{
Win32.ReleaseCapture();
Win32.SendMessage(base.Handle, Win32.WM_SYSCOMMAND, Win32.SC_MOVE + Win32.HTCAPTION, 0);//窗体移动
}
}
#region 调用UpdateLayeredWindow函数
protected override CreateParams CreateParams
{//重载窗体的CreateParams方法
get
{
const int WS_MINIMIZEBOX = 0x00020000; // Winuser.h中定义
CreateParams cp = base.CreateParams;
cp.Style = cp.Style | WS_MINIMIZEBOX; // 允许最小化操作
cp.ExStyle |= 0x00080000; // WS_EX_LAYERED
return cp;
}
}
IntPtr oldBits = IntPtr.Zero;
IntPtr screenDC = IntPtr.Zero;
IntPtr hBitmap = IntPtr.Zero;
IntPtr memDc = IntPtr.Zero;
Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
Win32.Point srcLoc = new Win32.Point(0, 0);
Win32.Size bitMapSize = new Win32.Size(0, 0);
Win32.Point topLoc = new Win32.Point(0, 0);
void SetBits()//调用UpdateLayeredWindow()方法。this.BackgroundImage为你事先准备的带透明图片。
{
if (NowDisImage == null)
return;
if (!Bitmap.IsCanonicalPixelFormat(NowDisImage.PixelFormat) || !Bitmap.IsAlphaPixelFormat(NowDisImage.PixelFormat))
throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
if (HeadImage != null)
{
Graphics g = Graphics.FromImage(NowDisImage);
g.DrawImage(HeadImage, new Point(0, 0));
g.Dispose();
}
oldBits = IntPtr.Zero;
screenDC = Win32.GetDC(IntPtr.Zero);
hBitmap = IntPtr.Zero;
memDc = Win32.CreateCompatibleDC(screenDC);
topLoc = new Win32.Point(Left, Top);
bitMapSize = new Win32.Size(NowDisImage.Width, NowDisImage.Height);
blendFunc = new Win32.BLENDFUNCTION();
srcLoc = new Win32.Point(0, 0);
hBitmap = NowDisImage.GetHbitmap(Color.FromArgb(0));
oldBits = Win32.SelectObject(memDc, hBitmap);
blendFunc.BlendOp = Win32.AC_SRC_OVER;
blendFunc.SourceConstantAlpha = 255;
blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
blendFunc.BlendFlags = 0;
Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
if (hBitmap != IntPtr.Zero)
{
Win32.SelectObject(memDc, oldBits);
Win32.DeleteObject(hBitmap);
}
Win32.ReleaseDC(IntPtr.Zero, screenDC);
Win32.DeleteDC(memDc);
}
#endregion
List<ShakingControl> controls = new List<ShakingControl>();
/// <summary>
/// 控件列表
/// </summary>
public List<ShakingControl> ShakingControls
{
get { return controls; }
set { controls = value; }
}
public void ShakingShow()
{
if (MainImage == null)
return;
NowDisImage = new Bitmap(Size.Width, Size.Height);
Graphics g = Graphics.FromImage(NowDisImage);
g.DrawImage(MainImage, new Point(0, 0));
g.Dispose();
foreach (ShakingControl i in controls.ToArray())
DrawControls(i);
SetBits();
}
void ShakingRefresh()
{
if (MainImage == null)
return;
NowDisImage = new Bitmap(Size.Width, Size.Height);
Graphics g = Graphics.FromImage(NowDisImage);
g.DrawImage(MainImage, new Point(0, 0));
g.Dispose();
foreach (ShakingControl i in controls.ToArray())
DrawControlsType(i);
SetBits();
}
void DrawControls(ShakingControl control)
{
if (control is ShakingButton)//按钮
{
DrawControls((ShakingButton)control);
}
}
void DrawControlsType(ShakingControl control)
{
if (control is ShakingButton)//按钮
{
if (((ShakingButton)control).MouseEnter)
DrawMouseMove((ShakingButton)control);
else
DrawMouseLeave((ShakingButton)control);
}
}
void DrawControls(ShakingButton control)
{
DrawMouseLeave(control);
base.MouseMove += delegate(object sender, MouseEventArgs e)
{
if (!control.MouseEnter && new RectangleF(control.Location.X, control.Location.Y, control.Size.Width, control.Size.Height).Contains(e.Location))
{
control.MouseEnter = true;
DrawMouseMove(control);
ShakingRefresh();
}
else if (control.MouseEnter && !new RectangleF(control.Location.X, control.Location.Y, control.Size.Width, control.Size.Height).Contains(e.Location))
{
control.MouseEnter = false;
DrawMouseLeave(control);
ShakingRefresh();
}
};
base.MouseClick += delegate(object sender, MouseEventArgs e)
{
if (new RectangleF(control.Location.X, control.Location.Y, control.Size.Width, control.Size.Height).Contains(e.Location))
{
//click
control.Click();
}
};
}
无句柄控件实现- - 不知道这样子做 好不好。 感觉后面的控件会越来越难。。。