一个关于工具条可以移动和在四周停留的测试

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace qi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool flag=false;
bool ok = false;
int fangshi = 0;
Graphics mygf;
Pen mypen = new Pen(Color.Red);
Color mycl = new Color();
int width, height;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (flag)
{
mygf.Clear(mycl);
mygf.DrawRectangle(mypen, e.X - width, e.Y - 5, width, height);
if (e.X < 20 && e.Y > 20 && e.Y < (this.Size.Height - 20))
{
mygf.Clear(mycl);
mygf.DrawRectangle(mypen, 0, 0, 20, this.Size.Height);
fangshi = 1;
}
else if (e.Y < 20 && e.X < (this.Size.Width - 20))
{
mygf.Clear(mycl);
mygf.DrawRectangle(mypen, 0, 0, this.Size.Width, 20);
fangshi = 2;
}

else if (e.Y < (this.Size.Height - 20) && e.X > (this.Size.Width - 40))
{
mygf.Clear(mycl);
mygf.DrawRectangle(mypen, this.Size.Width - 35, 0, 30, this.Size.Height);
fangshi = 3;
}
else if (e.Y > (this.Size.Height - 55))
{
mygf.Clear(mycl);
mygf.DrawRectangle(mypen, 0, (this.Size.Height - 55), this.Size.Width, 20);
fangshi = 4;
}
else
fangshi = 0;

}
}

private void toolStrip1_MouseDown(object sender, MouseEventArgs e)
{
toolStrip1.Visible = false;
flag = true;
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (flag)
{
toolStrip1.Location = new Point(e.X - width, e.Y - 5);

switch (fangshi)
{
case 0:
toolStrip1.Dock = DockStyle.None;
break;
case 1:
toolStrip1.Dock = DockStyle.Left;
break;
case 2:
toolStrip1.Dock = DockStyle.Top;
break;
case 3:
toolStrip1.Dock = DockStyle.Right;
break;
case 4:
toolStrip1.Dock = DockStyle.Bottom;
break;
}
}
toolStrip1.Visible = true;
mygf.Clear(mycl);
flag = false;
fangshi = 0;
}

private void Form1_Load(object sender, EventArgs e)
{
mycl = this.BackColor;
mygf = this.CreateGraphics();
width = toolStrip1.Size.Width;
height = toolStrip1.Size.Height;
}
}
}
原文地址:https://www.cnblogs.com/ganb/p/2296438.html