做一些测试..。在ownerdraw自定义控件上。
这段代码会编译,但会崩溃...因为'm‘是<undefined value>
MySplitContainerControl::WndProc(m); /*with brakepoint,我可以看到一条消息....但拆下刹车点会导致撞车!
我正在尝试覆盖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
{
}
};
}发布于 2013-03-16 13:13:54
您必须:
protected: virtual void WndProc(Message% m) override
{
MySplitContainerControl::WndProc(m);
// ...
}因此,您调用的是被覆盖的WndProc方法本身。这会导致无限的递归调用,从而导致StackOverflowException。
让它成为Form::WndProc(m) (注释行)。它应该调用基类的WndProc,而不是它本身。
其次,在覆盖WM_PAINT时,您应该调用BeginPaint和EndPaint方法,而不是为窗口创建新的Graphics (和DC)。
https://stackoverflow.com/questions/15444813
复制相似问题