VB.NET窗体操作技巧两则

来源:互联网 发布:计算机编程可以自学吗 编辑:程序博客网 时间:2024/05/24 23:12
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
VB.NET窗体操作技巧两则

2003-03-18ⷠⷥ蘧𚢥曠刘乐坤 ⷂ𗹥sky



  一、如何拖动没有边框的窗体?

  这个功能在VB6中,需要借助于API函数才能实现。而在VB.NET中,凭自己的功能就能实现。首先设置窗体的FormBorderStyle属性为none以去掉窗体的边框,然后在窗体上添加一个按钮。窗体中的代码如下:

Public Class Form1
 Inherits System.Windows.Forms.Form

 Private mouse_offset As Point
 Private Sub form1_MouseDown(ByVal sender As Object, ByVal e As  System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
  mouse_offset = New Point(e.X, e.Y)
 End Sub

Private Sub form1_MouseMove(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
 '按住鼠标左右键均可拖动窗体
 If e.Button = MouseButtons.Left Or e.Button = MouseButtons.Right Then
  Dim mousePos As Point = Sender.findform().MousePosition
  '获得鼠标偏移量
  mousePos.Offset(-mouse_offset.X, -mouse_offset.Y)
  '设置窗体随鼠标一起移动
  Sender.findform().Location = mousePos
 End If
End Sub

Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 '关闭窗体
 Me.Close()
End Sub
End Class

   二、多个窗体之间互相调用

  在VB6中,多个窗体之间可以很方便地互相调用,如:在Form1中,只需要用一条“Form2.Show” 语句就能显示窗体Form2。然而在VB.NET中窗体处理机制发生了很大的变化:在访问窗体之前,你必须进行窗体实例化;如果在项目中有多处代码访问同一窗体,则你必须把它的同一实例指针传递给这些代码,否则新创建的窗体实例就不再是原先的窗体了。

  下面的代码实现窗体Form1和Form2之间互相调用,Form1为主窗体。Form1上的按钮BtnShowFrm2的标题为“显示Form2”,Form2上的按钮BtnShowFrm1的标题为“显示Form1”。

  1、Form1中的代码:

Public Class Form1
Inherits System.Windows.Forms.Form
'创建Form2的一个新的实例
Dim Frm2 As New Form2()

Public Function Instance2(ByVal frm As Form2)
Frm2 = frm
End Function

Private Sub BtnShowFrm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnShowFrm2.Click
'以下语句保证在Form2以及其他窗体中访问Form1时,
'都将得到Form1的同一个窗体实例。
Frm2.Instance(Me)
Frm2.Show()
Me.Hide()
End Sub

End Class

  2、Form2中的代码:

Public Class Form2
 Inherits System.Windows.Forms.Form
 Dim frm1 As Form1
 '借助一个新增的Instance属性来生成窗体frm1的实例
 Public Function Instance(ByVal frm As Form1)
  frm1 = frm
 End Function

 Private Sub BtnShowFrm1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)    Handles BtnShowFrm1.Click
  Me.Hide()
  frm1.Show()
 End Sub

 Private Sub Form2_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles  MyBase.Closed
  '如果Form2被关闭,则设置Form1的按钮BtnShowFrm2不可用。
  frm1.BtnShowFrm2.Enabled = False
  frm1.Show()
 End Sub
End Class

  以上代码全部在Windows XP,VB.NET下调试通过


VB.NET窗体操作技巧两则';return true">
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>