2013-11-22 65 views
0

我的問題是,即使當我想要在panel_MouseMove()上移動我繪製的矩形時,所有這些矩形都會閃爍。 我並不是沒有爲什麼。 這裏是我的panel_MouseMove法碼:C#.Net 4.0 panel_MouseMove事件閃爍矩形

private void panel_MouseMove(object sender, MouseEventArgs e) 
    { 
     bool flag = false; 
     if (_guiParserMgr != null) 
     { 
      if (e.Button == MouseButtons.Left) 
      { 
       foreach (GuiModulModel gmm in _guiParserMgr.Moduls) 
       { 
        log.Debug("Modul: XPos:" + gmm.getXPos() + " YPos:" + gmm.getYPos() + " WidthPos:" + ((int)gmm.getXPos() + (int)gmm.getWidth()) + " HeightPos:" + ((int)gmm.getYPos() + (int)gmm.getHeight())); 

        if (flag == false) 
        { 
         foreach (GuiEntityModel gem in gmm.Entitys) 
         { 
          log.Debug("Entity: XPos:" + gem.getXPos() + " YPos:" + gem.getYPos() + " WidthPos:" + ((int)gem.getXPos() + (int)gem.getWidth()) + " HeightPos:" + ((int)gem.getYPos() + (int)gem.getHeight())); 
          if (gem.getXPos() < e.X && gem.getXPos() + gem.getWidth() > e.X && gem.getYPos() < e.X && gem.getYPos() + gem.getHeight() > e.Y) 
          { 

           // Increment rectangle-location by mouse-location delta. 
           gem.setXPos(e.X /*+ _firstPoint.X*/); 
           gem.setYPos(e.Y /*+ _firstPoint.Y*/); 

           // Re-calibrate on each move operation. 
           _firstPoint = new MovePoint { X = e.X, Y = e.Y }; 

           //panel.Invalidate(); 

           foreach (GuiFieldModel gfm in gem.Fields) 
           { 
            // Increment rectangle-location by mouse-location delta. 
            gfm.setXPos(e.X/*+ _firstPoint.X*/); 
            gfm.setYPos(e.Y + 15/*+ _firstPoint.Y*/); 

            // Re-calibrate on each move operation. 
            _firstPoint = new MovePoint { X = e.X, Y = e.Y }; 

            //panel.Invalidate();          
           } 
           flag = true; 
          } 
         } 
        } 
        if (flag == false) 
        { 
         foreach (GuiEnumTypeModel getm in gmm.EnumTypes) 
         { 
          log.Debug("EnumType: XPos:" + getm.getXPos() + " YPos:" + getm.getYPos() + " WidthPos:" + ((int)getm.getXPos() + (int)getm.getWidth()) + " HeightPos:" + ((int)getm.getYPos() + (int)getm.getHeight())); 
          if (getm.getXPos() < e.X && getm.getXPos() + getm.getWidth() > e.X && getm.getYPos() < e.X && getm.getYPos() + getm.getHeight() > e.Y) 
          { 
           // Increment rectangle-location by mouse-location delta. 
           getm.setXPos(e.X /*+ _firstPoint.X*/); 
           getm.setYPos(e.Y /*+ _firstPoint.Y*/); 
           // Re-calibrate on each move operation. 
           _firstPoint = new MovePoint { X = e.X, Y = e.Y }; 

           //panel.Invalidate(); 
           flag = true; 
          } 

         } 
        } 
        if (flag == false) 
        { 

         foreach (GuiServiceModel gsm in gmm.Services) 
         { 
          log.Debug("Service: XPos:" + gsm.getXPos() + " YPos:" + gsm.getYPos() + " WidthPos:" + ((int)gsm.getXPos() + (int)gsm.getWidth()) + " HeightPos:" + ((int)gsm.getYPos() + (int)gsm.getHeight())); 
          if (gsm.getXPos() < e.X && gsm.getXPos() + gsm.getWidth() > e.X && gsm.getYPos() < e.X && gsm.getYPos() + gsm.getHeight() > e.Y) 
          { 

           // Increment rectangle-location by mouse-location delta. 
           gsm.setXPos(e.X /*+ _firstPoint.X*/); 
           gsm.setYPos(e.Y /*+ _firstPoint.Y*/); 

           // Re-calibrate on each move operation. 
           _firstPoint = new MovePoint { X = e.X, Y = e.Y }; 

           //panel.Invalidate(); 
           flag = true; 
          } 

         } 
        } 
        if (flag == false) 
        { 
         foreach (GuiWebMvcModel gwm in gmm.WebMvcs) 
         { 
          log.Debug("WebMVC: XPos:" + gwm.getXPos() + " YPos:" + gwm.getYPos() + " WidthPos:" + ((int)gwm.getXPos() + (int)gwm.getWidth()) + " HeightPos:" + ((int)gwm.getYPos() + (int)gwm.getHeight())); 

          if (gwm.getXPos() < e.X && gwm.getXPos() + gwm.getWidth() > e.X && gwm.getYPos() < e.X && gwm.getYPos() + gwm.getHeight() > e.Y) 
          { 
           // Increment rectangle-location by mouse-location delta. 
           gwm.setXPos(e.X /*+ _firstPoint.X*/); 
           gwm.setYPos(e.Y /*+ _firstPoint.Y*/); 

           // Re-calibrate on each move operation. 
           _firstPoint = new MovePoint { X = e.X, Y = e.Y }; 

           //panel.Invalidate(); 
           flag = true; 
          } 

         } 
        } 
        if (flag == false) 
        { 
         if (gmm.getXPos() < e.X && gmm.getXPos() + gmm.getWidth() > e.X && gmm.getYPos() < e.X && gmm.getYPos() + gmm.getHeight() > e.Y) 
         { 
          // Increment rectangle-location by mouse-location delta. 
          gmm.setXPos(e.X /*+ _firstPoint.X*/); 
          gmm.setYPos(e.Y /*+ _firstPoint.Y*/); 

          // Re-calibrate on each move operation. 
          _firstPoint = new MovePoint { X = e.X, Y = e.Y }; 

          //panel.Invalidate(); 
          flag = true; 

         } 
        } 
        panel.Invalidate(); 




       } 
      } 
     } 
    } 

請幫幫我! :D 謝謝:)

回答

0

我的示例代碼是用於PictureBox,但也適用於其他控件和形狀。它工作無閃爍。

using System; 
using System.Drawing; 
using System.Windows.Forms; 

namespace BigBrother 
{ 
    public class SizeablePictureBox : PictureBox 
    { 

     private const int grab = 16; 
     public Point PBstartLocation; 
     public Point PBcornerStart; 

     public SizeablePictureBox() 
     { 
      this.ResizeRedraw = true; 
     } 


     protected override void OnPaint(PaintEventArgs e) 
     { 
      base.OnPaint(e); 
      var rc = new Rectangle(this.ClientSize.Width - grab, this.ClientSize.Height - grab, grab, grab); 
      ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc); 
     } 

     protected override void WndProc(ref Message m) 
     { 
      base.WndProc(ref m); 
      if (m.Msg == 0x84) 
      { 

       var pos = this.PointToClient(new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16)); 
       if (pos.X >= this.ClientSize.Width - grab && pos.Y >= this.ClientSize.Height - grab) 
        m.Result = new IntPtr(17); 
      } 
     } 


     protected override void OnMouseDown(MouseEventArgs e) 
     { 
      if (e.Button == System.Windows.Forms.MouseButtons.Left) 
      { 
       PBstartLocation = e.Location; 
       PBcornerStart = this.Location; 
       this.BringToFront(); 
      } 

     } 


     protected override void OnMouseMove(MouseEventArgs e) 
     { 
      if (e.Button == System.Windows.Forms.MouseButtons.Left) 
      { 
       this.Left = e.X + this.Left - PBstartLocation.X; 
       this.Top = e.Y + this.Top - PBstartLocation.Y; 
      } 
     } 
    } 

最好的例子,從鏈接的評論使:https://mega.co.nz/#!cghECSLZ!KVzvbzkSPmuonuBVTsRok1Milz1cbbGW9bJEmTQQnsY

+0

我有一個問題,怎麼會是你的圖片框是一個沒有無效媒體鏈接drawed()? 當我刪除我的panel.invalidate()行,然後想要移動我的矩形,矩形不會移動。 – ActedLasmiranda

+0

@ActionLasmiranda這段代碼是在picturebox類本身寫入的:這是一個擴展PictureBox的控件。我將編輯我的代碼示例以使其更清晰。 –

+0

好的,謝謝,你可能有一個例子,你畫了一個相當大的矩形? – ActedLasmiranda