DataGridView draws wrong - c#

I have a form and it has tones of other controls(buttons, custom controls, labels, panel,gridview). You can guess i had flickering issue. I tried doublebuffering and it couldn't solve. Finally i tried this one:
protected override CreateParams CreateParams
{
get
{
// Activate double buffering at the form level. All child controls will be double buffered as well.
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // WS_EX_COMPOSITED
return cp;
}
}
Flickering gone but my datagridview draws wrong. It shows CellBorders, BorderColors wrong. Actually this code has some issue with background images, lines, and other stuff. Why is that and how can it be fixed?

I know this question is a little old, but better late than never...
Here's a work-around to stop flickering when a user resizes a form, but without messing up the drawing of controls such as DataGridView. Provided your form name is "Form1":
int intOriginalExStyle = -1;
bool bEnableAntiFlicker = true;
public Form1()
{
ToggleAntiFlicker(false);
InitializeComponent();
this.ResizeBegin += new EventHandler(Form1_ResizeBegin);
this.ResizeEnd += new EventHandler(Form1_ResizeEnd);
}
protected override CreateParams CreateParams
{
get
{
if (intOriginalExStyle == -1)
{
intOriginalExStyle = base.CreateParams.ExStyle;
}
CreateParams cp = base.CreateParams;
if (bEnableAntiFlicker)
{
cp.ExStyle |= 0x02000000; //WS_EX_COMPOSITED
}
else
{
cp.ExStyle = intOriginalExStyle;
}
return cp;
}
}
private void Form1_ResizeBegin(object sender, EventArgs e)
{
ToggleAntiFlicker(true);
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
ToggleAntiFlicker(false);
}
private void ToggleAntiFlicker(bool Enable)
{
bEnableAntiFlicker = Enable;
//hacky, but works
this.MaximizeBox = true;
}

I found the trick to having nice smooth resizing and show my grid-lines, was to add an additional flag if my app is running under Windows XP or Windows Server 2003:
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
if (this.IsXpOr2003 == true)
cp.ExStyle |= 0x00080000; // Turn on WS_EX_LAYERED
return cp;
}
}
private Boolean IsXpOr2003
{
get
{
OperatingSystem os = Environment.OSVersion;
Version vs = os.Version;
if (os.Platform == PlatformID.Win32NT)
if ((vs.Major == 5) && (vs.Minor != 0))
return true;
else
return false;
else
return false;
}
}

Related

Form doesn't repaint itself when scrolling coast stuck of all controls

When I scroll horizontally or vertically, the controls collapse with each other until I release the scroll bar and everything draws normally.
I already tried with DoubleBuffer and set style after InitializeComponent
SetStyle(ControlStyles.OptimizedDoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint, true);
I also use:
private const int WM_HSCROLL = 0x114;
private const int WM_VSCROLL = 0x115;
protected override void WndProc(ref Message m)
{
if ((m.Msg == WM_HSCROLL || m.Msg == WM_VSCROLL)
&& (((int)m.WParam & 0xFFFF) == 5))
{
// Change SB_THUMBTRACK to SB_THUMBPOSITION
m.WParam = (IntPtr)(((int)m.WParam & ~0xFFFF) | 4);
}
base.WndProc(ref m);
}
But I'm still having the same problem. This is the normal behavior:
And this is when scrolling behavior:
UPDATE i find a solution but it just Reduces sharpness of that flicker solution
To handle the issue more efficient, please provide a simple demo to reproduce the issue.
And maybe you can try to override CreateParams.
protected override CreateParams CreateParams
{
get
{
if (Environment.OSVersion.Version.Major >= 6)
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
else
{
return base.CreateParams;
}
}
}

Prevent Win32 from drawing classic title bar

I wanted to add a nice shadow to my borderless form, and the best way I found to do it with minimal performance loss is to use DwmExtendFrameIntoClientArea. However, this seems to be causing Windows to draw a classic title bar over the window, but it is non-functional (ie. the glitch is merely graphical).
This is the code I am using:
int v = (int) DWMNCRENDERINGPOLICY.DWMNCRP_ENABLED;
NativeApi.DwmSetWindowAttribute(Handle, DwmWindowAttribute.NCRENDERING_POLICY, ref v, sizeof(int));
int enable = 0;
NativeApi.DwmSetWindowAttribute(Handle, DwmWindowAttribute.ALLOW_NCPAINT, ref enable, sizeof(int));
MARGINS margins = new MARGINS() {
leftWidth = 0,
topHeight = 0,
rightWidth = 0,
bottomHeight = 1
};
NativeApi.DwmExtendFrameIntoClientArea(Handle, ref margins);
I have tried setting ALLOW_NCPAINT to 1, and I even tried returning 0 when the window receives WM_NCPAINT without calling DefWndProc, but it made no difference.
Is there a way to resolve this weird issue while still using DwmExtendFrameIntoClientArea?
Big thanks to #Erik Philips, I have finally resolved the issue by following this answer's advice. The issue was in CreateParams:
/// <summary>
/// Gets the parameters that define the initial window style.
/// </summary>
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
if (!DesignMode) {
cp.ClassStyle |= (int) ClassStyle.DoubleClicks;
cp.Style |= unchecked((int) (WindowStyle.Popup | WindowStyle.SystemMenu | WindowStyle.ClipChildren | WindowStyle.ClipSiblings));
cp.ExStyle |= (int) ExtendedWindowStyle.Layered;
}
return cp;
}
}
The | had to be removed from cp.Style:
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
if (!DesignMode) {
cp.ClassStyle |= (int) ClassStyle.DoubleClicks;
cp.Style = unchecked((int) (WindowStyle.Popup | WindowStyle.SystemMenu | WindowStyle.ClipChildren | WindowStyle.ClipSiblings));
cp.ExStyle |= (int) ExtendedWindowStyle.Layered;
}
return cp;
}
}
This solved the issue, as apparently WinForms adds WS_BORDER by default to the class style, even if FormBorderStyle is later set to None.

How to avoid shadow in ToolTips?

I have developed a control for ToolTip and registered it as ToolTip with below codes,
protected override CreateParams CreateParams
{
get
{
CreateParams param = base.CreateParams;
param.ClassName = "tooltips_class32";
param.Style = unchecked(WindowMessages.WS_POPUP) | WindowMessages.TTS_ALWAYSTIP;
param.ExStyle |= WindowMessages.WS_EX_TOPMOST;
return param;
}
}
By default, the tooltip popup is enabled with Shadow.
How can i disable the default shadow of the ToolTip?
Regards,
Try to remove CS_DROPSHADOW flag from ClassStyle:
int CS_DROPSHADOW = 0x00020000;
{
...
param.ClassStyle &= ~CS_DROPSHADOW;
...
}

The Form hide and shows when using CreateParams

How can I fix form suddenly hides and shows again
Here is what cause the issue
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
cp.Style &= ~0x02000000; // Turn off WS_CLIPCHILDREN
return cp;
}
}
I use this line of code so that form with background image loads faster and remove flickering issuer
Refer Following Code:
private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
get
{
CreateParams myCp = base.CreateParams;
myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON ;
return myCp;
}
}
You cant hide, but can disable it by above code.
Referance link:
http://www.codeproject.com/Articles/20379/Disabling-Close-Button-on-Forms

.NET Custom UserControl's contents are disappearing when the parent is resized

I have derived the .NET UserControl, to make my own PictureList class.
However, when I resize the parent of the control, the contents of the control disappear. No Resize event is issued or whatever. I left out code not relevant to the question.
public ImageList(int width, int height)
{
ClientSize = new Size(width, height);
ResizeRedraw = true;
}
// Ensure background transparency will be handled corretly.
protected override CreateParams CreateParams
{
get {
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20; // WS_EX_TRANSPARENT
return cp;
}
}
protected override void OnPaint(PaintEventArgs e)
{
MessageBox.Show("This messagebox is only shown upon the first drawing, but not after the magically disappearing of the contents");
img = /*the image to draw*/
rect = /*the size of the image*/
e.Graphics.DrawImage(img, rect);
}
Do you need to enable AutoRedraw?

Categories