2017-04-19 45 views
0

我有2個表(母公司&子)
子窗體的屬性是ControlBox:FalseDoubleBuffer:True & FormBorderStyle:SizeableToolWindow(其餘全部爲默認值)。
我想子窗體從心的大小(而不是從左上方)調整大小從中心[的WinForms]

在子窗體Resize事件,我有以下代碼

this.Location = System.Drawing.Point(x/2 - this.Width/2, y/2 - this.Height/2); 
//tried this.CenterToParent();` 

其中x =父窗體的寬度和y =父窗體的高度。

現在在從父母形式顯示子形式後,在調整大小後閃爍很多,子形式往往會回到原來的位置!
如何從中心創建平滑調整大小?
有沒有辦法將Form的調整大小原點改爲中心?
這個問題已經貼here,但不明白的解決方案

+1

該解決方案已經給你所需要的,如果你不明白你不會理解這裏發佈的任何解決方案都是一樣的。 – Gusman

+0

您找到了解決方案。這是問題的核心:覆蓋該窗口的WinProc,然後'吃'該SDK消息。 –

+0

@Dysmondad,謝謝你的回覆。我是C#和winform的新手。所以我不知道什麼是WinProc,如何處理SDK消息e.t.c –

回答

1
using System; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     protected override void OnResize(EventArgs e) 
     { 
      this.Location = new System.Drawing.Point(this.Location.X/2 - this.Width/2, this.Location.Y/2 - this.Height/2); 
      //tried this.CenterToParent();` 
      base.OnResize(e); 
     } 

     const int WM_SYSCOMMAND = 0x112; 
     const int SC_MAXIMIZE = 0xF030; 
     const int SC_MAXIMIZE2 = 0xF032; 

     protected override void WndProc(ref Message m) 
     { 
      if ((m.Msg == WM_SYSCOMMAND && m.WParam == new IntPtr(SC_MAXIMIZE)) || m.WParam == new IntPtr(SC_MAXIMIZE2)) 
      { 
       this.Size = this.MaximumSize; 
       m.Result = new IntPtr(0); 
       //base.WndProc(ref m); 
       //Eat the message so won't process further 
      } 
      else 
      { 
       m.Result = new IntPtr(0); 
       base.WndProc(ref m); 
      } 
     } 
    } 
} 

這裏有更好的鏈接: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.wndproc(v=vs.110).aspx

+0

感謝您的回答和參考鏈接。我試過你的代碼。在調整大小的同時,由於某種原因,形式仍舊在舊位置閃爍! –

+0

@Prakash M,這只是爲了展示如何添加覆蓋。這是否在你的代碼中工作? –

+0

它的工作原理(調整大小發生在中心),但如何避免閃爍?你能試試你的目的嗎? –

相關問題