日期:2014-05-19  浏览次数:20920 次

如何判断鼠标在一个控件的边缘,以及缩放这个控件呢?

最好能给出代码来,谢谢了!

------解决方案--------------------
前2天在某高人博客上找到的控件的代码,帮了我很大忙,喜欢也能给你一些帮助
谢谢那位高人,虽然记得是谁了
这个控件用于实现缩放From

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Size_MoveCtr
{
/// <summary>
/// Mover 的摘要说明。
/// </summary>
public class SizeGrip : System.Windows.Forms.Control
{
private const int WM_NCHITTEST = 0x84;
private const int WM_NCLBUTTONDOWN = 0xa1;

private const int HTCLIENT = 1;
private const int HTBOTTOMRIGHT = 17;

private System.Drawing.Drawing2D.GraphicsPath m_Path;

[System.Runtime.InteropServices.DllImport( "user32.dll ")]
public static extern int SendMessage(IntPtr hWnd,
int Msg,
IntPtr wParam,
IntPtr lParam
);

public SizeGrip()
{
this.m_Path = new System.Drawing.Drawing2D.GraphicsPath();

this.m_Path.StartFigure();
this.m_Path.AddLines(new Point[] { new Point(this.Width, 0), new Point(this.Width - this.Height, this.Height), new Point(this.Width, this.Height) });
this.m_Path.CloseFigure();
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//ControlPaint.DrawSizeGrip(this.CreateGraphics(), System.Drawing.Color.White, this.Width - this.Height, 0, this.Height, this.Height);

}

protected override void OnSizeChanged(EventArgs e)
{
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.StartFigure();
path.AddLines(new Point[] { new Point(this.Width, 0), new Point(this.Width - this.Height, this.Height), new Point(this.Width, this.Height) });
path.CloseFigure();

Region reReg = new Region(path);
reReg.Complement(m_Path);

this.Invalidate(reReg);

base.OnSizeChanged(e);
this.m_Path = path;

//ControlPaint.DrawSizeGrip(this.CreateGraphics(), System.Drawing.Color.FromArgb(0, 30, 88), this.Width - this.Height, 0, this.Height, this.Height);
}

protected override void WndProc(ref Message m)
{
if (this.DesignMode)
{
base.WndProc(ref m);
return;
}

switch (m.Msg)
{
case WM_NCHITTEST:
SendMessage(this.Parent.Handle, m.Msg, m.WParam, m.LParam);
base.WndProc(ref m);
if ((int)m.Result == HTCLIENT)
{
m.Result = (IntPtr)HTBOTTOMRIGHT;//右下
}
break;
case WM_NCLBUTTONDOWN:
SendMessage(this.Parent.Handle, m.Msg, m.WParam, m.LParam);
break;
default:
base.WndProc(ref m);
break;
}
}
}
}



------解决方案--------------------
经验之谈:
1.做一个小的正方形,就是在开发过程中,我们选中控件时控件周围会出现的小点点(8个),
鼠标移到该正方形控件上,可以根据正方形所在的位置,给出合适的鼠标形状。
2.使用一个矩形控件来在鼠标拖拽中代理显示被选择中的控件,当拖拽完毕后在根据矩形控件的状态来处理真正的控件属性,矩形控件主要用来显示控件的外边框