|
Hello masters,
I have done a simple application in C# where I simply create a new UserControl derived class that draws a diagonal a red line on a blue background.
I have used some techniques used in C++ to avoid flickering, but when I resize my dialog box that contains my control, the drawing awfuly flickers.
How can I avoid flickering drawings?
I do the following thing:
<br />
internal sealed class Tetrion : UserControl<br />
{<br />
private GameBoard m_gameBoard=new GameBoard();<br />
<br />
internal Tetrion()<br />
{<br />
Debug.WriteLine("Tetrion.Tetrion");<br />
SetStyle(ControlStyles.DoubleBuffer, true);<br />
}<br />
<br />
public void Draw(Object sender, PaintEventArgs e)<br />
{<br />
Debug.WriteLine("Tetrion.OnPaint");<br />
m_gameBoard.Draw(e.Graphics, ClientRectangle);<br />
}<br />
<br />
protected override void OnResize(EventArgs e)<br />
{<br />
base.OnResize(e);<br />
Invalidate();<br />
}<br />
}<br />
I have registered the Draw event in the MainForm's constructor class like this:
<br />
m_tetrion.Paint+=new PaintEventHandler(m_tetrion.Draw);<br />
and, at last, the Draw method of the GameBoard class looks like this:
<br />
public void Draw(Graphics graphics, Rectangle rect)<br />
{<br />
Debug.WriteLine("GameBoard.Draw");<br />
<br />
Bitmap bmp=new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);<br />
Graphics memGraphics=Graphics.FromImage(bmp);<br />
Pen pen=new Pen(Color.Red);<br />
<br />
SolidBrush brush=new SolidBrush(SystemColors.Control);<br />
memGraphics.FillRectangle(brush, rect);<br />
<br />
memGraphics.DrawLine(pen, rect.X, rect.Y, rect.Width, rect.Height);<br />
<br />
graphics.DrawImage(bmp, rect);<br />
}<br />
I think everything looks fine to me in the code, but it still flickering...
Please, help!
Thanks!!!
|
|
|
|
|
According to the documentation for ControlStyles.DoubleBuffer , you must also set ControlStyles.UserPaint and the ControlStyles.AllPaintingInWmPaint to true also for double buffering to work properly. For example
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
If you're going to be doing ~all~ the painting, don't use a UserControl. Instead you'll want to write a custom control: just create a class an inherit directly from System.Windows.Forms.Control class. Visual Studio has a template for this, Project->Add Class->Custom control.
---------------------------
He who knows that enough is enough will always have enough.
-Lao Tsu
|
|
|
|
|
Thanks a lot Judah!!!
It works great that way. it does not flickers anymore!
|
|
|
|
|
In addition to what Judah said, using Debug.WriteLine in drawing code is NOT recommended. The TraceListener s (both Debug and Trace use them, which are compiled to code if the DEBUG and/or TRACE pre-proc defs are defined at compile time) are very slow. When you debug this application, it gets even slower since the DefaultTraceListener uses the native OutputDebugString which VS.NET catches and displays in your output window - this is a blocking call (i.e., your drawing code is foregone until that process described above is complete).
If all you had was the Debug.WriteLine and did a default Release compile, it wouldn't be included, but there's really no reason to trace this call anyway. It either works or it doesn't, and this will only slow it down. If you're painting your control, you'll notice if it works and you don't need a trace statement for that.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
Thanks for the answers. If I included traces in the Drawing procedure, it's because I want to know if it does not call too many times. Then I see that when I resize my dialog box, the drawing function is called only once. But even in Release, it's still flicker with or without the trace calls.
|
|
|
|
|
I'm just sayin'...
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
After applying Judah's modification, when I have debug traces it slows down, but it does not flicker. In release, it runs smoothly C# is cool!
|
|
|
|
|
By the way,
In C#, I don't see any differences between these two drawing techniques (both are smooth):
- creating the image in a memory bitmap then display it once the bitmap is done,
- directly draw on the graphic's context (DC) without using a memory bitmap
Both are smooth. I guess it's because of the SetStyle call.. right?
In MFC/Win32, when you draw directly on the DC, it flickers, that's why you need to create the image in memory, and once done, you display it. in C#, wether or not the bitmap is built in memory, there is no flickering
Best regards.
|
|
|
|
|
bouli wrote:
I guess it's because of the SetStyle call.. right?
Most likely. If you specify the right flags for double-buffering, the OnDraw is probably called in a different thread (or buried deep in implementation) and once finished the image is drawn to the DC (or flipped). I haven't looked into it that deep.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
how can i encrypt the output of my program using the 128 bit encrypt algorithm?
|
|
|
|
|
System.Security.Cryptography implements a whole bunch of ways to encrypt look in the encryption part of this site: CP on Cryptograhpy
|
|
|
|
|
How to insert images from one window to different windows .for more info u can see the smilyes of outllok .
This is Jitendra
|
|
|
|
|
This can be done with a RichTextBox. There's a good article explaining this in more detail somewhere on this site, I suggest you do a search.
---------------------------
He who knows that enough is enough will always have enough.
-Lao Tsu
|
|
|
|
|
hi all =)
i'm looking on a way to retrieve the date time stamps that are stored in the cache index.dat of IE in order to know the time spend on each url.
if someone can help me
thx a lot
|
|
|
|
|
The cookie cache's index.dat file is weakly encrypted, this straight from the mouth of Microsoft. I'm betting the web cache's index.dat is too. They do that for a reason, so I won't suggest trying to reverse engineer it.
There is an "easier" way, though. Okay, it may not be easier if index.dat isn't encrypted, but it's 100% legal and more durable since Microsoft could change the format of index.dat at any time. Developers aren't supposed to be in it (and I honestly do recommend against this), so Microsoft has the right to change it whenever they want (for performance, size, whatever).
You'll have to declare the IUrlHistoryStg and IEnumSTATURL interfaces (with the proper GuidAttribute s), and define the STATURL structure. Then create a class that implements IUrlHistoryStg and is attributed with the GuidAttribute with the CLSID CLSID_CUrlHistoryStg , which is {3C374A40-BAE4-11CF-BF7D-00AA006946EE}. Also be sure to use the ComImportAttribute or else the CLR will create an instance of your object that uses empty definitions instead of the actual COM instance.
Create an instance of your IUrlHistoryStg implementation class, enum the URLs and get the STATURL struct for each, which gives you the various dates (although IE doesn't track how much time was spent on each site, so I'm not sure what you're talking about there). This will give you the last access time.
Of course, since all the URL history items are actually just .url shortcuts on the file system, you could always use simple IO to enumerate the files and check their last modification date. That should also work.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
If i have a xml document like this:
<?xml version="1.0" standalone="yes"?>
<sessions version="1.0">
<session name="Development">
<parameters>
<rootdirectory>n:\realsoftware</rootdirectory>
<validoslist>win32</validoslist>
<propathwin32>.,[rootdirectory]\usr\%username%,[rootdirectory]\fastforward\pvs100,[rootdirectory]\src,[rootdirectory],[rootdirectory]\start</propathwin32>
</parameters>
</session>
</sessions>
Now i want the text in propathwin32 in a textBox but i can't do it
I tried it like this:
XmlDocument xml = new XmlDocument();
xml.Load(@"c:\listbox.xml");
string test = xml.GetElementsByTagName("propathwin32").ToString();
and i tried it with InnerXml.
can anyone help me?
Thx in advance
|
|
|
|
|
Load the document and use the appropriate XPath expression:
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode node = doc.DocumentRoot.SelectSingleNode(
"/sessions/session/parameters/propathwin32");
if (node != null)
{
} Also, if you use a namespace to qualify your elements (or even attributes, but that's no typical), make sure you qualify them in code, too, by creating an XmlNamespaceManager using XmlDocument.NameTable as the argument to the constructor. Add your namespace and whatever prefix you want (the prefixes don't have to match - only the namespaces). To note, default namespaces (prefixes that equal "") don't work for some weird reason so you'll have to specify an actual prefix.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
you are using the wrong property.
use InnerText rather than InnerXml
"When the only tool you have is a hammer, a sore thumb you will have."
|
|
|
|
|
how to use it in c# and functions inside it?????
|
|
|
|
|
You should ask in the appropriate forum. i.e. The one associated with the article:
http://www.codeproject.com/useritems/GINA_SPY.asp[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."
--Charles Babbage (1791-1871)
|
|
|
|
|
Sorry, I spoke too soon. I originally googled for GINA.DLL and a CodeProject article was the first one hit.
I then googled for site:msdn.microsoft.com GINA and found this:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/security/security/winlogon_and_gina.asp[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."
--Charles Babbage (1791-1871)
|
|
|
|
|
To consume it, you'll need to declare its native functions in C# using P/Invoke. See the DllImportAttribute documentation in the .NET Framework SDK for details, as well as the MarshalAsAttribute .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
many thanks guys
|
|
|
|
|
I used this code and compiled it in VS2002 and it works fine.
#region Key events
protected override void OnKeyDown(KeyEventArgs kea)
{
switch (kea.KeyData)
{
case Keys.Enter:
...
..
break;
case Keys.Escape:
this.Close();
break;
}
}
#endregion
I then compiled it in VS2003 and it woudn't enter the method when I make
a key event like Esc or any other.
Anyone knows why?
Thanks
Thomas
|
|
|
|
|
The method is OnKeyDown . C# (and IL itself) is case-sensitive. It wouldn't have compiled, though, so I'm sure that is just lazy typing.
What class are you directly deriving from?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|