NaturalDemon
NaturalDemon

Reputation: 1014

Visual c++ WndProc override MySplitContainerControl::WndProc(m);

Doing some testing ... on ownerdraw custom controls.

this code compile, but crashes ... due to that 'm' is <undefined value>

MySplitContainerControl::WndProc(m); /*with brakepoint, i can see a messages .... but remove the brake point will result in crash!

i'm trying to override the windows look from the Splitcontainer.

protected: static int WM_PAINT = 0x000F;
protected: virtual void WndProc(Message% m) override 
    {
    MySplitContainerControl::WndProc(m); 
    /*Form::WndProc(m);*/
    if (m.Msg == WM_PAINT)
        {
                Graphics^ graphics = Graphics::FromHwnd(this->Handle);
                PaintEventArgs^ pe = gcnew PaintEventArgs(graphics, Rectangle(0,0, this->Width, this->Height));
                OnPaint(pe);
        }

To get an idea, what i'm doing here is the complete code:

#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;


namespace MySplitContainer {

    /// <summary>
    /// Summary for MySplitContainerControl
    /// </summary>
    public ref class MySplitContainerControl : public System::Windows::Forms::SplitContainer
    {
    public:
        MySplitContainerControl(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~MySplitContainerControl()
        {
            if (components)
            {
                delete components;
            }
        }

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container^ components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        }
#pragma endregion

    protected: static int WM_PAINT = 0x000F;
    protected: virtual void WndProc(Message% m) override 
        {
        MySplitContainerControl::WndProc(m);
        /*Form::WndProc(m);*/
        if (m.Msg == WM_PAINT)
            {
                    Graphics^ graphics = Graphics::FromHwnd(this->Handle);
                    PaintEventArgs^ pe = gcnew PaintEventArgs(graphics, Rectangle(0,0, this->Width, this->Height));
                    OnPaint(pe);
            }
        }

    protected: virtual void OnPaint(PaintEventArgs ^e) override
        {

        }

    };
}

Upvotes: 1

Views: 1077

Answers (1)

Moha Dehghan
Moha Dehghan

Reputation: 18463

You have:

protected: virtual void WndProc(Message% m) override 
{
    MySplitContainerControl::WndProc(m);
    // ...
}

So you are calling the overridden WndProc method in itself. This leads to an infinite recursive call, that causes a StackOverflowException.

Let it be Form::WndProc(m) (the commented line). It should call the base class's WndProc, not itself.

Secondly, when overriding WM_PAINT, you should call BeginPaint and EndPaint methods, instead of creating a new Graphics (and DC) for the window.

Upvotes: 1

Related Questions