2011-06-29 49 views
4

我有一個帶兩個面板的SplitContainer。當我拖動分隔線來調整兩個面板的大小時,我拖着一個灰色的條。在釋放鼠標按鈕之前,面板實際上不會重新繪製。我如何使面板刷新我拖動分離器?隨着分離器移動,刷新SplitContainer的面板

Fwiw,這是通過將分離器控件的「ResizeStyle」設置爲「rsUpdate」在Delphi中實現的。

我曾嘗試將下面的代碼放在SplitterMoving事件中,但沒有可見的更改。

private void splitCont_SplitterMoving(object sender, SplitterCancelEventArgs e) 
{ 
    splitCont.Invalidate(); 
    //also tried this: 
    //splitCont.Refresh(); 
} 

回答

10

你可以嘗試使用鼠標事件詳細in this page

//assign this to the SplitContainer's MouseDown event 
    private void splitCont_MouseDown(object sender, MouseEventArgs e) 
    { 
     // This disables the normal move behavior 
     ((SplitContainer)sender).IsSplitterFixed = true; 
    } 

    //assign this to the SplitContainer's MouseUp event 
    private void splitCont_MouseUp(object sender, MouseEventArgs e) 
    { 
     // This allows the splitter to be moved normally again 
     ((SplitContainer)sender).IsSplitterFixed = false; 
    } 

    //assign this to the SplitContainer's MouseMove event 
    private void splitCont_MouseMove(object sender, MouseEventArgs e) 
    { 
     // Check to make sure the splitter won't be updated by the 
     // normal move behavior also 
     if (((SplitContainer)sender).IsSplitterFixed) 
     { 
      // Make sure that the button used to move the splitter 
      // is the left mouse button 
      if (e.Button.Equals(MouseButtons.Left)) 
      { 
       // Checks to see if the splitter is aligned Vertically 
       if (((SplitContainer)sender).Orientation.Equals(Orientation.Vertical)) 
       { 
        // Only move the splitter if the mouse is within 
        // the appropriate bounds 
        if (e.X > 0 && e.X < ((SplitContainer)sender).Width) 
        { 
         // Move the splitter & force a visual refresh 
         ((SplitContainer)sender).SplitterDistance = e.X; 
         ((SplitContainer)sender).Refresh(); 
        } 
       } 
       // If it isn't aligned vertically then it must be 
       // horizontal 
       else 
       { 
        // Only move the splitter if the mouse is within 
        // the appropriate bounds 
        if (e.Y > 0 && e.Y < ((SplitContainer)sender).Height) 
        { 
         // Move the splitter & force a visual refresh 
         ((SplitContainer)sender).SplitterDistance = e.Y; 
         ((SplitContainer)sender).Refresh(); 
        } 
       } 
      } 
      // If a button other than left is pressed or no button 
      // at all 
      else 
      { 
       // This allows the splitter to be moved normally again 
       ((SplitContainer)sender).IsSplitterFixed = false; 
      } 
     } 
    } 
+2

這個工作!作爲參考,我將編輯您的答案以包含實際的代碼。謝謝! – JosephStyons

+1

我這樣做,但我subclassed'SplitContainer'的可重用性(和使用'Invalidate()',而不是'刷新()')。謝謝! –

+0

這個答案仍然像一個魅力。更有吸引力的分離器運動。 – DonBoitnott