C++/Cli里重载winform的WndProc和ProcessCmdKey
注意,ProcessCmdKey里的第二个参数,不能用%
不过看WndProc和ProcessCmdKey在C#上的定义,看不出System::Windows::Forms::Keys keyData居然不需要%
看看vs里面对象浏览器里的定义:
public ref class SelectEntityToolForm : public System::Windows::Forms::Form
{
protected: virtual System::Void WndProc(System::Windows::Forms::Message% msg)override;
virtual System::Boolean ProcessCmdKey(System::Windows::Forms::Message% msg, System::Windows::Forms::Keys keyData) override;
};
System::Void SelectEntityToolForm::WndProc(System::Windows::Forms::Message% m)
{
__super::WndProc(m);
//BeConsole::Printf();
if (m.Msg == 0x0018) // WM_SHOWWINDOW
{
if (m.WParam.ToInt32() == SW_HIDE)
{
int i = 0;
std::ignore = i;
// 窗体被隐藏时的处理代码
// ...
//System::Windows::Forms::MessageBox::Show("选择对话框被隐藏");
}
else if (m.WParam.ToInt32() == SW_SHOWNORMAL)
{
//System::Windows::Forms::MessageBox::Show("选择对话框被显示");
}
else
{
// auto x = m.WParam.ToInt32();
// std::ignore = x;
// std::wstring str = fmt::format(L"WM_SHOWWINDOW {}", x);
//
// System::Windows::Forms::MessageBox::Show(gcnew System::String(str.c_str()));
}
}
}
System::Boolean SelectEntityToolForm::ProcessCmdKey(System::Windows::Forms::Message% msg, System::Windows::Forms::Keys keyData)
{
// 检查是否按下了特定的键
if (keyData == System::Windows::Forms::Keys::Escape)
{
MiscUtil::ScheduleStopCurrentCommand();
this->Hide();
return true; // 表示键被处理了
}
// 如果不是F1键,则调用基类的ProcessCmdKey方法继续处理
return System::Windows::Forms::Form::ProcessCmdKey(msg, keyData);
}