VB.Net/GUI/Frame Form

Материал из VB Эксперт
Перейти к: навигация, поиск

Form Opacity in VB

<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Resources Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents timer1 As System.Windows.Forms.Timer
   Friend WithEvents label1 As System.Windows.Forms.Label
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.ruponents = New System.ruponentModel.Container()
       Me.timer1 = New System.Windows.Forms.Timer(Me.ruponents)
       Me.label1 = New System.Windows.Forms.Label()
       Me.SuspendLayout()
       "
       "timer1
       "
       "
       "label1
       "
       Me.label1.Location = New System.Drawing.Point(96, 24)
       Me.label1.Name = "label1"
       Me.label1.TabIndex = 1
       Me.label1.Text = "Click On Me!"
       Me.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 70)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.label1})
       Me.Name = "Form1"
       Me.Opacity = 0.7
       Me.Text = "Opacity = 0.5"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
       " timer1.Enabled = True
   End Sub
   Private Sub timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer1.Tick
       If Me.Opacity < 1.0 Then Me.Opacity = Me.Opacity + 0.1
       Me.Text = "Opacity = " & Me.Opacity.ToString()
   End Sub
   Private Sub Form1_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate
       timer1.Enabled = False
   End Sub
   Private Sub label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles label1.Click
       If Me.Opacity < 1.0 Then Me.Opacity = Me.Opacity + 0.1
       Me.Text = "Opacity = " & Me.Opacity.ToString()
   End Sub

End Class

Public Class MainClass

   Shared Sub Main()
       Dim myform As Form = New Form1()
       Application.Run(myform)
   End Sub

End Class


 </source>


Get Frame Client Rectangle Size

<source lang="vbnet"> Imports System Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Text Imports System.Globalization Imports System.Text Imports System.Collections Public Class MainClass

   Shared Sub Main()
       Dim myform As Form = New Form1()
       Application.Run(myform)
   End Sub

End Class Public Class Form1

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Button1.Location = New Point( _
           Me.ClientRectangle.Width - Button1.Width, _
           Me.ClientRectangle.Height - Button1.Height)
       Button1.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
   End Sub

End Class <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing AndAlso components IsNot Nothing Then
           components.Dispose()
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.Button1 = New System.Windows.Forms.Button
       Me.SuspendLayout()
       "
       "Button1
       "
       Me.Button1.Location = New System.Drawing.Point(24, 24)
       Me.Button1.Name = "Button1"
       Me.Button1.Size = New System.Drawing.Size(75, 23)
       Me.Button1.TabIndex = 0
       Me.Button1.Text = "Button1"
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(185, 84)
       Me.Controls.Add(Me.Button1)
       Me.Name = "Form1"
       Me.Text = "Form1"
       Me.ResumeLayout(False)
   End Sub
   Friend WithEvents Button1 As System.Windows.Forms.Button

End Class


 </source>


Non rectangle window in VB

<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Collections Imports System.ruponentModel Imports System.Drawing.Drawing2D Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Resources Public Class TransparentForm

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
       Me.SetStyle(ControlStyles.ResizeRedraw, True)
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(208, 68)
       Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
       Me.Name = "TransparentForm"
       Me.Text = "Transparency Test"
   End Sub
  1. End Region
   Private Sub TransparentForm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
       Dim g As Graphics = e.Graphics
       Dim _pen As Pen = New Pen(Color.DarkBlue, 4)
       Dim rect As Rectangle = Me.ClientRectangle
       rect.Inflate(-2, -2)
       g.FillEllipse(Brushes.Yellow, rect)
       g.DrawEllipse(_pen, rect)
       _pen.Dispose()
       Dim _font As Font = New Font(Me.Font, FontStyle.Bold)
       Dim format As StringFormat = New StringFormat()
       format.Alignment = StringAlignment.Center
       format.LineAlignment = StringAlignment.Center
       g.DrawString("Window", _font, Brushes.DarkBlue, RectangleF.op_Implicit(Me.ClientRectangle), format)
       _font.Dispose()
   End Sub
   Sub SetEllipseRegion()
       Dim rect As Rectangle = Me.ClientRectangle
       Dim path As GraphicsPath = New GraphicsPath()
       path.AddEllipse(RectangleF.op_Implicit(rect))
       Me.Region = New Region(path)
       path.Dispose()
   End Sub
   Private Sub TransparentForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
       SetEllipseRegion()
   End Sub
   Private Sub TransparentForm_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
       SetEllipseRegion()
   End Sub

End Class Public Class MainClass

   Shared Sub Main()
       Dim myform As Form = New TransparentForm()
       Application.Run(myform)
   End Sub

End Class


 </source>


Resetting a Form

<source lang="vbnet"> Imports System.Windows.Forms Module MainModule

   Public Sub ResetForm(ByVal FormToReset As Form)
       Dim objControl As Control
       For Each objControl In FormToReset.Controls
           If TypeOf (objControl) Is System.Windows.Forms.TextBox Then
               objControl.Text = "" " Clear TextBox
           ElseIf TypeOf (objControl) Is System.Windows.Forms.CheckBox Then
               Dim objCheckBox As System.Windows.Forms.CheckBox = objControl
               objCheckBox.Checked = False " Uncheck CheckBox
           ElseIf TypeOf (objControl) Is System.Windows.Forms.ruboBox Then
               Dim objComboBox As System.Windows.Forms.ruboBox = objControl
               objComboBox.SelectedIndex = -1 " Deselect any ComboBox entry
           End If
       Next
   End Sub

End Module


 </source>


Simple window frame

<source lang="vbnet"> Imports System Imports System.Windows.Forms Class MyFirstForm

   Inherits Form
   Private WithEvents mybutton As Button
   Public Sub New()
       Me.Text = "Hello, WinForms!"
       mybutton = New Button()
       mybutton.Text = "Click Me!"
       Me.Controls.Add(mybutton)
   End Sub
   Public Sub mybutton_Click(sender As Object, e As EventArgs) Handles mybutton.Click
       MessageBox.Show("Message")
   End Sub
   Shared Sub Main()
       Dim myform As Form = New MyFirstForm()
       Application.Run(myform)
   End Sub

End Class


 </source>


Snap to Control

<source lang="vbnet"> Imports System.Windows.Forms Imports System.Drawing Public Class Form1

   Inherits System.Windows.Forms.Form
   Public Sub New()
       MyBase.New()
       InitializeComponent()
   End Sub
   Friend WithEvents Button1 As System.Windows.Forms.Button
   Friend WithEvents LinkLabel1 As System.Windows.Forms.LinkLabel
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
       Me.Button1 = New System.Windows.Forms.Button()
       Me.LinkLabel1 = New System.Windows.Forms.LinkLabel()
       Me.SuspendLayout()
       "
       Me.Button1.Image = CType(resources.GetObject("Button1.Image"), System.Drawing.Bitmap)
       Me.Button1.Location = New System.Drawing.Point(192, 48)
       Me.Button1.Size = New System.Drawing.Size(168, 64)
       "
       Me.LinkLabel1.Location = New System.Drawing.Point(24, 16)
       Me.LinkLabel1.Text = "Click here to snap!"
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(544, 182)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.LinkLabel1, Me.Button1})
       Me.Text = "Snapping to a Control"
       Me.ResumeLayout(False)
   End Sub
   Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
       SnapToControl(Button1)
   End Sub
   Public Sub SnapToControl(ByVal Control As Control)
       Dim objPoint As Point = Control.PointToScreen(New Point(0, 0))
       Cursor.Position = objPoint
   End Sub

End Class


 </source>