1

做一些测试......关于ownerdraw自定义控件。

此代码编译,但崩溃......由于那个'm'是<undefined value>

MySplitContainerControl::WndProc(m);/*带刹车点,我可以看到一条消息....但是删除刹车点会导致崩溃!

我正在尝试覆盖 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);
        }

为了得到一个想法,我在这里做的是完整的代码:

#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
        {

        }

    };
}
4

1 回答 1

1

你有:

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

所以你自己调用了被覆盖的WndProc方法。这会导致无限递归调用,从而导致StackOverflowException.

顺其自然Form::WndProc(m)(注释行)。它应该调用基类的WndProc,而不是它本身。

其次,在覆盖 时WM_PAINT,您应该调用BeginPaintandEndPaint方法,而不是为窗口创建新的Graphics(and DC)。

于 2013-03-16T05:13:53.530 回答