Windows Forms编程实战学习:第三章 菜单

本文深入探讨了Windows Forms控件的层次结构、关键属性、方法和事件,以及菜单系统的实现方式,包括传统菜单与上下文菜单的区别,并展示了如何在顶级菜单中添加子菜单项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第三章 菜单

1,控件和容器

所有的Windows Forms控件都是从System.Windows.Forms.Control类继承的,相关类的层次结构如下图所示:

MarshalByRefObject类表示必须通过引用进行列集(marshale)的对象。列集表示从一个上下文环境中传递一个数据项使它可以在另一个上下文环境中被理解的一种机制。只在创建它们的进程中有效,必须通过引用才能在进程外使用。

Component类为通过引用进行列集的对象提供了IComponent接口。组件是可以在容器中存在的对象,并且可以通过Dispose方法清理系统资源。

ButtonBase是所有按钮的基类,包括单选和复选按钮。

 

容器类:可以包含其他控件的控件称为容器控件。Control类本身提供了对容器的支持,例如Controls属性和GetNextControl方法。

 

Form类的层次结构

其中ScrollableControl提供了包含对象自滚动的功能。

ContainerControl类表示提供焦点管理的控件,提供了它所包含的控件集合的逻辑边界。这个类追踪容器中的活动控件,还负责管理容器中各个控件的Tab顺序。

 

Control类的常用属性、方法和事件

公共属性

AllowDrop

是否允许拖放

Anchor

锚定设置

BackColor

背景色

ContextMenuStrip

上下文菜单

Controls

控件所包含的控件

ClientRectangle

获取客户区域,DisplayRectangle获取控件的显示区域。

Cursor

鼠标出现在这个控件上所呈现的光标

Enabled

是否启用这个控件

Location

控件的位置

Parent

控件的父控件

TabIndex

Tab索引

TabStop

是否启动Tab

Text

控件关联的文本

Visible

是否可见

公共方法

BringToFront

使这个控件出现在Z序的最前边

GetNextControl

按照Tab顺序返回上一个或下一个控件

Invalidate

强制这个控件的全部或部分重绘

PointToClient

把一个屏幕位置转换为客户坐标

公共事件

Click

点击时发生

KeyPress

具有焦点且按下按钮时发生

MouseUp

鼠标光标位于控件内部时,释放一个按钮时发生

Paint

全部或部分重绘时发生

 

2,菜单

菜单一般分为传统菜单(主菜单或者锚定菜单)和上下文菜单(弹出菜单或者快捷菜单)。.Net中的上下文菜单一般与控件相关联。

ToolStrip:可滚动控件

MenuStrip:菜单

StatusStrip:状态栏

ContextMenuStrip:弹出式菜单

 

ToolStripMenuItem表示一个菜单项。MenuStrip和ContextMenuStrip对象是简单的容器。

ToolStripDropDownItem:

公共属性

Checked

是否在右边显示选中标记

CheckState

三态

Enabled

是否启用

Overflow

如何与重合的按钮进行交互,基于ToolStripItemOverflow枚举

ShortcutKeyDisplay_String

快捷键显示的字符串,若为空,显示为实际值。

ShortcutKeys

快捷键,基于Keys的枚举

ShowShortcutKeys

显示菜单是是否显示ShortcutKeys设置

公共事件

CheckedChanged

Checked属性变换

CheckStateChanged

CheckState状态变化

 

namespace chapter_3

{

partial class Form1

{

/// <summary>

/// 必需的设计器变量。

/// </summary>

private System.ComponentModel.IContainer components = null;

 

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

 

#region Windows 窗体设计器生成的代码

 

/// <summary>

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.menuStrip1 = new System.Windows.Forms.MenuStrip();

this.fileFToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.loadLToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.ExitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.menuStrip1.SuspendLayout();

this.SuspendLayout();

//

// menuStrip1

//

this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {

this.fileFToolStripMenuItem});

this.menuStrip1.Location = new System.Drawing.Point(0, 0);

this.menuStrip1.Name = "menuStrip1";

this.menuStrip1.Size = new System.Drawing.Size(284, 25);

this.menuStrip1.TabIndex = 0;

this.menuStrip1.Text = "menuStrip1";

//

// fileFToolStripMenuItem

//

this.fileFToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {

this.loadLToolStripMenuItem,

this.ExitToolStripMenuItem});

 

this.fileFToolStripMenuItem.Name = "fileFToolStripMenuItem";

this.fileFToolStripMenuItem.Size = new System.Drawing.Size(53, 21);

this.fileFToolStripMenuItem.Text = "File(&F)";

this.fileFToolStripMenuItem.Click += new System.EventHandler(this.fileFToolStripMenuItem_Click);

//

// loadLToolStripMenuItem

//

this.loadLToolStripMenuItem.Name = "loadLToolStripMenuItem";

this.loadLToolStripMenuItem.ShortcutKeyDisplayString = "";

this.loadLToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.L)));

this.loadLToolStripMenuItem.Size = new System.Drawing.Size(162, 22);

this.loadLToolStripMenuItem.Text = "Load(&L)";

this.loadLToolStripMenuItem.Click += new System.EventHandler(this.loadLToolStripMenuItem_Click);

//

// ExitToolStripMenuItem

//

this.ExitToolStripMenuItem.Name = "ExitToolStripMenuItem";

this.ExitToolStripMenuItem.Size = new System.Drawing.Size(162, 22);

this.ExitToolStripMenuItem.Text = "Exit(&Q)";

this.ExitToolStripMenuItem.Click += new System.EventHandler(this.ExitToolStripMenuItem_Click);

//

// Form1

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(284, 261);

this.Controls.Add(this.menuStrip1);

this.MainMenuStrip = this.menuStrip1;

this.Name = "Form1";

this.Text = "Form1";

this.menuStrip1.ResumeLayout(false);

this.menuStrip1.PerformLayout();

this.ResumeLayout(false);

this.PerformLayout();

 

}

 

#endregion

 

private System.Windows.Forms.MenuStrip menuStrip1;

private System.Windows.Forms.ToolStripMenuItem fileFToolStripMenuItem;

private System.Windows.Forms.ToolStripMenuItem loadLToolStripMenuItem;

private System.Windows.Forms.ToolStripMenuItem ExitToolStripMenuItem;

}

}

 

 

在顶层菜单中添加子菜单项,先创建ToolStripItem数组,然后用DropDownItem属性的AddRange方法添加到菜单中去。

 

ToolStripItem表示ToolStrip的一个项

公共属性

AllowDrop

重新排序和拖放操作是默认行为(false)还是自定义行为(true)

Alignment

与包含它的工具条起始还是末尾对齐

Anchor

如何粘附到容器的各个边缘

BackColor

背景色

ClientRectangle

内部绘制区域

DisplayStyle

定义是否为这个相显示文本,为ToolStripItemDisplayStyle枚举

Enabled

是否对用户交互做出响应

Image

所显示的图像

MergeAction

如何合并到一个目标工具条

Parent

父控件

Text

项相关联的文本

ToolTipText

提示文本,AutoToolTip设为true,使用Text设置提示文本

Visible

该项与子项是否可见

公共方法

Invalidate

全部或部分重绘

PerformClick

调用Click事件

公共事件

Click

点击

DragDrop

一个拖放操作完成时

MouseUp

位于项边界之内释放鼠标

Paint

绘制

 

ToolStripButton:显示一个按钮

ToolStripControlHost:几乎可以容纳所有的Forms控件。

ToolStripDropDownItem:下拉式菜单,ToolStripMenuItem是其子类。

 

转载于:https://www.cnblogs.com/xyb930826/p/5303913.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值