准备自己做一个比较独特的窗体控件,到了用鼠标拖动边框时不能实现变大,只能变小.是不是要获得鼠标在屏幕中的坐标,怎么能实现呢.还请各位高手能指点

解决方案 »

  1.   

    /// <summary>
    /// 鼠标左击标题栏处理程序,开始移动本控件
    /// 2004-10-04 修改,改为移动焦点框
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void TitlePanel_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if(!this.Entered)
    {
    this.Focus();
    Entered=true;
    }
    if( (e.Button==MouseButtons.Left) )
    { Moving=true;
    OldLeft = e.X;
    OldTop = e.Y;
    //获取屏幕上下文 Common.ClipCursorIn(this.Parent);
    ScreenDC = Crownwood.Magic.Win32.User32.GetDC(System.IntPtr.Zero);
    Point p = new Point(0,0);
    p = this.Parent.PointToScreen(p);
    ClipRgn = WinAPI.CreateRectRgn(p.X,p.Y,p.X + this.Parent.Width,p.Y + this.Parent.Height);
    WinAPI.SelectClipRgn(ScreenDC,ClipRgn);
    //rect.left = -900;
    #region 画焦点框
    rect.left = this.Left;
    rect.top = this.Top;
    rect.right = this.Left + this.Width;
    rect.bottom = this.Top + this.Height; p = new Point(rect.left,rect.top);
    p = this.Parent.PointToScreen(p);
    rect.left = p.X;
    rect.top = p.Y;

    p = new Point(rect.right,rect.bottom);
    p = this.Parent.PointToScreen(p);
    rect.right = p.X;
    rect.bottom = p.Y;
    Crownwood.Magic.Win32.User32.DrawFocusRect(ScreenDC,ref rect);
    #endregion
    //Crownwood.Magic.Win32.User32.
    }
    }
    /// <summary>
    /// 鼠标在标题栏移动时的程序,移动本控件
    /// 2004-10-04 修改,改为移动焦点框
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void TitlePanel_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if( Moving )
    {

    //将以前的重新画一遍 //if(rect.left != -900)
    Crownwood.Magic.Win32.User32.DrawFocusRect(ScreenDC,ref rect);
    rect.left = this.Left + e.X - OldLeft;
    rect.top = this.Top + e.Y - OldTop;
    rect.right = rect.left + this.Width;
    rect.bottom = rect.top + this.Height; Point p = new Point(rect.left,rect.top);
    p = this.Parent.PointToScreen(p);
    rect.left = p.X;
    rect.top = p.Y;

    p = new Point(rect.right,rect.bottom);
    p = this.Parent.PointToScreen(p);
    rect.right = p.X;
    rect.bottom = p.Y;
    Crownwood.Magic.Win32.User32.DrawFocusRect(ScreenDC,ref rect);
    }
    }
    /// <summary>
    /// 鼠标键在标题栏释放处理程序,移动本控件结束
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void TitlePanel_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if (Moving) 
    {
    Crownwood.Magic.Win32.User32.DrawFocusRect(ScreenDC,ref rect);
    if ((this.Left!=this.Left+e.X+OldLeft)
    ||(this.Top!=this.Top+e.Y-OldTop))
    {
    this.Left=this.Left+e.X  - OldLeft;
    this.Top=this.Top+e.Y  - OldTop;
    }

    Common.RestoreCursor();
    WinAPI.DeleteObject(ClipRgn);
    Crownwood.Magic.Win32.User32.ReleaseDC(System.IntPtr.Zero,ScreenDC);
    this.Parent.Refresh();
    }
    Moving=false;
    if(e.Button==MouseButtons.Right&&PopMenu!=null)
    PopMenu.TrackPopup(TitlePanel.PointToScreen(new Point(e.X,e.Y)));
    //if Assigned(FOnColRePaint) then
    //    FOnColRePaint(self);
    }
      

  2.   

    谢谢xxj!但有很多地方不明白.能不能再给我讲讲Common.ClipCursorIn(this.Parent);
    ScreenDC = Crownwood.Magic.Win32.User32.GetDC(System.IntPtr.Zero);
      

  3.   

    [DllImport("User32.dll")]
    public static System.IntPtr GetDC ( System.IntPtr hWnd );ClipCursorIn(this.Parent);/// <summary>
    /// 锁定鼠标光标活动范围,引入自user32.dll
    /// </summary>
    /// <param name="r"></param>
    [DllImport("user32.dll")]
    public extern static void ClipCursor(RECT r);