4 Replies - 1066 Views - Last Post: 13 May 2011 - 01:30 PM Rate Topic: -----

#1 sgt frankieboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-August 09

Non-Client border flickers, draw custom close button

Posted 13 May 2011 - 10:35 AM

I am making a custom Components Pack but I run into a little problem.

When I resize the form. the border flickers and the old title bar shows up.
Also I can't figure out how to custom draw the control box buttons(close,maximize, etc.)

Here is the code I use for non-client drawing:

        [DllImport("user32.dll")]
        static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

        [DllImport("User32.dll")]
        private static extern IntPtr GetWindowDC(IntPtr hWnd);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern void DisableProcessWindowsGhosting();

        [DllImport("UxTheme.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern IntPtr SetWindowTheme(IntPtr hwnd, string pszSubAppName, string pszSubIdList);

protected override void WndProc(ref Message m)
        {

            const int WM_NCPAINT = 0x85;
            base.WndProc(ref m);
            if (m.Msg == WM_ERASEBKGND)
                m.Result = new IntPtr(0);

            if (m.Msg == WM_NCPAINT || m.Msg == WM_IME_NOTIFY || m.Msg == WM_SIZE || m.Msg == WM_NCACTIVATE)
            {

                IntPtr hdc = GetWindowDC(m.HWnd);
                if ((int)hdc != 0)
                {

                    Graphics g = Graphics.FromHdc(hdc);

                    Rectangle rect = new Rectangle(new Point(0, 0), new Size(this.Width, 23));

                    LinearGradientBrush linearBrush = new LinearGradientBrush(rect, Color.FromArgb(134, 174, 226), Color.FromArgb(178, 204, 239), LinearGradientMode.Vertical);
                    rect.Height = 7;
                    LinearGradientBrush linearBrush1 = new LinearGradientBrush(rect, Color.FromArgb(170, 199, 239), Color.FromArgb(157, 194, 249), LinearGradientMode.Vertical);

                    Color col = Color.FromArgb(178, 204, 239);
                    Rectangle BorderLeft = new Rectangle(0, 23, 3, this.Height - 23);
                    g.FillRectangle(new SolidBrush(col), BorderLeft);
                    Rectangle BorderRight = new Rectangle(this.Width - 4, 23, 4, this.Height - 23);
                    g.FillRectangle(new SolidBrush(col), BorderRight);
                    Rectangle BorderBottom = new Rectangle(0, this.Height - 4, this.Width, 4);
                    g.FillRectangle(new SolidBrush(col), BorderBottom);
                    

                    g.FillRectangle(linearBrush, 0, 0, this.Width, 23);
                    g.FillRectangle(linearBrush1, 0, 0, this.Width, 7);

                    Rectangle clientRect = this.ClientRectangle;
                    clientRect.Location = new Point(3, 23);
                    g.FillRectangle(new SolidBrush(BackColor), clientRect);

                    if (this.ShowIcon != false && this.Icon != null)
                    {
                        g.DrawImage(this.Icon.ToBitmap(), 6, 5, 16, 16);
                    }

                    g.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), 25, 6);


                    foreach (Control control in this.Controls)
                        control.Invalidate();

                    g.Flush();
                    g.Dispose();
                    linearBrush.Dispose();
                    linearBrush1.Dispose();
                    ReleaseDC(m.HWnd, hdc);
                }
            }
        }



sometimes the control box buttons show up.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Non-Client border flickers, draw custom close button

#2 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4929
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Non-Client border flickers, draw custom close button

Posted 13 May 2011 - 11:28 AM

What is your reasoning behind doing this in a WndProc message handler?

It makes sense that the original 'look' is still happening because you haven't done anything to stop it.

Typically you would override the on_paint handler.

In your case you have the on_paint still happening followed by your WndProc so you get the effect you are seeing.
Was This Post Helpful? 0
  • +
  • -

#3 sgt frankieboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-August 09

Re: Non-Client border flickers, draw custom close button

Posted 13 May 2011 - 12:15 PM

View PosttlhIn`toq, on 13 May 2011 - 08:28 PM, said:

What is your reasoning behind doing this in a WndProc message handler?

It makes sense that the original 'look' is still happening because you haven't done anything to stop it.

Typically you would override the on_paint handler.

In your case you have the on_paint still happening followed by your WndProc so you get the effect you are seeing.


The Reason is I'm using WndProc is that the OnPaint event doesn't allow painting in the non-client areas from the form.
Is there a way to disable the flickering by disabling the OnPaint event or use the onPaint event to draw non-client areas?

This post has been edited by sgt frankieboy: 13 May 2011 - 12:16 PM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4929
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Non-Client border flickers, draw custom close button

Posted 13 May 2011 - 01:08 PM

You could try creating an override method for it that is empty.

The default is base.On_Paint.
Just comment out that line.
Was This Post Helpful? 0
  • +
  • -

#5 sgt frankieboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-August 09

Re: Non-Client border flickers, draw custom close button

Posted 13 May 2011 - 01:30 PM

View PosttlhIn`toq, on 13 May 2011 - 10:08 PM, said:

You could try creating an override method for it that is empty.

The default is base.On_Paint.
Just comment out that line.


:( that doesn't fix the problem. you still keep seeing the old Theme behind it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1