AdvancedHMI Software
General Category => Support Questions => Topic started by: MrPike on September 30, 2018, 12:17:10 PM
-
Archie, I wanted to create a custom control for a gauge. I created a new user control and set the background image of it to the gauge i want then I draw the line of the gauge using the following code. Everything works great except the control 'flickers' when it is updated too fast. I set the Double Buffer to true and enabled AntiAliasing, however this does not seem to help. What can I do to prevent this flickering? Thank you
P.S. How do you post code in this forum?
protected override void OnPaint(PaintEventArgs e)
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
//call the base paint method
base.OnPaint(e);
// enable anti Aliasing
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// draw the objects
Pen pointer = new Pen(this.BackColor, needleWidth);// create an initial pointer for the gauge using the controls back color
// draw the pointer on the gauge
e.Graphics.TranslateTransform(Width * 0.5f, Height * 0.5f); // sets the center of the control as the point to rotate the needle around
e.Graphics.RotateTransform(180+n_value); //the value to rotate the needle based on a public "Value" property. Had to offset by 180 degrees
e.Graphics.DrawLine(pointer, 0, 0, 0, Height * 0.5f); // this sets the length of the needle to just under half the height
pointer.Dispose(); // release the resources
-
Flickering can be difficult to control I a WinForms application. The best results I have come up with is to use this in the constructor:
Public Sub New()
MyBase.New()
''* reduce the flicker
Me.SetStyle(System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer Or
System.Windows.Forms.ControlStyles.AllPaintingInWmPaint Or
System.Windows.Forms.ControlStyles.UserPaint Or
System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, True)
End Sub
If you started with a UserControl, then a constructor may already exist in the designer.vb file
-
Nice job Archie, works great. Thank you
How can I post code in this forum?
-
If you started with a UserControl, then you will need to attached the *.vb and the *.designer.vb files. If you start with inheriting Control, then you will only need to the *.vb file.
-
Nice job Archie, works great. Thank you
How can I post code in this forum?
Click the hashtag button and paste your code between the markers. It's no different than adding a link.
-
Ignore my last post. My mind was on a complete different train of thought. Phrog's post is the answer you were looking for.
-
LOL, Thanks Archie. You had me baffled for a sec but I figured you were referring to something else.
Anyway, I took a stab at creating a custom control because nothing in the toolbox fit the bill. This is a .dll of a simple synchroscope I made. I didn't get too crazy as it does what I need. Hope it helps someone else. Thanks.