2010-10-25 90 views
2

我將一個winform作爲對話框顯示(在主窗口上使用ShowDialog)。 因此,我將FormBorderStyle設置爲None,因爲我既不需要控制箱也不需要標題欄。 雖然,我想繪製一個邊框(例如像普通窗口一樣的藍色邊框)並保留移動窗體的能力。 我不需要調整它的大小。 我試圖通過重寫OnPaint來繪製邊框,但它從不被調用。 這裏是我的代碼:當FormBorderStyle設置爲None時,winforms繪製邊框並移動。

protected override void OnPaint (PaintEventArgs e) 
    { 
    base.OnPaint (e); 
    int borderWidth = 2; 
    Color borderColor = Color.Blue; 
    ControlPaint.DrawBorder (e.Graphics, e.ClipRectangle, borderColor, 
     borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, 
     ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, 
     borderColor, borderWidth, ButtonBorderStyle.Solid); 
    } 

任何幫助將不勝感激。

+0

請使用可用來標記代碼格式化工具,使得它更容易閱讀的問題這樣... – JohnoBoy 2010-10-25 09:24:41

+0

你的代碼工作正常,當我把它貼到表單中。 – 2010-10-25 09:43:02

+0

我想代碼是好的,它只是不叫! – Pierre 2010-10-25 10:04:39

回答

2

Paint方法在這裏是錯誤的,因爲它不會繪製窗體的所謂的非客戶區域,例如,邊框和標題欄。

要隱藏標題欄,您需要將ControlBox屬性設置爲false並清除表單的Text屬性。將邊框設置爲FixedDialog以使表單不可重新定義。

要保留無標題欄移動窗體的能力,您需要覆蓋WndProc

protected override void WndProc(ref Message m) 
{ 
    switch (m.Msg) 
    { 
     case 0x84: m.Result = new IntPtr(0x2); 
      return; 
    } 
    base.WndProc(ref m); 
} 

基本上,這是處理WM_NCHITTEST消息,欺騙,說的標準的方式 - 鼠標光標在窗口的標題[返回值0X2],這樣你就可以,即使你單擊移動窗體客戶區並拖動它。

+0

OnPaint方法沒問題,他將FormBorderStyle設置爲None。 – 2010-10-25 09:44:11

+0

謝謝,它有幫助。 – Pierre 2010-10-25 10:12:28

+0

雖然移動的代碼不起作用,但是案例不存在,Result是IntPtr,它不能被分配爲0x2。此外,我想改變邊界的大小和顏色。 – Pierre 2010-10-25 10:13:58

0

由於沒有更多信息可用,我將按照建議離開邊界,將其設置爲FixedDialog,將ControlBox屬性設置爲false並清除表單的文本。 我更喜歡邊框的另一種顏色,以及移動窗口的能力。 無論如何非常感謝答案。

+0

要有一個自定義的邊框,你需要做你自己的非客戶區域計算和繪畫。讓我說,使用託管代碼時有點複雜。也許最好將邊框設置爲None,然後使用代碼或其他(DrawRect或其他)中列出的函數來繪製「邊框」(這將是一個假的,看起來只是一個樣子)。 – liggett78 2010-11-07 15:21:21

1

我的問題是有一個薄邊框可調整大小的窗體。

我設置FormBorderStyle爲None

我用一個停靠面板誰包含我的所有控件。

我使用面板填充來設置我的邊框寬度。

然後:

Point ResizeLocation = Point.Empty; 
     void panResize_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
     { 
      if (e.Button == MouseButtons.Left) { 
       ResizeLocation = e.Location; 
       ResizeLocation.Offset(-panResize.Width, -panResize.Height); 
       if (!(ResizeLocation.X > -16 || ResizeLocation.Y > -16)) 
        ResizeLocation = Point.Empty; 
      } 
      else 
       ResizeLocation = Point.Empty; 
     } 
     void panResize_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) 
     { 
      if (e.Button == MouseButtons.Left && !ResizeLocation.IsEmpty) { 
       if (panResize.Cursor == Cursors.SizeNWSE) 
        Size = new Size(e.Location.X - ResizeLocation.X, e.Location.Y - ResizeLocation.Y); 
       else if (panResize.Cursor == Cursors.SizeWE) 
        Size = new Size(e.Location.X - ResizeLocation.X, Size.Height); 
       else if (panResize.Cursor == Cursors.SizeNS) 
        Size = new Size(Size.Width, e.Location.Y - ResizeLocation.Y); 
      } 
      else if (e.X - panResize.Width > -16 && e.Y - panResize.Height > -16) 
       panResize.Cursor = Cursors.SizeNWSE; 
      else if (e.X - panResize.Width > -16) 
       panResize.Cursor = Cursors.SizeWE; 
      else if (e.Y - panResize.Height > -16) 
       panResize.Cursor = Cursors.SizeNS; 
      else { 
       panResize.Cursor = Cursors.Default; 
      } 

     } 

     void panResize_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) 
     { 
      ResizeLocation = Point.Empty; 
     } 
+0

我改變了你的代碼,併爲我工作。感謝分享! – MiBol 2016-11-18 18:13:44

相關問題