Form Background Image Tearing - C# - WinForms [duplicate] - c#

This question already has answers here:
How to fix the flickering in User controls
(12 answers)
Closed 2 years ago.
I was making a Form application on Visual Studio 2019 (Windows Forms) and then noticed the image was tearing when I was resizing the form. How can I fix it? Should I change one of the properties of the TabControl control?
Here's a photo showing what happened:
Background Image Tearing
Note: the image seen in the photo is the background image of the Form control

Maybe you can try to override CreateParams method.
protected override CreateParams CreateParams
{
get
{
if (Environment.OSVersion.Version.Major >= 6)
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
else
{
return base.CreateParams;
}
}
}

Related

Winform Application flickers on XP system

Hi I have a windows application (winform .net framework 4). The app is flickering alot on windows XP system. I added code:
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
The flickers were removed when I added above code. It worked fine on my development system which is windows 7 32 bit but On Windows XP the flickers has increased and also the background image is not loaded.
Is there any way by which flickers can be removed in all the windows OS?
Here is some information about Double Buffering. This is a built-in feature that is turned off by default. In my experience it doesn't always help but it is worth trying.
To turn it on, open the designer and select the form and look in the Properties of the form. Under the category 'Behavior' you will find the DoubleBuffered property. Just set this to true.
You may also try
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);

How to render XNA GraphicsDevice to Windows Forms Graphics object?

I use XNA in Windows Forms.
According to Microsoft tutorial, the control that uses XNA should have something like this in OnPaint():
GraphicsDevice.Present(srcRectangle, null, this.Handle);
But I wanted to draw additional things with Windows Forms Graphics object. I can do it just fine placing drawing code after GraphicsDevice.Present(), but I get horrible flickering. That's why I thought of setting double buffering this way:
protected override CreateParams CreateParams
{
get
{
var cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
But unfortunately it will draw only thing drawn with Graphics object (or the internal controls). That's why I would like to render my XNA scene to Graphics object. Is there any way for that?
I have tried:
Built-in WinForms double buffering, but not only each second frame is black but those normal frames are flickering too.
Saving content of window after Present() and then showing it with Graphics object.
Rendering to RenderTarget2D and then created bitmap to show on Graphics object with Image.FromStream(Stream) where stream came from RenderTarget.SaveAsPng().
Every idea failed. Is there anything else?
Actually,
Rendering to Texture is OK. Just rembember to call:
GraphicsDevice.SetRenderTarget(null);
Before you access data from it

windows form creates trail on moving on desktop

I have developed a windows form application using c#. And now when I move the that form on desktop a very large trail appears. Its like when we move any window like notepad when system is overloaded. But when system is working fine. no overload still the trail appears. which doesn't look good.
so is there any way to avoid that.
My system RAM is 2GB!
Do you use large pictures on winform?
You can try this:
Try to set the Double buffered property of the form.
or
Maybe you can solve with this code:
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
And finally you can try to set this property of the form : Transparency key with a color which you don't use on your form.

transparent richTextBox

how can I make my richtext box transparent
I want this cuz I am trying to put a text on top of a graphic image (which is the background of my form).
That is why I wanted the richTextBox to be transparent,
I am using .NET ,c# and in a windows form application
I know this answer is very late, but I hope it helps others who would like an easy way to get this done.
First, create a new User Control in your project and give it a name, say CustomRTB.cs. Once done, open the partial class and change:
public partial class CustomRTB : UserControl
to:
public partial class CustomRTB : RichTextBox
This will cause an error when you open the Design file so just go to the Designer.cs file and remove/comment the lines which show errors (there will be no more than two lines with errors). Next, add the following to the partial class:
protected override CreateParams CreateParams
{
get
{
//This makes the control's background transparent
CreateParams CP = base.CreateParams;
CP.ExStyle |= 0x20;
return CP;
}
}
The class should look like this now:
public partial class CustomRTB : RichTextBox
{
public CustomRTB()
{
InitializeComponent();
}
protected override CreateParams CreateParams
{
get
{
//This makes the control's background transparent
CreateParams CP = base.CreateParams;
CP.ExStyle |= 0x20;
return CP;
}
}
}
Now build your solution and you will be able to use the control in your forms. This control will be completely transparent and you will not be able to adjust the transparency. You will also be able to create different transparent controls apart from a richtextbox by changing the first line in this code. Hope this helps :)
Edit:
The problem with the above control is that it can only be used to display text programmatically as it is problematic to edit while running or debugging the application (as #nevelis explains in the comment below). However, there is a simple workaround for this:
First, create another User Control in your project and name it TranslucentPanel.cs (Yes, it is a panel and it is going to be translucent whose opacity can be controlled programmatically). Now open the partial class and modify it as:
public partial class TranslucentPanel : Panel
{
public TranslucentPanel()
{
InitializeComponent();
SetStyle(ControlStyles.SupportsTransparentBackColor |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.UserPaint, true);
BackColor = Color.Transparent;
}
}
You will have to take care of the error that crops up when you build the project by simply commenting out the line in the Designer.cs file which throws it. Once done, build your project again and the translucent panel will appear in your toolbox as before. Use this panel as a parent control to your transparent richtextbox i.e. place the panel on your form and place the RTB inside it. You can also set the BorderStyle property as None to remove any trace of the RTB from the UI.
You can also control the opacity of the translucent panel by using its BackColor property in your program:
translucentPanel1.BackColor = Color.FromArgb(50, 0, 0, 0);
Changing the arguments passed above will let you control the opacity and the colour of the panel.
This workaround will solve the cursor and scrolling problems of not only the transparent RTB, but also any other transparent control you create.
There is no such thing as true transparency in a WinForms Control. Transparent mode inherits the default background of its parent. The way I have worked around it in the past has been to use the OnPaint event and then use the Graphics.DrawString method to position the text where I want it.
Have you given this a try?
http://www.codeproject.com/KB/edit/AlphaBlendedTextControls.aspx?artkw=richTextBox%20to%20be%20transparent
There is no way to have Windows Forms controls with a transparent background. Many have tried it before and all have failed. Some came up with exotic hacks, but they all fail at some detail. Use WPF or HTML if you need more advanced rendering capabilities than the old Windows Forms can offer you.

During FlowLayoutPanel scrolling, background distorts + flickers

I have a windows form application that has a background. Within it, I have a flowlayoutpanel with a transparent background. When I scroll, the following happens:
I also see some flickering. I've tried all the doublebuffered business, and it doesn't work.
Any suggestions?
this is what worked for me.
public class CustomFlowLayoutPanel : FlowLayoutPanel
{
public CustomFlowLayoutPanel()
: base()
{
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}
protected override void OnScroll(ScrollEventArgs se)
{
this.Invalidate();
base.OnScroll(se);
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // WS_CLIPCHILDREN
return cp;
}
}
}
Yeah, that doesn't work. Here's a class that improves it somewhat:
using System;
using System.Windows.Forms;
class MyFlowLayoutPanel : FlowLayoutPanel {
public MyFlowLayoutPanel() {
this.DoubleBuffered = true;
}
protected override void OnScroll(ScrollEventArgs se) {
this.Invalidate();
base.OnScroll(se);
}
}
Compile and drop it from the top of the toolbox onto your form. It however cannot fix the fundamental problem, the "Show window content while dragging" option. That's a system option, it will be turned on for later versions of Windows. When it is on, Windows itselfs scrolls the content of the panel, then asks the app to draw the part that was revealed by the scroll. The OnScroll method overrides that, ensuring that the entire window is repainted to keep the background image in place. The end-result is not pretty, you'll see the image doing the "pogo", jumping up and down while scrolling.
The only fix for this is turning the system option off. That's not a practical fix, users like the option and it affects every program, not just yours. If you can't live with the pogo then you'll have to give up on the transparency.
I am very pleased to report that Hans, and the internet at large (just learn WPF....pfffft), is wrong here.
The problem is in the WM_HSCROLL and WM_VSCROLL events. Through some trial and error, I found that, if I dragged the scroll bar fast enough, I was able to move a ghost copy of my background over the actual background which was fxied how I wanted it. So whatever is happening inside the scrollable control, Windows is able to keep up and some out of sync redraw is what's causing the shearing.
So how do you solve this problem?
Set your scrollable control to DoubleBuffered.
Catch the WM_HSCROLL/WM_VSCROLL messages. Invalidate. Set the "do_not_paint" flag to true. Call the base.WndProc(). Set the "do_not_paint" flag to false. Update.
Catch the WM_PAINT and related messages. Only call base.WndProc() if the "do_not_paint" flag is false.
What this does is allow the scrollable control to do whatever layout calculations and scrollbar repositioning it needs to do but doesn't let it redraw anything that would trigger the shearing effect.
I added Application.DoEvents() to the Scroll event of the FlowPanel amd that stopped the blurring of the FlowPanel child controls that I was getting.
It´s a little bit late ... but this things happen if you mess with Color.Transparent. Check if your FlowLayoutPanel has Transparent Background. If so, try to change that.
try this
using Extended Window Styles
class MyFlowLayoutPanel : FlowLayoutPanel
{
protected override CreateParams CreateParams
{
get
{
var cp = base.CreateParams;
cp.ExStyle |=0x2000000 | 0x02000000;
return cp;
}
}
public MyFlowLayoutPanel()
{
this.DoubleBuffered = true;
}
protected override void OnScroll(ScrollEventArgs se)
{
this.Invalidate();
base.OnScroll(se);
}
}

Categories