Linked Questions

Popular Questions

How to make the child form flickers?

Asked by At

I'm developing a custom form that provides more options to customize the appearance of the form.I had a button in the parent form and by clicking the button i have shown another form by following code.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        CustomForm form = new CustomForm();
        form.ShowDialog();
    }
}

I handled the On_Wm_NcActivate through the following code.Im thinking im not handling the On_Wm_NcActivate correctly.

private void On_Wm_NcActivate(ref Message m)
{
    if (!this.IsMdiContainer)
        NativeMethods.LockWindowUpdate(this.Handle);
    base.WndProc(ref m);
    NativeMethods.LockWindowUpdate(IntPtr.Zero);
    if (Style != null)
    {
        var msg = new Message();
        msg.Msg = WindowMessages.WM_NCPAINT;
        msg.HWnd = m.HWnd;
        msg.WParam = (IntPtr)1;
        msg.LParam = (IntPtr)0;
        On_Wm_NcPaint(ref msg);
    }
}

I handled the On_Wm_NcPaint through the following code to customize the title bar and border and background of the overrided form.

private void On_Wm_NcPaint(ref Message m)
        {
            var rect = new NativePaint.RECT();
            NativeMethods.GetWindowRect(m.HWnd, ref rect);
            var deviceContext = NativeMethods.GetWindowDC(m.HWnd);
            if (deviceContext != IntPtr.Zero)
            {
                var bufferDeviceContext = NativeMethods.CreateCompatibleDC(deviceContext);
                if (bufferDeviceContext != IntPtr.Zero)
                {
                    IntPtr bmp = NativeMethods.CreateCompatibleBitmap(deviceContext, rect.Width, rect.Height);

                    if (bmp != IntPtr.Zero)
                    {
                        if (style == null)
                        {
                            return;
                        }

                        int borderThickness = 1;
                        var oldBmp = NativeMethods.SelectObject(bufferDeviceContext, bmp);
                        using (Graphics graphics = Graphics.FromHdc(bufferDeviceContext))
                        {
                            //To clip the client area from painting based on the border thickness and title bar height.
                            var top = GetCaptionBarHeight() + (int)borderThickness + 5;
                            var left = (int)borderThickness;
                            var right = rect.Width - (int)borderThickness;
                            var bottom = rect.Height - (int)borderThickness;
                            if (FormBorderStyle != FormBorderStyle.None)
                                DrawFrame(graphics, rect);

                            if (FormBorderStyle == FormBorderStyle.None)
                            {
                                top = 0;
                                left = 0;
                                right = Width;
                                bottom = Height;
                            }

                            //Clip the client region from the window rectangle.
                            if (this.WindowState != FormWindowState.Minimized)
                                NativeMethods.ExcludeClipRect(deviceContext, left + 1, top, right - 1, bottom - 1);

                            NativeMethods.BitBlt(deviceContext, 0, 0, rect.Width, rect.Height, bufferDeviceContext, 0, 0, WindowMessages.SRCCOPY);
                            NativeMethods.SelectObject(bufferDeviceContext, oldBmp);
                            NativeMethods.DeleteObject(bmp);
                            NativeMethods.DeleteObject(oldBmp);
                            NativeMethods.DeleteDC(deviceContext);
                            NativeMethods.DeleteObject(bufferDeviceContext);
                        }
                    }
                }
            }
        }

The Issue is while im clicking the parentform the child form is not flickers. I dont know how to solve this issue.Can any one please give the solution for this issue.

Note: Icon in the taskbar flickers but the custom form is not flickering

Related Questions