Overriding WndProc -> Cant click OK in a MessageBox

Post your questions regarding programming in C# in here.
4 posts Page 1 of 1
Contributors
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

I have overridden the WndProc function to draw in the non-client area. The drawing and everything works great, but for some reason I can't click in any dialogs. Message Boxes, File Dialogs, they both respond to mouse hovering, but I cant click any of the buttons inside the dialogs. Not even caption buttons. This is the WndProc and HitTest functions I have. You wont be able to compile since there are a lot of other functions referenced too which I'm not going to post.
Code: Select all
        protected override void WndProc(ref Message m)
        {
            WindowHitTestRegions hit;
            Point p;

            switch ((WM)m.Msg)
            {
                case WM.NCHITTEST:

                    HitTest(ref m);

                    return;

                case WM.NCMOUSEMOVE:

                    hit = HitTest(m.LParam.ToInt32(), m.HWnd);

                    switch (hit)
                    {
                        case WindowHitTestRegions.CloseButton:
                            CloseButton.MouseEnter();
                            MaximizeButton.MouseLeave();
                            MinimizeButton.MouseLeave();
                            return;
                        case WindowHitTestRegions.MaximizeButton:
                            MaximizeButton.MouseEnter();
                            CloseButton.MouseLeave();
                            MinimizeButton.MouseLeave();
                            return;
                        case WindowHitTestRegions.MinimizeButton:
                            MinimizeButton.MouseEnter();
                            CloseButton.MouseLeave();
                            MaximizeButton.MouseLeave();
                            return;
                        default:
                            CloseButton.MouseLeave();
                            MinimizeButton.MouseLeave();
                            MaximizeButton.MouseLeave();
                            if (hit == WindowHitTestRegions.CloseButton || hit == WindowHitTestRegions.MaximizeButton || hit == WindowHitTestRegions.MinimizeButton) return;
                            break;
                    }

                    break;

                case WM.NCLBUTTONDOWN:

                    hit = HitTest(m.LParam.ToInt32(), m.HWnd);
                    p = new Point(m.LParam.ToInt32());

                    if (hit == WindowHitTestRegions.CloseButton)
                    {
                        CloseButton.MouseDown(new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 1, p.X, p.Y, 0));
                        return;
                    }
                    if (hit == WindowHitTestRegions.MaximizeButton)
                    {
                        MaximizeButton.MouseDown(new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 1, p.X, p.Y, 0));
                        return;
                    }
                    if (hit == WindowHitTestRegions.MinimizeButton)
                    {
                        MinimizeButton.MouseDown(new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 1, p.X, p.Y, 0));
                        return;
                    }

                    break;

                case WM.NCLBUTTONUP:

                    hit = HitTest(m.LParam.ToInt32(), m.HWnd);
                    p = new Point(m.LParam.ToInt32());

                    if (hit == WindowHitTestRegions.CloseButton)
                    {
                        CloseButton.MouseUp(new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 1, p.X, p.Y, 0));
                        return;
                    }

                    if (hit == WindowHitTestRegions.MaximizeButton)
                    {
                        MaximizeButton.MouseUp(new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 1, p.X, p.Y, 0));
                        return;
                    }
                    if (hit == WindowHitTestRegions.MinimizeButton)
                    {
                        MinimizeButton.MouseUp(new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 1, p.X, p.Y, 0));
                        return;
                    }

                    break;

                //case WM.PAINT:
                //case WM.PAINTICON:
                //case WM.PAINT:


                case WM.NCPAINT:
                case WM.ACTIVATEAPP:
                case WM.ACTIVATE:
                case WM.NCACTIVATE:
                    PaintFrame(m.HWnd);
                    m.Result = new IntPtr(0);
                    return;

                default:
                    break;
            }


            if (Height != LastHeight)
            {
                clientBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, Height), Color.FromArgb(70, 70, 70), Color.DimGray);
                LastHeight = Height;
            }
            if (Width != LastWidth)
            {
                CloseButton.ClientRectangle = new Rectangle(Width - 26, 8, 16, 16);
                MaximizeButton.ClientRectangle = new Rectangle(Width - 47, 8, 16, 16);
                MinimizeButton.ClientRectangle = new Rectangle(Width - 68, 8, 16, 16);
                LastWidth = Width;
            }

            base.WndProc(ref m);
        }

        private void HitTest(ref Message m)
        {
            Point p = new Point(m.LParam.ToInt32());
            p.Offset(new Point(-this.Location.X, -this.Location.Y));

            if (GetClientRectangle().Contains(p))
            {
                m.Result = (IntPtr)(int)WindowHitTestRegions.ClientArea;
                return;
            }

            if (CloseButton.ClientRectangle.Contains(p))
            {
                m.Result = (IntPtr)(int)WindowHitTestRegions.CloseButton;
                return;
            }

            if (MaximizeButton.ClientRectangle.Contains(p))
            {
                m.Result = (IntPtr)(int)WindowHitTestRegions.MaximizeButton;
                return;
            }

            if (MinimizeButton.ClientRectangle.Contains(p))
            {
                m.Result = (IntPtr)(int)WindowHitTestRegions.MinimizeButton;
                return;
            }

            base.WndProc(ref m);

            WindowHitTestRegions res = (WindowHitTestRegions)m.Result.ToInt32();
            if (res == WindowHitTestRegions.CloseButton || res == WindowHitTestRegions.MaximizeButton || res == WindowHitTestRegions.MinimizeButton || res == WindowHitTestRegions.HelpButton)
                m.Result = (IntPtr)(int)WindowHitTestRegions.TitleBar;
        }

        private WindowHitTestRegions HitTest(int lParam, IntPtr Handle)
        {
            Point p = new Point(lParam);
            p.Offset(new Point(-this.Location.X, -this.Location.Y));

            if (GetClientRectangle().Contains(p))
            {
                return WindowHitTestRegions.ClientArea;
            }

            if (CloseButton.ClientRectangle.Contains(p))
            {
                return WindowHitTestRegions.CloseButton;
            }

            if (MaximizeButton.ClientRectangle.Contains(p))
            {
                return WindowHitTestRegions.MaximizeButton;
            }

            if (MinimizeButton.ClientRectangle.Contains(p))
            {
                return WindowHitTestRegions.MinimizeButton;
            }

            Message m = Message.Create(Handle, (int)WM.NCHITTEST, IntPtr.Zero, new IntPtr((int)(p.Y << 16) | (p.X)));

            base.WndProc(ref m);

            WindowHitTestRegions res = (WindowHitTestRegions)m.Result.ToInt32();
            if (res == WindowHitTestRegions.CloseButton || res == WindowHitTestRegions.MaximizeButton || res == WindowHitTestRegions.MinimizeButton || res == WindowHitTestRegions.HelpButton)
                return WindowHitTestRegions.TitleBar;

            return (WindowHitTestRegions)m.Result.ToInt32();
        }
Last edited by MrAksel on Sat Apr 07, 2012 4:50 pm, edited 2 times in total.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
DreadNought
VIP - Donator
VIP - Donator
Posts: 116
Joined: Fri Jan 08, 2010 12:37 pm

You have posted alot of code so I am not going to go through it all.

However, I know the messagebox buttons will still respond to hover even if they are not active, and you cant click when they are not active(something in its way) So maybe something else is active and not the required form? -Just a random idea here.
Bound and boom tech,
The Future Of Coding
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Yes, you are right, but the problem is that the dialog form isnt actice at all. I ca click the caption bar and move it, but it's still not active
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
DreadNought
VIP - Donator
VIP - Donator
Posts: 116
Joined: Fri Jan 08, 2010 12:37 pm

I am unable to help, I briefly went over your code(I just got back from a rough night, Hangover's aren't fun) I have never actually played with WndProc, For hooking(or overriding some say) I try to stick to C++ and just execute functions from that native DLL, Or I just use EasyHooks, but that's not the point, Only thing I can suggest is programatically making the dialog active as a temporarily workaround for the problem.

Sorry I cant be of assistance :(
Bound and boom tech,
The Future Of Coding
4 posts Page 1 of 1
Return to “General coding help”