1.使用WINDOWS API和TIMER控件
class Win32API
{
[DllImport("user32")]
public static extern bool PtInRect(ref Rectangle r, Point p);
//方法名称固定
}
向上方收缩代码:
private void timer1_Tick(object sender, EventArgs e)
{
Point pp = new Point(Cursor.Position.X, Cursor.Position.Y);
Rectangle rects = new Rectangle(this.Left, this.Top, this.Left + this.Width, this.Top + this.Height);
if ((this.Top < 0) && Win32API.PtInRect(ref rects, pp))
{
this.Top = 0;
}
else
{
if (this.Top > -5 && this.Top < 5 && !(Win32API.PtInRect(ref rects, pp)))
{
this.Top = 5 - this.Height;
}
}
}
向左边收缩代码:
private void timer2_Tick(object sender, EventArgs e)
{
Point pp = new Point(Cursor.Position.X, Cursor.Position.Y);
Rectangle rects = new Rectangle(this.Left, this.Top, this.Left + this.Width, this.Top + this.Height);
if ((this.Left < 0) && Win32API.PtInRect(ref rects, pp))
{
this.Left = 0;
}
else
{
if (this.Left > -5 && this.Left < 5 && !(Win32API.PtInRect(ref rects, pp)))
{
this.Left = 5 - this.Right;
}
}
}
启动两个timer控件:
this.timer1.Start();
this.timer2.Start();
2.使用事件
double width;
double height;
private void win_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
if (this.Top <= 0)
{
this.Top = 0;
this.Height = 10;
}
if (this.Left <= 0)
{
this.Left = 0;
this.Width = 10;
}
}
private void win_MouseMove(object sender, MouseEventArgs e)
{
if (this.Top == 0)
{
this.Height = height;
}
if (this.Left == 0)
{
this.Width = width;
}
}
private void win_MouseLeave(object sender, MouseEventArgs e)
{
if (this.Top == 0 && this.Height == height)
{
this.Height = 10;
}
if (this.Left == 0 && this.Width == width)
{
this.Width = 10;
}
}
private void win_Loaded(object sender, RoutedEventArgs e)
{
height = win.Height;
width = win.Width;
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
在WinForm中实现收缩功能
最新推荐文章于 2022-08-18 17:02:17 发布