这样的话,只能重写一个panel的控件来实现,因为这时panel是当前接爱鼠标事件,当然你可以做成当panel有鼠标事件时让窗体得到,但是重写一个panel控件有时可能更有意思,我没有重写panel,但我重写了一个button的,代码我想只是它们继承的基类不一样罢了:代码大致如下:
using System;
using System.Windows.Forms;
using System.Drawing;namespace MoveControlLocation
{
/// <summary>
/// 
/// </summary>
public class CusLabel: System.Windows.Forms.Button
{
public CusLabel()
{
// 
// TODO: 在此处添加构造函数逻辑
//
}
private const int WM_NCHITTEST = 0x84;
private const int WM_SIZING = 532; private const int HTERROR = (-2);
private const int HTTRANSPARENT = (-1);
private const int HTNOWHERE = 0;
private const int HTCLIENT =  1;
private const int HTCAPTION = 2;
private const int HTSYSMENU = 3;
private const int HTGROWBOX = 4;
private const int HTSIZE = HTGROWBOX;
private const int HTMENU = 5;
private const int HTHSCROLL = 6;
private const int HTVSCROLL = 7;
private const int HTMINBUTTON = 8;
private const int HTMAXBUTTON = 9;
private const int HTLEFT = 10;
private const int HTRIGHT = 11;
private const int HTTOP = 12;
private const int HTTOPLEFT = 13;
private const int HTTOPRIGHT = 14;
private const int HTBOTTOM = 15;
private const int HTBOTTOMLEFT = 16;
private const int HTBOTTOMRIGHT = 17;
private const int HTBORDER = 18;
private const int HTREDUCE = HTMINBUTTON;
private const int HTZOOM = HTMAXBUTTON;
private const int HTSIZEFIRST = HTLEFT;
private const int HTSIZELAST = HTBOTTOMRIGHT;
/* WINVER >= 0x0400 */
private const int HTOBJECT = 19;
private const int HTCLOSE = 20;
private const int HTHELP = 21;
/* WINVER >= 0x0400 */ private const int BorderWidth = 3;
private int CaptionHeight = 17; //可以调整窗体的大小和移动窗体的位置
protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch(m.Msg)
{
case WM_NCHITTEST:
base.WndProc(ref m);
if (DesignMode)
{
return;
}
if ((int)m.Result == HTCLIENT)
{
m.HWnd = this.Handle;
int pLeft=0;
int pTop=0; bool IsControl = true;
if (IsControl)
{
if ((this.Parent as Form).FormBorderStyle==FormBorderStyle.None)
{
pLeft=this.Parent.Left;
pTop=this.Parent.Top;
}
else
{
pLeft=this.Parent.Left+2;
CaptionHeight = this.Parent.Height-this.Parent.ClientRectangle.Height-2 * BorderWidth;
pTop=this.Parent.Top + CaptionHeight + BorderWidth-2;
}
} if ((Cursor.Position.X<=pLeft + this.Left + BorderWidth) && (Cursor.Position.Y <= pTop + this.Top + BorderWidth))
m.Result = (IntPtr)HTTOPLEFT;//左上
else if ((Cursor.Position.X>=pLeft + this.Left + this.Width-BorderWidth) && (Cursor.Position.Y<=pTop + this.Top +BorderWidth))
m.Result = (IntPtr)HTTOPRIGHT;//右上
else if ((Cursor.Position.X <= pLeft + this.Left + BorderWidth) && (Cursor.Position.Y>=pTop + this.Top + this.Height-BorderWidth))
m.Result = (IntPtr)HTBOTTOMLEFT;//左下
else if ((Cursor.Position.X>=pLeft + this.Left + this.Width-BorderWidth) && (Cursor.Position.Y>=pTop + this.Top + this.Height-BorderWidth))
m.Result = (IntPtr)HTBOTTOMRIGHT;//右下
else if (Cursor.Position.X<=pLeft + this.Left + BorderWidth-1)
m.Result = (IntPtr)HTLEFT;//左
else if (Cursor.Position.X>=pLeft + this.Left + this.Width-BorderWidth)
m.Result = (IntPtr)HTRIGHT;//右
else if (Cursor.Position.Y<=pTop + this.Top + BorderWidth-1)
m.Result = (IntPtr)HTTOP;//上
else if (Cursor.Position.Y>=pTop + this.Top + this.Height-BorderWidth)
m.Result = (IntPtr)HTBOTTOM;//下
else if (Cursor.Position.Y<=pTop + this.Top + CaptionHeight + BorderWidth )
{
if (Cursor.Position.X<=pLeft + this.Left + BorderWidth + CaptionHeight)
{
m.Result = (IntPtr)HTSYSMENU;
}
else
{
m.Result = (IntPtr)HTCAPTION;
}
}
}
return;
default:
base.WndProc(ref m);
break;
}
}  }
}

解决方案 »

  1.   

    鼠标形状已经改变了符合要求,但是窗体大小怎么控制呢?半天也没整出来:(
    代码如下:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication5
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private CusLabel label1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.label1 = new WindowsApplication5.Form1.CusLabel();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.BackColor = System.Drawing.SystemColors.ActiveCaption;
    this.label1.Dock = System.Windows.Forms.DockStyle.Top;
    this.label1.Location = new System.Drawing.Point(0, 0);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(292, 23);
    this.label1.TabIndex = 0;
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.label1);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    }
    public class CusLabel: System.Windows.Forms.Label
    {

    private const int WM_NCHITTEST = 0x84;
    private const int WM_SIZING = 532; private const int HTERROR = (-2);
    private const int HTTRANSPARENT = (-1);
    private const int HTNOWHERE = 0;
    private const int HTCLIENT =  1;
    private const int HTCAPTION = 2;
    private const int HTSYSMENU = 3;
    private const int HTGROWBOX = 4;
    private const int HTSIZE = HTGROWBOX;
    private const int HTMENU = 5;
    private const int HTHSCROLL = 6;
    private const int HTVSCROLL = 7;
    private const int HTMINBUTTON = 8;
    private const int HTMAXBUTTON = 9;
    private const int HTLEFT = 10;
    private const int HTRIGHT = 11;
    private const int HTTOP = 12;
    private const int HTTOPLEFT = 13;
    private const int HTTOPRIGHT = 14;
    private const int HTBOTTOM = 15;
    private const int HTBOTTOMLEFT = 16;
    private const int HTBOTTOMRIGHT = 17;
    private const int HTBORDER = 18;
    private const int HTREDUCE = HTMINBUTTON;
    private const int HTZOOM = HTMAXBUTTON;
    private const int HTSIZEFIRST = HTLEFT;
    private const int HTSIZELAST = HTBOTTOMRIGHT;
    /* WINVER >= 0x0400 */
    private const int HTOBJECT = 19;
    private const int HTCLOSE = 20;
    private const int HTHELP = 21;
    /* WINVER >= 0x0400 */ private const int BorderWidth = 2;

    private int CaptionHeight = 17; //可以调整窗体的大小和移动窗体的位置
    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
    switch(m.Msg)
    {
    case WM_NCHITTEST:
    base.WndProc(ref m);
    if (DesignMode)
    {
    return;
    }
    if ((int)m.Result == HTCLIENT)
    {
    m.HWnd = this.Handle;
    int pLeft=0;
    int pTop=0; bool IsControl = true;
    if (IsControl)
    {
    if ((this.Parent as Form).FormBorderStyle==FormBorderStyle.None)
    {
    pLeft=this.Parent.Left;
    pTop=this.Parent.Top;
    }
    else
    {
    pLeft=this.Parent.Left+2;
    CaptionHeight = this.Parent.Height-this.Parent.ClientRectangle.Height-2 * BorderWidth;
    pTop=this.Parent.Top + CaptionHeight + BorderWidth-2;
    }
    } if ((Cursor.Position.X<=pLeft + this.Left + BorderWidth) && (Cursor.Position.Y <= pTop + this.Top + BorderWidth))
    m.Result = (IntPtr)HTTOPLEFT;//左上
    else if ((Cursor.Position.X>=pLeft + this.Left + this.Width-BorderWidth) && (Cursor.Position.Y<=pTop + this.Top +BorderWidth))
    m.Result = (IntPtr)HTTOPRIGHT;//右上
    else if ((Cursor.Position.X <= pLeft + this.Left + BorderWidth) && (Cursor.Position.Y>=pTop + this.Top + this.Height-BorderWidth))
    m.Result = (IntPtr)HTLEFT;//左下-》左
    else if ((Cursor.Position.X>=pLeft + this.Left + this.Width-BorderWidth) && (Cursor.Position.Y>=pTop + this.Top + this.Height-BorderWidth))
    m.Result = (IntPtr)HTRIGHT;//右下-》右
    else if (Cursor.Position.X<=pLeft + this.Left + BorderWidth-1)
    m.Result = (IntPtr)HTLEFT;//左
    else if (Cursor.Position.X>=pLeft + this.Left + this.Width-BorderWidth)
    m.Result = (IntPtr)HTRIGHT;//右
    else if (Cursor.Position.Y<=pTop + this.Top + BorderWidth-1)
    m.Result = (IntPtr)HTTOP;//上
    else if (Cursor.Position.Y<=pTop + this.Top + CaptionHeight + BorderWidth )
    {
    if (Cursor.Position.X<=pLeft + this.Left + BorderWidth + CaptionHeight)
    {
    m.Result = (IntPtr)HTSYSMENU;
    }
    else
    {
    m.Result = (IntPtr)HTCAPTION;
    }
    }
    else 
    m.Result = (IntPtr)HTCAPTION;//移动窗体  
    }
    return;
    default:
    base.WndProc(ref m);
    break;
    }

    }
    private const int WM_NCHITTEST = 0x84;
    private const int HTCLIENT = 0x1;
    private const int HTSIZE = 0x4;
    private const int HTCAPTION = 0x2; 
    private const int WM_SIZING = 532;
    private const int HTMINBUTTON = 8;
    private const int BorderWidth = 5; // m.Result = (IntPtr)20;//关闭按纽
    // m.Result = (IntPtr)21;//help按纽 //可以调整窗体的大小和移动窗体的位置
    protected override void WndProc(ref Message m)
    {
    System.Diagnostics.Debug.WriteLine(m.ToString());
    switch(m.Msg)
    {
    case WM_NCHITTEST:
    base.WndProc(ref m);
    if (DesignMode)
    {
    return;
    }
    if (((int)m.Result == HTCLIENT ))
    {
    if ((Cursor.Position.X<=this.Left + BorderWidth) && (Cursor.Position.Y <= this.Top + BorderWidth))
    m.Result = (IntPtr)13;//左上
    else if ((Cursor.Position.X>=this.Left + this.Width-BorderWidth) && (Cursor.Position.Y<=this.Top +BorderWidth))
    m.Result = (IntPtr)14;//右上
    else if ((Cursor.Position.X <= this.Left + BorderWidth) && (Cursor.Position.Y>=this.Top + this.Height-BorderWidth))
    m.Result = (IntPtr)16;//左下
    else if ((Cursor.Position.X>=this.Left + this.Width-BorderWidth) && (Cursor.Position.Y>=this.Top + this.Height-BorderWidth))
    m.Result = (IntPtr)17;//右下
    else if (Cursor.Position.X<=this.Left + BorderWidth)
    m.Result = (IntPtr)10;//左
    else if (Cursor.Position.X>=this.Left + this.Width-BorderWidth)
    m.Result = (IntPtr)11;//右
    else if (Cursor.Position.Y<=this.Top + BorderWidth)
    m.Result = (IntPtr)12;//上
    else if (Cursor.Position.Y>=this.Top + this.Height-BorderWidth)
    m.Result = (IntPtr)15;//下
    else 
    m.Result = (IntPtr)HTCAPTION;//移动窗体   
    }
    return;
    default:
    base.WndProc(ref m);
    break;
    }
    }  }
    }
      

  2.   

    明白你的意思了,你可以再加这一句话试试:
    private void panel1_Resize(object sender, System.EventArgs e)
    {
    this.Size = this.panel1.Size;
    }
      

  3.   

    还有,就是像你这种情你可以给窗体留出一个"边"来,让你的窗体在这个"边"上调整窗体的大小位置等.
    也就是说你可以不用
    this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;来填满整个窗体.而是给你的窗体留出一个可以操作窗体的边来,你也应注意到一个正常的窗体由其是在XP下看到它是有一个边的,正是这个边才能操作窗体的大小位置,如果把这个边设为None则不能再对它操作了,我想应该是这个道理的.
      

  4.   

    to hbxtlhx(下着春雨的天) 
    this.Size = this.panel1.Size这个方法是行不通的
    你说给窗体留出一个"边"来是没有办法的办法
    ^_^
      

  5.   

    to hbxtlhx(下着春雨的天) 
    要是有更好的办法请发短消息告诉我
    结贴了
      

  6.   

    不是办法的办法:
    可以给窗体添加Panel控件,调整好与form的上下左右的距离,给窗体留出一个"边"
    效果也不错,^_^
      

  7.   

    问题是怎样来给窗体这个边?
    我的一般的做法是得写如下的方法,在里面放上要调整的控件的位置或大小等信息.
    protected override void OnLayout(LayoutEventArgs levent)
    {
    base.OnLayout (levent);
    }
      

  8.   

    对Pannel操作,刚才忘说了,要把panel的Anchor设置为上下左右
    this.Panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
      

  9.   

    最简单的方法:
    新建一个项目,在窗体Load事件中添加
    private void Form1_Load(object sender, System.EventArgs e)
    {
    this.ControlBox = false;
    this.Text = string.Empty;
    }
    就把以上的问题全解决了,就这么简单的两行代码
    ^_^
      

  10.   

    问题是怎样来给窗体这个边?
    this.DockPadding.Bottom = 2;
    this.DockPadding.Left = 2;
    this.DockPadding.Right = 2;
    this.DockPadding.Top = 2;
    真简单啊