窗体代码.docx

上传人:b****8 文档编号:29663775 上传时间:2023-07-26 格式:DOCX 页数:144 大小:75.17KB
下载 相关 举报
窗体代码.docx_第1页
第1页 / 共144页
窗体代码.docx_第2页
第2页 / 共144页
窗体代码.docx_第3页
第3页 / 共144页
窗体代码.docx_第4页
第4页 / 共144页
窗体代码.docx_第5页
第5页 / 共144页
点击查看更多>>
下载资源
资源描述

窗体代码.docx

《窗体代码.docx》由会员分享,可在线阅读,更多相关《窗体代码.docx(144页珍藏版)》请在冰豆网上搜索。

窗体代码.docx

窗体代码

下面的代码示例启用窗体的自动滚动、调整窗体的大小并确保在调整窗体的大小后按钮仍然可见。

 该示例要求有一个 Form,其上带有名为 button2 的 Button。

PrivateSubResizeForm()

'Enableauto-scrollingfortheform.

Me.AutoScroll=True

'Resizetheform.

DimrAsRectangle=Me.ClientRectangle

'Subtract100pixelsfromeachsideoftheRectangle.

r.Inflate(-100,-100)

Me.Bounds=Me.RectangleToScreen(r)

'Makesurebutton2isvisible.

Me.ScrollControlIntoView(button2)

EndSub

下面的代码示例为窗体的 Resize 事件创建事件处理程序

PrivateSubMyForm_Resize(senderAsObject,eAsEventHandler)

'Setthesizeofbutton1tothesizeoftheclientareaoftheform.

button1.Size=Me.ClientSize

EndSub'MyForm_Resize

单击和松开鼠标右键时分配给 TreeView 的 ContextMenu。

 

该代码要求有一个 Form,其上带有 TreeView。

 它还要求已经为 TreeView 的 ContextMenu 属性分配了一个ContextMenu。

PrivateSubtreeView1_MouseUp(senderAsObject,_

eAsMouseEventArgs)HandlestreeView1.MouseUp

'Iftherightmousebuttonwasclickedandreleased,

'displaytheshortcutmenuassignedtotheTreeView.

Ife.Button=MouseButtons.RightThen

treeView1.ContextMenu.Show(treeView1,NewPoint(e.X,e.Y))

EndIf

EndSub

如何向 ContextMenuStrip 添加三个 ToolStripMenuItem 对象。

 

此外,还演示了如何设置窗体的 ContextMenuStrip 属性

ImportsSystem

ImportsSystem.Collections.Generic

ImportsSystem.ComponentModel

ImportsSystem.Data

ImportsSystem.Drawing

ImportsSystem.Text

ImportsSystem.Windows.Forms

PublicClassForm1

InheritsForm

PrivatecontextMenuStrip1AsContextMenuStrip

PrivatetoolStripMenuItem1AsToolStripMenuItem

PrivatetoolStripMenuItem2AsToolStripMenuItem

PrivatetoolStripMenuItem3AsToolStripMenuItem

PrivatecomponentsAsIContainer

 

PublicSubNew()

InitializeComponent()

EndSub

_

SharedSubMain()

Application.EnableVisualStyles()

Application.SetCompatibleTextRenderingDefault(False)

Application.Run(NewForm1())

EndSub

 

PrivateSubInitializeComponent()

Mponents=NewSystem.ComponentModel.Container()

Me.contextMenuStrip1=NewSystem.Windows.Forms.ContextMenuStrip(Mponents)

Me.toolStripMenuItem1=NewSystem.Windows.Forms.ToolStripMenuItem()

Me.toolStripMenuItem2=NewSystem.Windows.Forms.ToolStripMenuItem()

Me.toolStripMenuItem3=NewSystem.Windows.Forms.ToolStripMenuItem()

Me.contextMenuStrip1.SuspendLayout()

Me.SuspendLayout()

'

'contextMenuStrip1

'

Me.contextMenuStrip1.Items.AddRange(NewSystem.Windows.Forms.ToolStripItem(){Me.toolStripMenuItem1,Me.toolStripMenuItem2,Me.toolStripMenuItem3})

Me.contextMenuStrip1.Name="contextMenuStrip1"

Me.contextMenuStrip1.Size=NewSystem.Drawing.Size(180,70)

'

'toolStripMenuItem1

'

Me.toolStripMenuItem1.Name="toolStripMenuItem1"

Me.toolStripMenuItem1.Size=NewSystem.Drawing.Size(179,22)

Me.toolStripMenuItem1.Text="toolStripMenuItem1"

'

'toolStripMenuItem2

'

Me.toolStripMenuItem2.Name="toolStripMenuItem2"

Me.toolStripMenuItem2.Size=NewSystem.Drawing.Size(179,22)

Me.toolStripMenuItem2.Text="toolStripMenuItem2"

'

'toolStripMenuItem3

'

Me.toolStripMenuItem3.Name="toolStripMenuItem3"

Me.toolStripMenuItem3.Size=NewSystem.Drawing.Size(179,22)

Me.toolStripMenuItem3.Text="toolStripMenuItem3"

'

'Form1

'

Me.ClientSize=NewSystem.Drawing.Size(292,273)

Me.ContextMenuStrip=Me.contextMenuStrip1

Me.Name="Form1"

Me.contextMenuStrip1.ResumeLayout(False)

Me.ResumeLayout(False)

EndSub

EndClass

使用 ControlBox、FormBorderStyle、MaximizeBox、MinimizeBox 和 StartPosition 属性创建一个没有任何边框或标题框的窗体。

 在此示例中创建的窗体可用于创建应用程序的启动画面屏幕。

 此示例要求在窗体类中定义此示例的方法并在初始化窗体时调用此方法。

PublicSubCreateMyBorderlesWindow()

FormBorderStyle=FormBorderStyle.None

MaximizeBox=False

MinimizeBox=False

StartPosition=FormStartPosition.CenterScreen

'Removethecontrolboxsotheformwillonlydisplayclientarea.

ControlBox=False

EndSub'CreateMyBorderlesWindow

从派生类 Panel 的 Control.ControlCollection 中移除一个 Control(如果它是该集合的成员)。

 

该示例要求已在 Form 上创建了一个 Panel、一个 Button 以及至少一个 RadioButton 控件。

 将 RadioButton 控件添加到 Panel 控件,而将 Panel 控件添加到 Form。

 单击该按钮时,从 Control.ControlCollection 中移除名为 removeButton 的单选按钮。

'RemovetheRadioButtoncontrolifitexists.

PrivateSubRemoveButton_Click(ByValsenderAsSystem.Object,_

ByValeAsSystem.EventArgs)HandlesRemoveButton.Click

IfPanel1.Controls.Contains(RemoveButton)Then

Panel1.Controls.Remove(RemoveButton)

EndIf

EndSub

如何使用 Control.Cursor 属性、Cursor 类和 Cursors 类来更改鼠标光标。

 本示例创建了一个包含 ComboBox 控件、Panel 控件和 ListView 控件的窗体。

 ComboBox 包含了由 Cursors类提供的所有光标。

 当用户在 ComboBox 中选择了鼠标光标时,Control.Cursor 属性将设置为选定的光标,这将更新 Panel 的光标。

 每次发生 Control.CursorChanged 事件时,都会更新 ListView。

ImportsSystem

ImportsSystem.Drawing

ImportsSystem.Windows.Forms

NamespaceMCursor

'SummarydescriptionforForm1.

PublicNotInheritableClassForm1

InheritsSystem.Windows.Forms.Form

FriendWithEventscursorSelectionComboBoxAsSystem.Windows.Forms.ComboBox

FriendWithEventstestPanelAsSystem.Windows.Forms.Panel

Privatelabel1AsSystem.Windows.Forms.Label

Privatelabel2AsSystem.Windows.Forms.Label

PrivatecursorEventViewerAsSystem.Windows.Forms.ListView

Privatelabel3AsSystem.Windows.Forms.Label

_

PublicSharedSubMain()

System.Windows.Forms.Application.Run(NewForm1)

EndSub'Main

PublicSubNew()

Me.cursorSelectionComboBox=NewSystem.Windows.Forms.ComboBox

Me.testPanel=NewSystem.Windows.Forms.Panel

Me.label1=NewSystem.Windows.Forms.Label

Me.label2=NewSystem.Windows.Forms.Label

Me.cursorEventViewer=NewSystem.Windows.Forms.ListView

Me.label3=NewSystem.Windows.Forms.Label

'SelectCursorLabel

Me.label2.Location=NewSystem.Drawing.Point(24,16)

Me.label2.Size=NewSystem.Drawing.Size(80,16)

Me.label2.Text="Selectcursor:

"'

'CursorTestingPanelLabel

Me.label1.Location=NewSystem.Drawing.Point(24,80)

Me.label1.Size=NewSystem.Drawing.Size(144,23)

Me.label1.Text="Cursortestingpanel:

"

'CursorChangedEventsLabel

Me.label3.Location=NewSystem.Drawing.Point(184,16)

Me.label3.Size=NewSystem.Drawing.Size(128,16)

Me.label3.Text="Cursorchangedevents:

"

'CursorSelectionComboBox

Me.cursorSelectionComboBox.Location=NewSystem.Drawing.Point(24,40)

Me.cursorSelectionComboBox.Size=NewSystem.Drawing.Size(152,21)

Me.cursorSelectionComboBox.TabIndex=0

'CursorTestPanel

Me.testPanel.BackColor=System.Drawing.SystemColors.ControlDark

Me.testPanel.Location=NewSystem.Drawing.Point(24,104)

Me.testPanel.Size=NewSystem.Drawing.Size(152,160)

'CursorEventListView

Me.cursorEventViewer.Location=NewSystem.Drawing.Point(184,40)

Me.cursorEventViewer.Size=NewSystem.Drawing.Size(256,224)

Me.cursorEventViewer.TabIndex=4

Me.cursorEventViewer.View=System.Windows.Forms.View.List

'Setuphowtheformshouldbedisplayedandaddthecontrolstotheform.

Me.ClientSize=NewSystem.Drawing.Size(456,286)

Me.Controls.AddRange(NewSystem.Windows.Forms.Control(){Me.label3,_

Me.cursorEventViewer,Me.label2,Me.label1,_

Me.testPanel,Me.cursorSelectionComboBox})

Me.Text="CursorsExample"

'Addallthecursortypestothecombobox.

DimcursorAsCursor

ForEachcursorInCursorList()

cursorSelectionComboBox.Items.Add(cursor)

Nextcursor

EndSub'New

PrivateFunctionCursorList()AsCursor()

'MakeanarrayofallthetypesofcursorsinWindowsForms.

returnNewCursor(){Cursors.AppStarting,Cursors.Arrow,Cursors.Cross,_

Cursors.Default,Cursors.Hand,Cursors.Help,_

Cursors.HSplit,Cursors.IBeam,Cursors.No,_

Cursors.NoMove2D,Cursors.NoMoveHoriz,Cursors.NoMoveVert,_

Cursors.PanEast,Cursors.PanNE,Cursors.PanNorth,_

Cursors.PanNW,Cursors.PanSE,Cursors.PanSouth,_

Cursors.PanSW,Cursors.PanWest,Cursors.SizeAll,_

Cursors.SizeNESW,Cursors.SizeNS,Cursors.SizeNWSE,_

Cursors.SizeWE,Cursors.UpArrow,Cursors.VSplit,Cursors.WaitCursor}

EndFunction

PrivateSubcursorSelectionComboBox_SelectedIndexChanged(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlescursorSelectionComboBox.SelectedIndexChanged

'Setthecursorinthetestpaneltobetheselectedcursorstyle.

testPanel.Cursor=CType(cursorSelectionComboBox.SelectedItem,Cursor)

EndSub

PrivateSubtestPanel_CursorChanged(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlestestPanel.CursorChanged

'Buildupastringcontainingthetypeofobjectsendingtheevent,andtheevent.

DimcursorEventAsString=String.Format("[{0}]:

{1}",sender.GetType().ToString(),"Cursorchanged")

'Recordsthiseventinthelistview.

Me.cursorEventViewer.Items.Add(cursorEvent)

EndSub

EndClass'Form1

EndNamespace'MCursor

下面的示例在窗体中以正常大小绘制指定的光标,在拉伸模式中以正常大小的两倍绘制。

 该示例假定在调用此方法时有可传入到此方法中的 Form 和 Cursor 对象。

PrivateSubDrawCursorsOnForm(cursorAsCursor)

'Iftheform'scursorisnottheHandcursorandthe

'CurrentcursoristheDefault,Drawthespecified

'cursorontheforminnormalsizeandtwicenormalsize.

If(NotMe.Cursor.Equals(Cursors.Hand))And_

Cursor.Current.Equals(Cursors.Default)Then

'Drawthecursorstretched.

DimgraphicsAsGraphics=Me.CreateGraphics()

DimrectangleAsNewRectangle(NewPoint(10,10),_

NewSize(cursor.Size.Width*2,cursor.Size.Height*2))

cursor.DrawStretched(graphics,rectangle)

'Drawthecursorinnormalsize.

rectangle.Location=NewPoint(rectangle.Width+_

rectangle.Location.X,rectangle.Height+rectangle.Location.Y)

rectangle.Size=cursor.Size

cursor.Draw(graphics,rectangle)

'Disposeofthecursor.

cursor.Dispose()

EndIf

EndSub

用户的可用逻辑驱动器填充 ComboBox。

 该示例还设置了组合框的 Cursor 属性,使得当鼠标指针位于下拉按钮上时显示 Cursors.Hand 光标。

 该代码要求有一个 Form,其上带有ComboBox。

PrivateSubForm1_Load(senderAsObject,_

eAsEventArgs)HandlesMyBase.Load

'Displaythehandcursorwhenthemousepointer

'isoverthecomboboxdrop-downbutton.

comboBox1.Cur

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 职业教育 > 其它

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1