2014-12-02 71 views
1
public Form1() 
    { 
     InitializeComponent(); 
     SetStyle(ControlStyles.UserPaint, true); 
     SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
     SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 
     SetStyle(ControlStyles.ResizeRedraw, true); 
     SetStyle(ControlStyles.UserMouse, true); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     this.SetStyle(ControlStyles.UserPaint, true); 
     this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
     this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 


     //this.SetStyle(ControlStyles.UserPaint, true); 
     //this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
     //this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 


     this.DoubleBuffered = true; 

     Rect1LT.Width = 10; 
     Rect1LT.Height = 10; 
     Rect1L.Width = 10; 
     Rect1L.Height = 10; 
     Rect1RT.Width = 10; 
     Rect1RT.Height = 10; 
     Rect1B.Width = 10; 
     Rect1B.Height = 10; 
     Rect1RB.Width = 10; 
     Rect1RB.Height = 10; 
     Rect1R.Width = 10; 
     Rect1R.Height = 10; 
     Rect1LB.Width = 10; 
     Rect1LB.Height = 10; 
     Rect1T.Width = 10; 
     Rect1T.Height = 10; 

     Rect1.X = panel1.Location.X + 50; 
     Rect1.Y = panel1.Location.Y + 50; 
     Rect1.Width = 100; 
     Rect1.Height = 100; 

     Rect1LT.X = Rect1.X - 5; 
     Rect1LT.Y = Rect1.Y - 5; 

     Rect1RT.X = Rect1.X - 5 + Rect1.Width; 
     Rect1RT.Y = Rect1.Y - 5; 

     Rect1LB.X = Rect1.X - 5; 
     Rect1LB.Y = Rect1.Y - 5 + Rect1.Height; 

     Rect1RB.X = Rect1.X - 5 + Rect1.Width; 
     Rect1RB.Y = Rect1.Y - 5 + Rect1.Height; 

     Rect1T.X = Rect1.X - 5 + (Rect1.Width/2); 
     Rect1T.Y = Rect1.Y - 5; 

     Rect1B.X = Rect1.X - 5 + (Rect1.Width/2); 
     Rect1B.Y = Rect1.Y - 5 + Rect1.Height; 

     Rect1L.X = Rect1.X - 5; 
     Rect1L.Y = Rect1.Y - 5 + (Rect1.Height/2); 

     Rect1R.X = Rect1.X - 5 + Rect1.Width; 
     Rect1R.Y = Rect1.Y - 5 + (Rect1.Height/2); 



    } 

    private void panel1_Paint(object sender, PaintEventArgs e) 
    { 


    Pen mypen = default(Pen); 
    mypen = new Pen(System.Drawing.Color.Red, 3); 
    mypen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; 
    //For Dash Line in Rectangle 
    Pen mypen1 = default(Pen); 
    mypen1 = new Pen(System.Drawing.Color.Blue, 1); 
    mypen1.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; 

    L1 = Rect1LT.X + 5; 
    T1 = Rect1LT.Y + 5; 
    W1 = Rect1RB.X - Rect1LT.X; 
    H1 = Rect1RB.Y - Rect1LT.Y; 
    e.Graphics.DrawRectangle(mypen, new System.Drawing.Rectangle(L1, T1, W1, H1)); 
    e.Graphics.DrawRectangle(mypen1, new System.Drawing.Rectangle(L1, T1, W1, H1)); 

    e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1LT.X, Rect1LT.Y, 10, 10)); 
    e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1RT.X, Rect1RT.Y, 10, 10)); 
    e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1LB.X, Rect1LB.Y, 10, 10)); 
    e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1RB.X, Rect1RB.Y, 10, 10)); 

    e.Graphics.FillRectangle(Brushes.Blue, Rect1LT); 
    e.Graphics.FillRectangle(Brushes.Blue, Rect1RT); 
    e.Graphics.FillRectangle(Brushes.Blue, Rect1LB); 
    e.Graphics.FillRectangle(Brushes.Blue, Rect1RB); 

    e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1T.X, Rect1T.Y, 10, 10)); 
    e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1R.X, Rect1R.Y, 10, 10)); 
    e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1B.X, Rect1B.Y, 10, 10)); 
    e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1L.X, Rect1L.Y, 10, 10)); 

    e.Graphics.FillRectangle(Brushes.Blue, Rect1T); 
    e.Graphics.FillRectangle(Brushes.Blue, Rect1R); 
    e.Graphics.FillRectangle(Brushes.Blue, Rect1B); 
    e.Graphics.FillRectangle(Brushes.Blue, Rect1L); 


} 

int L1; 
int T1; 
int W1; 
int H1; 
bool flg1_LT = false; 
bool flg1_RT = false; 
bool flg1_LB = false; 
bool flg1_RB = false; 
bool flg1_mid = false; 
bool flg1_T = false; 
bool flg1_B = false; 
bool flg1_L = false; 

bool flg1_R = false; 
public System.Drawing.Rectangle Rect1; 
public System.Drawing.Rectangle Rect1LT; 
public System.Drawing.Rectangle Rect1RT; 
public System.Drawing.Rectangle Rect1LB; 
public System.Drawing.Rectangle Rect1RB; 
public System.Drawing.Rectangle Rect1T; 
public System.Drawing.Rectangle Rect1B; 
public System.Drawing.Rectangle Rect1L; 

public System.Drawing.Rectangle Rect1R; 
public Point Point1LT; 
public Point Point1RT; 
public Point Point1LB; 
public Point Point1RB; 
public Point Point1L; 
public Point Point1T; 
public Point Point1R; 

public Point Point1B; 

private void panel1_MouseDown(object sender, MouseEventArgs e) 
{ 
    if (e.Location.X >= Rect1LT.X & e.Location.Y >= Rect1LT.Y & e.Location.X <= (Rect1LT.X + 10) & e.Location.Y <= (Rect1LT.Y + 10)) 
    { 
     flg1_LT = true; 
    } 
    else if (e.Location.X >= Rect1RT.X & e.Location.Y >= Rect1RT.Y & e.Location.X <= (Rect1RT.X + 10) & e.Location.Y <= (Rect1RT.Y + 10)) 
    { 
     flg1_RT = true; 
    } 
    else if (e.Location.X >= Rect1LB.X & e.Location.Y >= Rect1LB.Y & e.Location.X <= (Rect1LB.X + 10) & e.Location.Y <= (Rect1LB.Y + 10)) 
    { 
     flg1_LB = true; 
    } 
    else if (e.Location.X >= Rect1RB.X & e.Location.Y >= Rect1RB.Y & e.Location.X <= (Rect1RB.X + 10) & e.Location.Y <= (Rect1RB.Y + 10)) 
    { 
     flg1_RB = true; 
    } 
    else if (e.Location.X >= Rect1B.X & e.Location.Y >= Rect1B.Y & e.Location.X <= (Rect1B.X + 10) & e.Location.Y <= (Rect1B.Y + 10)) 
    { 
     flg1_B = true; 
    } 
    else if (e.Location.X >= Rect1R.X & e.Location.Y >= Rect1R.Y & e.Location.X <= (Rect1R.X + 10) & e.Location.Y <= (Rect1R.Y + 10)) 
    { 
     flg1_R = true; 
    } 
    else if (e.Location.X >= Rect1T.X & e.Location.Y >= Rect1T.Y & e.Location.X <= (Rect1T.X + 10) & e.Location.Y <= (Rect1T.Y + 10)) 
    { 
     flg1_T = true; 
    } 
    else if (e.Location.X >= Rect1L.X & e.Location.Y >= Rect1L.Y & e.Location.X <= (Rect1L.X + 10) & e.Location.Y <= (Rect1L.Y + 10)) 
    { 
     flg1_L = true; 
    } 
    else if (e.Location.X >= (Rect1LT.X + 10) & e.Location.Y >= (Rect1LT.Y + 10) & e.Location.X <= (Rect1RB.X + 10) & e.Location.Y <= (Rect1RB.Y + 10)) 
    { 
     flg1_mid = true; 

     temp1X = e.Location.X; 
     temp1Y = e.Location.Y; 

     Point1L.X = Rect1L.X; 
     Point1T.X = Rect1T.X; 
     Point1B.X = Rect1B.X; 
     Point1R.X = Rect1R.X; 
     Point1L.Y = Rect1L.Y; 
     Point1T.Y = Rect1T.Y; 
     Point1B.Y = Rect1B.Y; 
     Point1R.Y = Rect1R.Y; 
     Point1LT.X = Rect1LT.X; 
     Point1LB.X = Rect1LB.X; 
     Point1RT.X = Rect1RT.X; 
     Point1RB.X = Rect1RB.X; 
     Point1LT.Y = Rect1LT.Y; 
     Point1LB.Y = Rect1LB.Y; 
     Point1RT.Y = Rect1RT.Y; 
     Point1RB.Y = Rect1RB.Y; 

     this.panel1.Cursor = System.Windows.Forms.Cursors.SizeAll; 
} 


} 
int temp1X; 
int temp1Y; 
private void panel1_MouseMove(object sender, MouseEventArgs e) 
{ 
    System.Drawing.Rectangle Rect_TEMP1 = default(System.Drawing.Rectangle); 
    System.Drawing.Rectangle Rect_TEMP2 = default(System.Drawing.Rectangle); 
    System.Drawing.Rectangle Rect_TEMP3 = default(System.Drawing.Rectangle); 
    System.Drawing.Rectangle Rect_TEMP4 = default(System.Drawing.Rectangle); 
    System.Drawing.Rectangle Rect_TEMP5 = default(System.Drawing.Rectangle); 
    System.Drawing.Rectangle Rect_TEMP6 = default(System.Drawing.Rectangle); 

    if (e.Button == MouseButtons.Left) 
    { 
     Rect1T.X = ((Rect1LT.X + Rect1RB.X)/2); 
     Rect1L.Y = (Rect1LT.Y + Rect1LB.Y)/2; 
     Rect1R.Y = (Rect1LT.Y + Rect1LB.Y)/2; 
     Rect1B.X = ((Rect1LT.X + Rect1RB.X)/2); 


     if (flg1_LT) 
     { 
      if (e.Location.X > Rect1LT.X) 
      { 
       Rect1LT.X = e.Location.X; 
       Rect1LB.X = e.Location.X; 
       Rect1L.X = e.Location.X; 
       Rect1T.X = ((Rect1LT.X + Rect1RB.X)/2); 
      } 
      if (e.Location.Y > Rect1LT.Y) 
      { 
       Rect1LT.Y = e.Location.Y; 
       Rect1RT.Y = e.Location.Y; 
       Rect1T.Y = e.Location.Y; 
       Rect1L.Y = (Rect1LT.Y + Rect1LB.Y)/2; 
      } 
      if (e.Location.X < Rect1LT.X) 
      { 
       Rect1LT.X = e.Location.X; 
       Rect1LB.X = e.Location.X; 
       Rect1L.X = e.Location.X; 
       Rect1T.X = ((Rect1LT.X + Rect1RB.X)/2); 
      } 
      if (e.Location.Y < Rect1LT.Y) 
      { 
       Rect1LT.Y = e.Location.Y; 
       Rect1RT.Y = e.Location.Y; 
       Rect1T.Y = e.Location.Y; 
       Rect1L.Y = (Rect1LT.Y + Rect1LB.Y)/2; 
      } 



     } 
     else if (flg1_RT) 
     { 
      if (e.Location.X > Rect1RT.X) 
      { 
       Rect1RT.X = e.Location.X; 
       Rect1RB.X = e.Location.X; 
       Rect1R.X = e.Location.X; 
       Rect1T.X = ((Rect1LT.X + Rect1RB.X)/2); 
      } 
      if (e.Location.Y > Rect1RT.Y) 
      { 
       Rect1RT.Y = e.Location.Y; 
       Rect1LT.Y = e.Location.Y; 
       Rect1T.Y = e.Location.Y; 
       Rect1R.Y = (Rect1LT.Y + Rect1LB.Y)/2; 
      } 
      if (e.Location.X < Rect1RT.X) 
      { 
       Rect1RT.X = e.Location.X; 
       Rect1RB.X = e.Location.X; 
       Rect1R.X = e.Location.X; 
       Rect1T.X = ((Rect1LT.X + Rect1RB.X)/2); 
      } 
      if (e.Location.Y < Rect1RT.Y) 
      { 
       Rect1RT.Y = e.Location.Y; 
       Rect1LT.Y = e.Location.Y; 
       Rect1T.Y = e.Location.Y; 
       Rect1R.Y = (Rect1LT.Y + Rect1LB.Y)/2; 
      } 


     } 
     else if (flg1_LB) 
     { 
      if (e.Location.X > Rect1LB.X) 
      { 
       Rect1LB.X = e.Location.X; 
       Rect1LT.X = e.Location.X; 
       Rect1L.X = e.Location.X; 
       Rect1B.X = ((Rect1LT.X + Rect1RB.X)/2); 
       Rect1T.X = ((Rect1LT.X + Rect1RB.X)/2); 
      } 
      if (e.Location.Y > Rect1LB.Y) 
      { 
       Rect1LB.Y = e.Location.Y; 
       Rect1RB.Y = e.Location.Y; 
       Rect1B.Y = e.Location.Y; 
       Rect1L.Y = (Rect1LT.Y + Rect1LB.Y)/2; 
      } 
      if (e.Location.X < Rect1LB.X) 
      { 
       Rect1LB.X = e.Location.X; 
       Rect1LT.X = e.Location.X; 
       Rect1L.X = e.Location.X; 
       Rect1B.X = ((Rect1LT.X + Rect1RB.X)/2); 
       Rect1T.X = ((Rect1LT.X + Rect1RB.X)/2); 
      } 
      if (e.Location.Y < Rect1LB.Y) 
      { 
       Rect1LB.Y = e.Location.Y; 
       Rect1RB.Y = e.Location.Y; 
       Rect1B.Y = e.Location.Y; 
       Rect1L.Y = (Rect1LT.Y + Rect1LB.Y)/2; 
      } 


     } 
     else if (flg1_RB) 
     { 
      if (e.Location.X > Rect1RB.X) 
      { 
       Rect1RB.X = e.Location.X; 
       Rect1RT.X = e.Location.X; 
       Rect1R.X = e.Location.X; 
       Rect1B.X = ((Rect1LT.X + Rect1RB.X)/2); 
       Rect1T.X = ((Rect1LT.X + Rect1RB.X)/2); 
      } 
      if (e.Location.Y > Rect1RB.Y) 
      { 
       Rect1RB.Y = e.Location.Y; 
       Rect1LB.Y = e.Location.Y; 
       Rect1B.Y = e.Location.Y; 
       Rect1R.Y = (Rect1LT.Y + Rect1LB.Y)/2; 
      } 
      if (e.Location.X < (Rect1RB.X + 10)) 
      { 
       Rect1RB.X = e.Location.X; 
       Rect1RT.X = e.Location.X; 
       Rect1R.X = e.Location.X; 
       Rect1B.X = ((Rect1LT.X + Rect1RB.X)/2); 
       Rect1T.X = ((Rect1LT.X + Rect1RB.X)/2); 
      } 
      if (e.Location.Y < (Rect1RB.Y + 10)) 
      { 
       Rect1RB.Y = e.Location.Y; 
       Rect1LB.Y = e.Location.Y; 
       Rect1B.Y = e.Location.Y; 
       Rect1R.Y = (Rect1LT.Y + Rect1LB.Y)/2; 
      } 


     } 
     else if (flg1_B) 
     { 
      if (e.Location.Y > Rect1B.Y) 
      { 
       Rect1B.Y = e.Location.Y; 
       Rect1LB.Y = e.Location.Y; 
       Rect1RB.Y = e.Location.Y; 
      } 
      if (e.Location.Y < (Rect1B.Y + 10)) 
      { 
       Rect1B.Y = e.Location.Y; 
       Rect1LB.Y = e.Location.Y; 
       Rect1RB.Y = e.Location.Y; 
      } 


     } 
     else if (flg1_T) 
     { 
      if (e.Location.Y > Rect1T.Y) 
      { 
       Rect1T.Y = e.Location.Y; 
       Rect1LT.Y = e.Location.Y; 
       Rect1RT.Y = e.Location.Y; 
      } 

      if (e.Location.Y < (Rect1T.Y + 10)) 
      { 
       Rect1T.Y = e.Location.Y; 
       Rect1LT.Y = e.Location.Y; 
       Rect1RT.Y = e.Location.Y; 
      } 


     } 
     else if (flg1_R) 
     { 
      if (e.Location.X > Rect1R.X) 
      { 
       Rect1R.X = e.Location.X; 
       Rect1RT.X = e.Location.X; 
       Rect1RB.X = e.Location.X; 
      } 

      if (e.Location.X < (Rect1R.X + 10)) 
      { 
       Rect1R.X = e.Location.X; 
       Rect1RT.X = e.Location.X; 
       Rect1RB.X = e.Location.X; 
      } 


     } 
     else if (flg1_L) 
     { 
      if (e.Location.X > Rect1L.X) 
      { 
       Rect1L.X = e.Location.X; 
       Rect1LT.X = e.Location.X; 
       Rect1LB.X = e.Location.X; 
      } 

      if (e.Location.X < (Rect1L.X + 10)) 
      { 
       Rect1L.X = e.Location.X; 
       Rect1LT.X = e.Location.X; 
       Rect1LB.X = e.Location.X; 
      } 


     } 
     else if (flg1_mid) 
     { 
      Rect1LT.X = Point1LT.X + (e.Location.X - temp1X); 
      Rect1RT.X = Point1RT.X + (e.Location.X - temp1X); 
      Rect1LB.X = Point1LB.X + (e.Location.X - temp1X); 
      Rect1RB.X = Point1RB.X + (e.Location.X - temp1X); 
      Rect1LT.Y = Point1LT.Y + (e.Location.Y - temp1Y); 
      Rect1RT.Y = Point1RT.Y + (e.Location.Y - temp1Y); 
      Rect1LB.Y = Point1LB.Y + (e.Location.Y - temp1Y); 
      Rect1RB.Y = Point1RB.Y + (e.Location.Y - temp1Y); 
      Rect1L.X = Point1L.X + (e.Location.X - temp1X); 
      Rect1R.X = Point1R.X + (e.Location.X - temp1X); 
      Rect1B.X = Point1B.X + (e.Location.X - temp1X); 
      Rect1T.X = Point1T.X + (e.Location.X - temp1X); 
      Rect1L.Y = Point1L.Y + (e.Location.Y - temp1Y); 
      Rect1R.Y = Point1R.Y + (e.Location.Y - temp1Y); 
      Rect1B.Y = Point1B.Y + (e.Location.Y - temp1Y); 
      Rect1T.Y = Point1T.Y + (e.Location.Y - temp1Y); 

     } 

     Rect1T.X = ((Rect1LT.X + Rect1RB.X)/2); 
     Rect1L.Y = (Rect1LT.Y + Rect1LB.Y)/2; 
     Rect1R.Y = (Rect1LT.Y + Rect1LB.Y)/2; 
     Rect1B.X = ((Rect1LT.X + Rect1RB.X)/2); 

     if (Rect1LT.X > Rect1RT.X) 
     { 
      Rect_TEMP1 = Rect1RT; 
      Rect_TEMP2 = Rect1R; 
      Rect_TEMP3 = Rect1RB; 
      Rect1RT = Rect1LT; 
      Rect1R = Rect1L; 
      Rect1RB = Rect1LB; 
      Rect1LT = Rect_TEMP1; 
      Rect1L = Rect_TEMP2; 
      Rect1LB = Rect_TEMP3; 

     } 
     if (Rect1LT.Y > Rect1LB.Y) 
     { 
      Rect_TEMP4 = Rect1LB; 
      Rect_TEMP5 = Rect1B; 
      Rect_TEMP6 = Rect1RB; 
      Rect1LB = Rect1LT; 
      Rect1B = Rect1T; 
      Rect1RB = Rect1RT; 
      Rect1LT = Rect_TEMP4; 
      Rect1T = Rect_TEMP5; 
      Rect1RT = Rect_TEMP6; 
     } 
     this.DoubleBuffered= true; 
     this.SetStyle(ControlStyles.UserPaint | 
     ControlStyles.AllPaintingInWmPaint | 
     ControlStyles.ResizeRedraw | 
     ControlStyles.ContainerControl | 
     ControlStyles.OptimizedDoubleBuffer | 
     ControlStyles.SupportsTransparentBackColor 
     , true); 

     panel1.Invalidate(); 
     //public void Invalidate(Rectangle); 
     //panel1.Update(); 
     //panel1.Refresh(); 
     //panel1.Invalidate(Rect1T); 
     //invalidateRect(Rect_TEMP1); 
     //panel1.Invalidate(Rect_TEMP1); 
     //panel1.Invalidate(Rect_TEMP2); 
     //panel1.Invalidate(Rect_TEMP3); 
     //panel1.Invalidate(Rect_TEMP4); 
     //panel1.Invalidate(Rect_TEMP5); 
     //panel1.Invalidate(Rect_TEMP6); 
     //panel1.Invalidate(Rect1); 
     //panel1.Invalidate(Rect1B); 
     //Invalidate(Rect1L, true); 
     //Invalidate(Rect_TEMP1,true); 

     //panel1.Invalidate(Rect1LB); 
     //panel1.Invalidate(Rect1LT); 
     //panel1.Invalidate(Rect1R); 
     //panel1.Invalidate(Rect1RB); 
     //panel1.Invalidate(Rect1T); 
     //panel1.Invalidate(Rect1RT); 

     //panel1.Invalidate(Rect); 
    } 

    if (e.Location.X >= Rect1LT.X & e.Location.Y >= Rect1LT.Y & e.Location.X <= (Rect1LT.X + 10) & e.Location.Y <= (Rect1LT.Y + 10)) 
    { 
     this.panel1.Cursor = System.Windows.Forms.Cursors.SizeNWSE; 
    } 
    else if (e.Location.X >= Rect1RT.X & e.Location.Y >= Rect1RT.Y & e.Location.X <= (Rect1RT.X + 10) & e.Location.Y <= (Rect1RT.Y + 10)) 
    { 
     this.panel1.Cursor = System.Windows.Forms.Cursors.SizeNESW; 
    } 
    else if (e.Location.X >= Rect1LB.X & e.Location.Y >= Rect1LB.Y & e.Location.X <= (Rect1LB.X + 10) & e.Location.Y <= (Rect1LB.Y + 10)) 
    { 
     this.panel1.Cursor = System.Windows.Forms.Cursors.SizeNESW; 
    } 
    else if (e.Location.X >= Rect1RB.X & e.Location.Y >= Rect1RB.Y & e.Location.X <= (Rect1RB.X + 10) & e.Location.Y <= (Rect1RB.Y + 10)) 
    { 
     this.panel1.Cursor = System.Windows.Forms.Cursors.SizeNWSE; 
    } 
    else if (e.Location.X >= Rect1B.X & e.Location.Y >= Rect1B.Y & e.Location.X <= (Rect1B.X + 10) & e.Location.Y <= (Rect1B.Y + 10)) 
    { 
     this.panel1.Cursor = System.Windows.Forms.Cursors.SizeNS; 
    } 
    else if (e.Location.X >= Rect1R.X & e.Location.Y >= Rect1R.Y & e.Location.X <= (Rect1R.X + 10) & e.Location.Y <= (Rect1R.Y + 10)) 
    { 
     this.panel1.Cursor = System.Windows.Forms.Cursors.SizeWE; 
    } 
    else if (e.Location.X >= Rect1T.X & e.Location.Y >= Rect1T.Y & e.Location.X <= (Rect1T.X + 10) & e.Location.Y <= (Rect1T.Y + 10)) 
    { 
     this.panel1.Cursor = System.Windows.Forms.Cursors.SizeNS; 
    } 
    else if (e.Location.X >= Rect1L.X & e.Location.Y >= Rect1L.Y & e.Location.X <= (Rect1L.X + 10) & e.Location.Y <= (Rect1L.Y + 10)) 
    { 
     this.panel1.Cursor = System.Windows.Forms.Cursors.SizeWE; 
    } 
    else if (e.Location.X >= (Rect1LT.X + 10) & e.Location.Y >= (Rect1LT.Y + 10) & e.Location.X <= (Rect1RB.X + 10) & e.Location.Y <= (Rect1RB.Y + 10)) 
    { 
     this.panel1.Cursor = System.Windows.Forms.Cursors.SizeAll; 
    } 
} 

    private void panel1_MouseUp(object sender, MouseEventArgs e) 
    { 
     flg1_LB = false; 
     flg1_LT = false; 
     flg1_RB = false; 
     flg1_RT = false; 
     temp1X = 0; 
     temp1Y = 0; 
     flg1_B = false; 
     flg1_L = false; 
     flg1_R = false; 
     flg1_T = false; 
     flg1_mid = false; 
    } 
} 

如上所示我繪製矩形,它會導致面板上閃爍? 我也設置雙緩衝屬性爲true,但仍然是零。 我在面板中設置背景圖像,圖像屬性設置爲拉伸,如果圖像屬性是平鋪,它不閃爍。 我應該做什麼改變以避免閃爍。如何避免閃爍使用繪畫方法在面板上繪製矩形?

+0

我沒有仔細看過你的場景,但在構造函數中的SetStyle()調用之後嘗試調用'UpdateStyles();'。 – 2014-12-02 16:26:10

+0

我嘗試過,但仍然閃爍。 – 2014-12-03 04:34:34

回答

0

設置DoubleBuffered屬性不是防止閃爍的魔法。 該屬性只允許一次渲染繪製的曲面,而不是直接渲染漸變,您首先將所有圖形繪製到緩衝區中。然後將其交換到要繪製的有效表面。這對你來說是透明的,但這僅僅是一件事。

如果你仍然有閃爍,你可能會錯誤地訪問你正在繪製的表面。
有在您的程序驅動的圖形操作的方式主要有兩種:

  • 活動消息隊列
  • 遊戲循環

事件消息隊列當圖形是主要用於在由用戶或系統觸發的動作時更新。點擊鼠標,系統通知,按下鍵盤按鍵,... 這是事件驅動的,等待新事件是阻止操作。因此,在處理cpu的每個事件之間可能有足夠的時間執行其他操作。這就是說,如果事件速度低於繪製到曲面所需的時間,則可以執行一些圖形操作,而不會出現太多的性能命中(因此閃爍)。

while (!exit) 
{ 
    var msg = WaitForMessage(); // Blocks until message arrives 
    switch (msg.Type) 
    { 
     case Message.MouseMove: OnMouseMove(msg); break; 
     case Message.Paint: OnPaint(msg); break; 
     ... 
    } 
} 

來源:opentk

遊戲循環是一個更好的方法,當你需要不斷地畫上的圖形表面和/或圖形無需用戶交互進行更新。

while (!exit) 
{ 
    var time = GetTime(); 
    Update(time); 
    Render(time); 
} 

來源:opentk

用這種方法執行非阻塞操作,因此必須處理空閒CPU時間,其他應用程序或你會被消耗的CPU時間的100%。 這是FPS控制的基礎。

您的代碼正在使用第一種方法,因爲在每次鼠標移動事件後,您都會使繪製的曲面無效並請求圖形更新。

這不是一個好方法,因爲與執行繪圖所用的時間相比,鼠標移動事件可能會更快。這可能會降低UI的響應速度。

你可以考慮以下幾點工作:

  • 減少您的圖形更新所需的時間:
    • 避免在你的繪製操作堆新對象的實例化系統。嘗試重新使用固定實例。設置一次使用的樣式和顏色。沒有必要一遍又一遍地重複它。
  • 打破了鼠標移動和圖形失效之間的聯繫:
    • 嘗試創建一個遊戲循環,你的來電無效將在衆所周知率獨立的鼠標移動事件來進行。這將使您能夠控制FPS。
  • 的Invalidate只需要採取什麼無效:
    • 您可以通過重繪只需要執行何種獲得性能。
      使方法無效,接受描述將在繪畫方法上重繪的區域的矩形。這將避免總是重繪不變的像素。

所有這些行動將提高性能和減少閃爍的效果。