473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

On-Form drawing flickering

Well, to start, I would like to say that I read many of things available online to overcome the notorious flickering problem that all programmers face when trying to draw, resize, or even drag somethings on any form.

I started my project trying to dissect the screen into 4 quadrant using the mouse position to draw the dissection lines, however it turned out to be a very challenging job and I can get through with the thing by using a form and draw the dividing lines on that form.

My code snippet is (on the OnPaint event):
Expand|Select|Wrap|Line Numbers
  1.             Pen p = new Pen(Color.Red, 5);
  2.  
  3.             Graphics g = this.CreateGraphics();
  4.  
  5.             g.DrawLine(p, MousePosition.X, 0, MousePosition.X, 
  6.                              SystemInformation.PrimaryMonitorSize.Height);
  7.  
  8.             g.DrawLine(p, 0, MousePosition.Y,                                                                   
  9.                          SystemInformation.PrimaryMonitorSize.Width, 
  10.                           MousePosition.Y);
  11.             Refresh();
  12.  
  13.  
  14.             g.Dispose();
  15.  
Of course the last code will do the job with flickering red lines! so I tried using one of the available double buffering algorithms but it didn't work with me!

The double buffering code snippet is (on the OnPaint event):
Expand|Select|Wrap|Line Numbers
  1.             Pen p = new Pen(Color.Red, 5);
  2.             Bitmap offScreenBmp;
  3.             Graphics offScreenDC;
  4.             offScreenBmp = new Bitmap(this.Width, this.Height);
  5.             offScreenDC = Graphics.FromImage(offScreenBmp);
  6.  
  7.  
  8.             Graphics clientDC = this.CreateGraphics();
  9.  
  10.             offScreenDC.DrawLine(p, MousePosition.X, 0, MousePosition.X, SystemInformation.PrimaryMonitorSize.Height);
  11.             offScreenDC.DrawLine(p, 0, MousePosition.Y, SystemInformation.PrimaryMonitorSize.Width, MousePosition.Y);
  12.  
  13.             clientDC.DrawImage(offScreenBmp, 0, 0);
  14.             Refresh();
  15.  
I really hope that someone will have a solution for this problem.

Note: removing the (Refresh()) will not update the drawing!
Jul 17 '08 #1
4 1454
IanWright
179 100+
Have you tried something simple like this?

Expand|Select|Wrap|Line Numbers
  1.  
  2. public partial class Form1 : Form
  3. {
  4.    public Form1()
  5.    {
  6.       this.SetStyle(ControlStyles.UserPaint, true);
  7.       this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  8.    }
  9. }
  10.  
I've got this in one of my sample app's that I created for myself some time ago that had crazy flickering, but this prevented it...
Jul 18 '08 #2
IanWright
179 100+
In addition from the MSDN website, they include ControlSyles.DoubleBuffer

... "The following code example enables double-buffering on a Form and updates the styles to reflect the changes."

Expand|Select|Wrap|Line Numbers
  1. public void EnableDoubleBuffering()
  2. {
  3.    // Set the value of the double-buffering style bits to true.
  4.    this.SetStyle(ControlStyles.DoubleBuffer | 
  5.       ControlStyles.UserPaint | 
  6.       ControlStyles.AllPaintingInWmPaint,
  7.       true);
  8.    this.UpdateStyles();
  9. }
Jul 18 '08 #3
This is the solution with the least flickering so far!

The code works for about one second more or less! then the flickering starts again!

I've also tried the code from MSDN which gave me a different problem! The lines that I drew got stuck on the screen!

------------------------------------------------------------------------------------------
In addition from the MSDN website, they include ControlSyles.DoubleBuffer

... "The following code example enables double-buffering on a Form and updates the styles to reflect the changes."

Expand|Select|Wrap|Line Numbers
  1. public void EnableDoubleBuffering()
  2. {
  3.    // Set the value of the double-buffering style bits to true.
  4.    this.SetStyle(ControlStyles.DoubleBuffer | 
  5.       ControlStyles.UserPaint | 
  6.       ControlStyles.AllPaintingInWmPaint,
  7.       true);
  8.    this.UpdateStyles();
  9. }

This is the solution with the least flickering so far!

The code works for about one second more or less! then the flickering starts again!

I've also tried the code from MSDN which gave me a different problem! The lines that I drew got stuck on the screen!

------------------------------------------------------------------------------------------
Jul 21 '08 #4
IanWright
179 100+
asalaheddin,

I've just tried reading your original post in a little more detail. I'm not sure exactly what you are doing with your segments, but do you really need to creating a new Graphics object?

Normally I would do the following;

Expand|Select|Wrap|Line Numbers
  1.  
  2. private void Form_Paint(object sender, PaintEventArgs e)
  3. {
  4.   Graphics g = e.Graphics;
  5.   g.DrawLine( ... );
  6.   Refresh();
  7. }
Apart from that, I'm not sure if there is much I can suggest, maybe someone else could follow up if you're still having problems after that...
Jul 21 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Chris Krasnichuk | last post by:
hello, Does anyone know how I make php work on "my" computer? I made a mistake in my last post so I fixed it here. Chris
4
by: Frank | last post by:
Whats best : register_globals ON ? OR register_globals OFF ? I currently use: $_POST
1
by: David Walker | last post by:
Hi I've got two servers I can use for a website. The one (I'll call it 'X') I need to keep most of the site on doesn't have all the PHP libraries on I need, but I can't put the whole site on the...
10
by: Vadim Zima | last post by:
How would you approach this task? I have a phpBB forum on my site. The majority of participants are Russians living in the US, who don't know Russian keyboard layout. Is it possible to provide...
3
by: Ralph Freshour | last post by:
I have a PHP web app using MySQL - when I save a .jpg file named test's.jpg I see that the filename on the unix server is: test\'s.jpg - the filename I end up saving in my SQL table is named...
0
by: Google Mike | last post by:
After a lot of thought and research, and playing with FreeTDS and InlineTDS, as well as various ODBC connections, I have determined that the fastest and cheapest way to get up and going with PHP on...
3
by: TJ | last post by:
I need information on how to implement the PFPRO function within Verisign on a Windows platform. I hope it isn't true but I believe Verisign does not support PFPRO functions on Windows platforms...
3
by: Bruce W.1 | last post by:
With ASP.NET I've found that request are processed sequentially, one at a time, at least on a server with one CPU. If one request blocks for some reason, maybe on a long database query, then all...
10
by: Yang Li Ke | last post by:
Hi guys, Im about to build a script which will log visitor time spent on my website. I got a few ideas about this, maybe checking visitors ip and storing that info in db with time in and then...
6
by: Hasan Ceylan | last post by:
Hi Everyone, Recently we have started to have performance problems on our server which has the following configuration: Win 2K IIS 5 php The traffic to our web site is on normal time 300...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.