2010-04-04 20 views
20

有沒有人知道我沒有邊框時如何調整WinForm的大小。我不想Windows的默認邊框,所以我將屬性「FormBorderStyle」更改爲「無」。這刪除了邊框,但現在它不能調整大小。我已經想出瞭如何移動窗體,我只需要知道如何調整窗體大小。如何移動和調整沒有邊框的表單?

回答

42

即允許移動和縮放表格一些示例代碼:

public partial class Form1 : Form { 
    public Form1() { 
     InitializeComponent(); 
     this.FormBorderStyle = FormBorderStyle.None; 
     this.DoubleBuffered = true; 
     this.SetStyle(ControlStyles.ResizeRedraw, true); 
    } 
    private const int cGrip = 16;  // Grip size 
    private const int cCaption = 32; // Caption bar height; 

    protected override void OnPaint(PaintEventArgs e) { 
     Rectangle rc = new Rectangle(this.ClientSize.Width - cGrip, this.ClientSize.Height - cGrip, cGrip, cGrip); 
     ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc); 
     rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption); 
     e.Graphics.FillRectangle(Brushes.DarkBlue, rc); 
    } 

    protected override void WndProc(ref Message m) { 
     if (m.Msg == 0x84) { // Trap WM_NCHITTEST 
     Point pos = new Point(m.LParam.ToInt32()); 
     pos = this.PointToClient(pos); 
     if (pos.Y < cCaption) { 
      m.Result = (IntPtr)2; // HTCAPTION 
      return; 
     } 
     if (pos.X >= this.ClientSize.Width - cGrip && pos.Y >= this.ClientSize.Height - cGrip) { 
      m.Result = (IntPtr)17; // HTBOTTOMRIGHT 
      return; 
     } 
     } 
     base.WndProc(ref m); 
    } 
    } 
+0

像這種淡藍色的面板,我得到的頂部藍色的醜陋吧。 – C4u 2015-08-14 10:50:11

+5

也許你更喜歡Brushes.Chartreuse。造型當然完全取決於你,而不是寫這樣的代碼。如果你不想照顧它,那麼就不要打擾無邊界的窗口。或聘請設計師。 – 2015-08-14 11:14:49

+3

不需要粗魯。哈芬看到有一個插入的那種顏色的筆刷。您可以將這些信息附加到您的問題上,而不是僅僅進行復制和粘貼。不管怎麼說,還是要謝謝你。用刷子固定了它。 – C4u 2015-08-14 11:25:22

-1

如果您刪除了邊框,您還要刪除內置功能來調整框架的大小。有辦法避開它,但你必須建立自己的功能來拖拽和調整邊框的大小。

1

(詳細說明:在註釋2)

調整大小「形態「來自所有地方並移動它>>>完整代碼< < < < <

//第一U最添加這個類

class ReSize 
    { 

     private bool Above, Right, Under, Left, Right_above, Right_under, Left_under, Left_above; 


     int Thickness=6; //Thickness of border u can cheang it 
     int Area = 8;  //Thickness of Angle border 


     /// <summary> 
     /// Constructor 
     /// </summary> 
     /// <param name="thickness">set thickness of form border</param> 
     public ReSize(int thickness) 
     { 
      Thickness = thickness; 
     } 

     /// <summary> 
     /// Constructor set thickness of form border=1 
     /// </summary> 
     public ReSize() 
     { 
      Thickness = 10; 
     } 

     //Get Mouse Position 
     public string getMosuePosition(Point mouse, Form form) 
     { 
      bool above_underArea = mouse.X > Area && mouse.X < form.ClientRectangle.Width - Area; /* |\AngleArea(Left_Above)\(=======above_underArea========)/AngleArea(Right_Above)/| */ //Area===>(==) 
      bool right_left_Area = mouse.Y > Area && mouse.Y < form.ClientRectangle.Height - Area; 

      bool _Above=mouse.Y <= Thickness; //Mouse in Above All Area 
      bool _Right= mouse.X >= form.ClientRectangle.Width - Thickness; 
      bool _Under=mouse.Y >= form.ClientRectangle.Height - Thickness; 
      bool _Left=mouse.X <= Thickness; 

      Above = _Above && (above_underArea); if (Above) return "a"; /*Mouse in Above All Area WithOut Angle Area */ 
      Right = _Right && (right_left_Area); if (Right) return "r"; 
      Under = _Under && (above_underArea); if (Under) return "u"; 
      Left = _Left && (right_left_Area); if (Left) return "l"; 


      Right_above =/*Right*/ (_Right && (!right_left_Area)) && /*Above*/ (_Above && (!above_underArea)); if (Right_above) return "ra";  /*if Mouse Right_above */ 
      Right_under =/* Right*/((_Right) && (!right_left_Area)) && /*Under*/(_Under && (!above_underArea)); if (Right_under) return "ru";  //if Mouse Right_under 
      Left_under = /*Left*/((_Left) && (!right_left_Area)) && /*Under*/ (_Under && (!above_underArea)); if (Left_under) return "lu";  //if Mouse Left_under 
      Left_above = /*Left*/((_Left) && (!right_left_Area)) && /*Above*/(_Above && (!above_underArea));  if (Left_above) return "la";  //if Mouse Left_above 

      return ""; 

     } 


    } 

進而形成CS

public partial class FormGDI : Form 
    { 

     ReSize resize = new ReSize();  // ReSize Class "/\" To Help Resize Form <None Style> 


     public FormGDI() 
     { 
      InitializeComponent(); 
      this.SetStyle(ControlStyles.ResizeRedraw, true); 

     } 


     private const int cGrip = 16;  // Grip size 
     private const int cCaption = 32; // Caption bar height; 

     protected override void OnPaint(PaintEventArgs e) 
     { 
      //this if you want to draw (if) 

      Color theColor = Color.FromArgb(10, 20, 20, 20); 
      theColor = Color.DarkBlue; 
      int BORDER_SIZE = 4; 
      ControlPaint.DrawBorder(e.Graphics, ClientRectangle, 
             theColor, BORDER_SIZE, ButtonBorderStyle.Dashed, 
             theColor, BORDER_SIZE, ButtonBorderStyle.Dashed, 
             theColor, BORDER_SIZE, ButtonBorderStyle.Dashed, 
             theColor, BORDER_SIZE, ButtonBorderStyle.Dashed); 


      Rectangle rc = new Rectangle(this.ClientSize.Width - cGrip, this.ClientSize.Height - cGrip, cGrip, cGrip); 
      ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc); 
      rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption); 
      e.Graphics.FillRectangle(Brushes.DarkBlue, rc); 



      base.OnPaint(e); 
     } 


     //set MinimumSize to Form 
     public override Size MinimumSize 
     { 
      get 
      { 
       return base.MinimumSize; 
      } 
      set 
      { 
       base.MinimumSize = new Size(179, 51); 
      } 
     } 

     // 
     //override WndProc 
     // 
     protected override void WndProc(ref Message m) 
     { 
      //**************************************************************************** 

      int x = (int)(m.LParam.ToInt64() & 0xFFFF);    //get x mouse position 
      int y = (int)((m.LParam.ToInt64() & 0xFFFF0000) >> 16); //get y mouse position you can gave (x,y) it from "MouseEventArgs" too 
      Point pt = PointToClient(new Point(x, y)); 

      if (m.Msg == 0x84) 
      { 
       switch (resize.getMosuePosition(pt, this)) 
       { 
        case "l": m.Result = (IntPtr)10; return; // the Mouse on Left Form 
        case "r": m.Result = (IntPtr)11; return; // the Mouse on Right Form 
        case "a": m.Result = (IntPtr)12; return; 
        case "la": m.Result = (IntPtr)13; return; 
        case "ra": m.Result = (IntPtr)14; return; 
        case "u": m.Result = (IntPtr)15; return; 
        case "lu": m.Result = (IntPtr)16; return; 
        case "ru": m.Result = (IntPtr)17; return; // the Mouse on Right_Under Form 
        case "": m.Result = pt.Y < 32 /*mouse on title Bar*/ ? (IntPtr)2 : (IntPtr)1; return; 

       } 
      } 

       base.WndProc(ref m); 

     } 

    } 
+0

你能否詳細說明這是如何回答這個問題的?謝謝! – DanM7 2014-09-21 23:10:23

+0

詳細說明:第一個id make resize類它有一個方法來確定鼠標窗體計算位置('point mouse')和(''窗體窗體')來獲取窗體的寬度和高度,然後是最重要的'WndProc' Override方法這個方法有'ref Message m'參數,當'm.msg = 0x84'value你可以設置m.result = 10,這意味着你調用'form'從左邊調整大小,'m.Result = 11'調用'form'從Right和....到17,但是當'm.Result = 2時,你調用移動'form',而當'm.Result = 1'時,這意味着調用'不做任何事情' – NourAldienArabian 2014-09-25 09:41:52

+0

它有幫助,但它不會處理滾動,它會在滾動時通過表單創建新的邊框「線條」 – CularBytes 2014-12-10 22:18:35

4

下面是與調整的全部8個點的自定義格式的完整的例子:

public partial class Form1 : Form { 
public Form1() { 
    InitializeComponent(); 
    this.FormBorderStyle = FormBorderStyle.None; // no borders 
    this.DoubleBuffered = true; 
    this.SetStyle(ControlStyles.ResizeRedraw, true); // this is to avoid visual artifacts 
} 

protected override void OnPaint(PaintEventArgs e) // you can safely omit this method if you want 
{ 
    e.Graphics.FillRectangle(Brushes.Green, Top); 
    e.Graphics.FillRectangle(Brushes.Green, Left); 
    e.Graphics.FillRectangle(Brushes.Green, Right); 
    e.Graphics.FillRectangle(Brushes.Green, Bottom); 
} 

private const int 
    HTLEFT = 10, 
    HTRIGHT = 11, 
    HTTOP = 12, 
    HTTOPLEFT = 13, 
    HTTOPRIGHT = 14, 
    HTBOTTOM = 15, 
    HTBOTTOMLEFT = 16, 
    HTBOTTOMRIGHT = 17; 

const int _ = 10; // you can rename this variable if you like 

Rectangle Top { get { return new Rectangle(0, 0, this.ClientSize.Width, _); } } 
Rectangle Left { get { return new Rectangle(0, 0, _, this.ClientSize.Height); } } 
Rectangle Bottom { get { return new Rectangle(0, this.ClientSize.Height - _, this.ClientSize.Width, _); } } 
Rectangle Right { get { return new Rectangle(this.ClientSize.Width - _, 0, _, this.ClientSize.Height); } } 

Rectangle TopLeft { get { return new Rectangle(0, 0, _, _); } } 
Rectangle TopRight { get { return new Rectangle(this.ClientSize.Width - _, 0, _, _); } } 
Rectangle BottomLeft { get { return new Rectangle(0, this.ClientSize.Height - _, _, _); } } 
Rectangle BottomRight { get { return new Rectangle(this.ClientSize.Width - _, this.ClientSize.Height - _, _, _); } } 


protected override void WndProc(ref Message message) 
{ 
    base.WndProc(ref message); 

    if (message.Msg == 0x84) // WM_NCHITTEST 
    { 
     var cursor = this.PointToClient(Cursor.Position); 

     if (TopLeft.Contains(cursor)) message.Result = (IntPtr)HTTOPLEFT; 
    else if (TopRight.Contains(cursor)) message.Result = (IntPtr)HTTOPRIGHT; 
    else if (BottomLeft.Contains(cursor)) message.Result = (IntPtr)HTBOTTOMLEFT; 
    else if (BottomRight.Contains(cursor)) message.Result = (IntPtr)HTBOTTOMRIGHT; 

    else if (Top.Contains(cursor)) message.Result = (IntPtr)HTTOP; 
    else if (Left.Contains(cursor)) message.Result = (IntPtr)HTLEFT; 
    else if (Right.Contains(cursor)) message.Result = (IntPtr)HTRIGHT; 
    else if (Bottom.Contains(cursor)) message.Result = (IntPtr)HTBOTTOM; 
    } 
}} 
1

「儀」是在右下角 enter image description here

int Mx; 
    int My; 
    int Sw; 
    int Sh; 

    bool mov; 

    void SizerMouseDown(object sender, MouseEventArgs e) 
    { 
     mov = true; 
     My = MousePosition.Y; 
     Mx = MousePosition.X; 
     Sw = Width; 
     Sh = Height; 
    } 

    void SizerMouseMove(object sender, MouseEventArgs e) 
    { 
     if (mov == true) { 
      Width = MousePosition.X - Mx + Sw; 
      Height = MousePosition.Y - My + Sh; 
     } 
    } 

    void SizerMouseUp(object sender, MouseEventArgs e) 
    { 
     mov = false; 
    } 
相關問題