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,但仍然是零。 我在面板中設置背景圖像,圖像屬性設置爲拉伸,如果圖像屬性是平鋪,它不閃爍。 我應該做什麼改變以避免閃爍。如何避免閃爍使用繪畫方法在面板上繪製矩形?
我沒有仔細看過你的場景,但在構造函數中的SetStyle()調用之後嘗試調用'UpdateStyles();'。 – 2014-12-02 16:26:10
我嘗試過,但仍然閃爍。 – 2014-12-03 04:34:34