// mouse position operation
//code technology document
//niti collection
//图片的划分
//bottom_left
//bottom_middle
//bottom_right
//
//middle_left左边框
//middle_right右边框
//标题栏
//top_left
//top_middle
//top_right
//系统按钮
//sysbutton_min
//sysbutton_max
//sysbutton_close
//sysbutton_restore
namespace mouse
{
//以下是上述画皮肤方法的具体实现部分,举一个画左边框的代码示例,
private void drawmiddle_left(graphics g)
{
brush brush = new texturebrush(middle_left, new rectangle(0, 0,middle_left.width, middle_left.height));
g.fillrectangle(brush, 0, title_width, middle_left.width,height - bottom_middle.height - title_width);
}
鼠标移动 ,以及反应代码
//定义了一个抽象的基类mouseaction,用来表示所有的鼠标事件,它有一个抽象方法action:
public abstract class mouseaction
{
public abstract void action(int screenx, int screeny, system.windows.forms.form form);
}
//向右拉伸窗口事件的代码响应
// mousesizeleft:拉伸左边框
// mousesizebottom:拉伸下边框
// mousesizetop:拉伸上边框
// mousesizetopleft:拉伸左上角
// mousesizetopright:拉伸右上角
// mousesizebottomleft:拉伸左下角
// mousesizebottomright:拉伸右下角
// mousedrag:鼠标拖动
public class mousesizeright : mouseaction
{
private int lx;
public mousesizeright(int locationx)
{
lx = locationx;
}
public override void action(int screenx, int screeny, system.windows.forms.form form)
{
form.width = screenx - lx;
form.invalidate();
}
}
// 鼠标拖动同样也很简单,不过却稍不同于窗口的缩放拉伸,这里举出它的实现代码:
public class mousedrag : mouseaction
{
private int x, y;
public mousedrag(int hitx, int hity)
{
x = hitx;
y = hity;
}
public override void action(int screenx, int screeny, system.windows.forms.form form)
{
form.location = new point(screenx - x, screeny - y);
}
}
//接下来我们开始编写发出事件的代码,先定义几个变量:
private int left = 5, right = 5, bottom = 5, top = 5, title_width = 45;//边框和标题栏的大小
private int x = 0, y = 0;//保存鼠标的临时坐标
private mouseaction mouse;//鼠标的事件响应对象
然后在form的mousedown事件中记录下鼠标的当前坐标:
x = e.x;
y = e.y;
附:e为system.windows.forms.mouseeventargs
//////////
///根据鼠标的坐标定义出事件响应对象:
///
//鼠标点击左上边框
if((e.x <= left + 10 && e.y <= top) || (e.y <= top + 10 && e.x <= left))
{
mouse = new mousesizetopleft(location.x, location.y, width, height);
return;
}
//鼠标点击系统关闭按纽
if(e.x > width - 20 && e.y > 6 && e.x < width - 20 + sysbutton_min.width && e.y < 6 + sysbutton_min.height)
{
close();
return;
}
//大部分的事件响应实际上是在mousemove事件中完成的:
private void form_mousemove(object sender, system.windows.forms.mouseeventargs e)
{
this.parent.cursor = checkcursortype(e.x, e.y);//改变鼠标的指针形状
if(mouse != null)
{
mouse.action(control.mouseposition.x, control.mouseposition.y, this);//执行时间响应
//注意坐标是control.mouseposition这个静态变量给出的,它的值为鼠标在桌面上的全局坐标
}
}
最后,mouseup事件中将mouse变量释放掉:
private void form_mouseup(object sender, system.windows.forms.mouseeventargs e)
{
mouse = null;
}
加上标题栏的双击最大化或者还原的事件:
private void form_doubleclick(object sender, system.eventargs e)
{
if(y > top && y < title_width)
{
if(windowstate == formwindowstate.normal)
{
windowstate = formwindowstate.maximized;
sysbutton = sysbutton_restore;
invalidate();
}
else if(windowstate == formwindowstate.maximized)
{
windowstate = formwindowstate.normal;
sysbutton = sysbutton_max;
invalidate();
}
}
}
//防止窗体被缩小成一个点,最好给窗口的minimumsize赋上一个适当的值,例如200,200
}
新闻热点
疑难解答