windows form creates trail on moving on desktop - c#

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.

Related

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

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;
}
}
}

Activate window that is created with WS_EX_NOACTIVATE?

I have a window that is created with the following CreateParams in C#:
protected override CreateParams CreateParams
{
get
{
CreateParams ret = base.CreateParams;
ret.ExStyle |= (int) Windows.WS_EX_NOACTIVATE;
return ret;
}
}
The effect that I want from this is that it doesn't steal (keyboard) focus from other windows, yet still responds to mouse input.
Question:
How can I nevertheless activate, move and then deactivate it from code, when I want to move it using a mouse? I'm overriding WndProc and working with windows messages.
There must be window messages that should be able to do it. The sys command "Move" does activate it, so it should be possible. But I need it to be much more simpler for users to move the window.
Edit:
I managed to activate and deactivate it with the following code but it doesn't move with the mouse (only changes its position on mouse up). The keyboard focus (caret) remains in the other window. That's what I need to keep intact, the focus in the other window.
SendMessage(m.HWnd, WM_NCACTIVATE, new IntPtr(1), IntPtr.Zero); // activate
SendMessage(m.HWnd, WM_NCACTIVATE, IntPtr.Zero, IntPtr.Zero); // deactivate

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

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