第四章菜单、工具栏和状态条.ppt
文本预览下载声明
Windows程序设计(Visual C++版)( Windows Programming Based on Visual C++ );第四章 菜单、工具栏和状态条;内 容;一、通常的约定;二、菜单类和菜单事件(P153);一些菜单事件:
(P153);三、用设计器编辑菜单;(4)再将工具箱中的MenuStrip组件拖放到窗体上,为窗体添加第二个menuStrip2主菜单。在“请在此处键入”框中输入“返回(R)”,并将快捷键属性ShortcutKeys选择为F5,即为F5功能键。;(5)利用属性窗口分别为 “切换(C)” 、 “退出(X)” 、 “返回(R)”菜单添加Click事件, 函数名称分别为On_Change、On_Exit和On_Return;为Form1窗体添加Load事件。
(6)各事件处理函数代码如下:;private: System::Void On_Change(System::Object^ sender,
System::EventArgs^ e)
{
this-menuStrip1-Visible=false;
this-menuStrip2-Visible=true;
}
private: System::Void On_Exit(System::Object^ sender,
System::EventArgs^ e)
{
Application::Exit();
}
private: System::Void On_Return(System::Object^ sender,
System::EventArgs^ e)
{
this-menuStrip1-Visible=true;
this-menuStrip2-Visible=false;
};ToolStripItemCollection 类表示 ToolStripItem 对象的集合 。 从ToolStrip派生类的很多属性都与它有关,如MenuStrip类的Items属性、ToolStripMenuItem的DropDownItems属性等都是ToolStripItemCollection句柄类型,所以通过使用ToolStripItemCollection 的Add、Remove 和 RemoveAt 等方法很容易在集合中添加和移除单个控件。还可以使用 AddRange 或 Clear 方法在集合中添加或移除所有控件。;(2) 单击[添加]按钮,为该控件添加Click事件代码:;private: System::Void On_MenuCommand(System::Object ^
sender,System::EventArgs ^e)
{
System::Windows::Forms::ToolStripMenuItem ^tempItem=
safe_castSystem::Windows::Forms::
ToolStripMenuItem ^(sender);
String^str = tempItem-Text;
if ((str-Equals( L退出(X)))
||(str-Equals( L退出(x))))
this-Close();
else
MessageBox::Show( str, L你选择了菜单项 );
};五、快捷菜单;五、快捷菜单;private: System::Void Form1_MouseDown(System::Object^
sender, System::Windows::Forms::MouseEventArgs^ e)
{
if (e-Button ==
System::Windows::Forms::MouseButtons::Right)
{ // 右击鼠标
this-contextMenuStrip1-Show( this,
System::Drawing::Point( e-X, e-Y ));
}
};private: System::Void contextMenuStrip1_Opening
(System::Object^ sender, System::ComponentModel::CancelEventArgs^ e)
{
this-ToolStripMenuItem1-Enabled = true;
this-ToolStripMenuItem2-Enabled = true;
this-ToolStripMenuItem3-Enabled = true;
if (this-contextMenuStrip1-SourceControl
== this-textBox1)
this-ToolStri
显示全部