Screen Flickers When I Move Mouse
-
Hi, I am writing some code (for fun) that will display the mouse coordinates in real time on a WinForm. The "e.X, e.Y" coordinate text is displayed next to the position of the cursor. Problem is, every time I move the mouse the graph that I've drawn to "run" the coordinates over keeps getting re-drawn (flicker?) until I stop moving the mouse-- and then the coordinates are displayed over the graph I've drawn. Is there any way to fix this? ps- many people have very helpful to my questions --> I am very grateful. //////////////////////////////////////////////////////////// Here are the relevant code fragments....1)Paint, 2)MouseMove, 3)override OnPaint /////////////////////////////////////////////////////////// void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.DrawRectangle(graphPen, xIndent, yIndent, 400,400); g.FillRectangle(graphBrush, xIndent, yIndent, 400, 400); g.DrawLine(graphPen, xIndent + 200, yIndent, xIndent + 200, yIndent + 400); g.DrawLine(graphPen, xIndent, yIndent + 200, xIndent + 400, yIndent + 200); DrawGraphLines(g); DrawXYAxes(g); if(m_bFlag1) DrawComplexLineA(g); if(m_bFlag2) DrawComplexLineB(g); if(m_bAddFlag) DrawComplexAdd(g); } /////////////////////////////////////////////////////////////////////////// private Point mousePoints; protected override void OnMouseMove(MouseEventArgs e) { // TODO: Add Form1.OnMouseMove implementation mousePoints = new Point(e.X,e.Y); Invalidate(); base.OnMouseMove (e); } //////////////////////////////////////////////////////////////////////////// protected override void OnPaint(PaintEventArgs e) { // TODO: Add Form1.OnPaint implementation base.OnPaint (e); Point screen = PointToScreen(mousePoints); string location = screen.ToString(); e.Graphics.DrawString("[Screen: " + location + "]", new Font("Arial",8), SystemBrushes.ControlText, (PointF)mousePoints); } /////////////////////////////////////////////////////////
-
Hi, I am writing some code (for fun) that will display the mouse coordinates in real time on a WinForm. The "e.X, e.Y" coordinate text is displayed next to the position of the cursor. Problem is, every time I move the mouse the graph that I've drawn to "run" the coordinates over keeps getting re-drawn (flicker?) until I stop moving the mouse-- and then the coordinates are displayed over the graph I've drawn. Is there any way to fix this? ps- many people have very helpful to my questions --> I am very grateful. //////////////////////////////////////////////////////////// Here are the relevant code fragments....1)Paint, 2)MouseMove, 3)override OnPaint /////////////////////////////////////////////////////////// void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.DrawRectangle(graphPen, xIndent, yIndent, 400,400); g.FillRectangle(graphBrush, xIndent, yIndent, 400, 400); g.DrawLine(graphPen, xIndent + 200, yIndent, xIndent + 200, yIndent + 400); g.DrawLine(graphPen, xIndent, yIndent + 200, xIndent + 400, yIndent + 200); DrawGraphLines(g); DrawXYAxes(g); if(m_bFlag1) DrawComplexLineA(g); if(m_bFlag2) DrawComplexLineB(g); if(m_bAddFlag) DrawComplexAdd(g); } /////////////////////////////////////////////////////////////////////////// private Point mousePoints; protected override void OnMouseMove(MouseEventArgs e) { // TODO: Add Form1.OnMouseMove implementation mousePoints = new Point(e.X,e.Y); Invalidate(); base.OnMouseMove (e); } //////////////////////////////////////////////////////////////////////////// protected override void OnPaint(PaintEventArgs e) { // TODO: Add Form1.OnPaint implementation base.OnPaint (e); Point screen = PointToScreen(mousePoints); string location = screen.ToString(); e.Graphics.DrawString("[Screen: " + location + "]", new Font("Arial",8), SystemBrushes.ControlText, (PointF)mousePoints); } /////////////////////////////////////////////////////////
Turn on double buffering: this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.ResizeRedraw, true); Christian Graus - Microsoft MVP - C++
-
Turn on double buffering: this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.ResizeRedraw, true); Christian Graus - Microsoft MVP - C++
-
Christian, Thanks-- In which/where (method?) do I "turn on double buffering"? what's "this."? - An instantiated "what"? I'm a bit behind the learning curve here but very appreciative of your response-- thanks again....
-
Christian, Thanks-- In which/where (method?) do I "turn on double buffering"? what's "this."? - An instantiated "what"? I'm a bit behind the learning curve here but very appreciative of your response-- thanks again....
this means the class that the code is written in. You don't need it, actually. Christian Graus - Microsoft MVP - C++