473,809 Members | 2,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

On-Form drawing flickering

2 New Member
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 1476
IanWright
179 New Member
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 New Member
In addition from the MSDN website, they include ControlSyles.Do ubleBuffer

... "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
asalaheddin
2 New Member
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.Do ubleBuffer

... "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 New Member
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
11679
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
12465
by: Frank | last post by:
Whats best : register_globals ON ? OR register_globals OFF ? I currently use: $_POST
1
2161
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 other (called 'Y') because I can't use the database from there. So, I need a way to be able to call the libraries (GD and PDF) from X with the functions on Y. For both of these, I need to be able to write the result of the function running on Y...
10
6478
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 them with some sort of a button, in the posting edit window to convert latin characters into cyrillic characters? TIA for any ideas and/or links. -- Vadim Zima Certified Russian Translator/Conference Interpreter
3
6599
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 test's.jpg - when I use an image tag to display the photo on my web page, no image displays. I tried to strip the slash out of the filename but the image still won't display on the web page - maybe I'm all goofed up here and don't understand what's...
0
3641
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 Linux, connecting to MS SQL Server, unless it was already pre-installed by your Linux installation, is to build your own multithreaded TCP socket server on Windows and connect to it through the socket API in PHP on Linux (if you have installed...
3
2344
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 only Unix. Has anyone handled e-commerce transaction via PHP on a Windows platform through Verisign. Again does anyone have any examples or has anyone here implemented an ecommerce site utilizing PHP on a Windows platform through Verisign using...
3
2571
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 users must wait until it completes, for their request to process. This leaves me wondering about PHP running on IIS and Window. Does this hold true for PHP also? What about PHP on Unix? Does this allow true simultaneous processing of all...
10
4761
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 using session vars to check time out ... Anyone has any ideas about doing that kind of script? Thank you! -- Yang
6
2299
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 - 400 concurrent
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10121
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.