手柄控件

翻译|其它|编辑:郝浩|2005-01-14 14:41:00.000|阅读 1514 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>


在StatusBar上有一个可以拖动窗体大小的手柄,鼠标在其上拖动时就可以调整当前窗体的大小,如何自己来仿制一个呢,我作了一个小控件就可以实现了,只是效果上相似,至于系统里到底是怎么来实现的,我是不知道的了,下面是控件的代码,没有写注释,因为怕有人笑我,呵呵,其实简单的不用写注释了:


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.SystemColors.Control, 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.SystemColors.Control, 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;
}
}
}
}


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP