📄 form1.vb
字号:
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
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
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents line As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu()
Me.line = New System.Windows.Forms.MenuItem()
Me.MenuItem1 = New System.Windows.Forms.MenuItem()
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.line, Me.MenuItem1})
'
'line
'
Me.line.Index = 0
Me.line.Text = "画直线段"
'
'MenuItem1
'
Me.MenuItem1.Index = 1
Me.MenuItem1.Text = "拾取"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.BackColor = System.Drawing.Color.White
Me.ClientSize = New System.Drawing.Size(440, 366)
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
<DllImport("gdi32.dll")> _
Private Shared Function SetROP2(ByVal hdc As IntPtr, _
ByVal nDrawMode As Integer) As Boolean
End Function
<DllImport("gdi32.dll")> _
Private Shared Function Rectangle _
(ByVal hdc As IntPtr, ByVal X1 As Integer, ByVal Y1 As Integer, _
ByVal X2 As Integer, ByVal Y2 As Integer) As Boolean
End Function
Private m_StartX, m_StartY As Single
Private m_EndX, m_EndY As Single
Private m_Step As Integer = 0
Protected Overrides Sub OnMouseDown(ByVal e As Windows.Forms.MouseEventArgs)
Dim g As Graphics = Me.CreateGraphics()
Dim hdc As IntPtr = g.GetHdc()
m_Step += 1
SetROP2(hdc, 10)
If m_Step = 1 Then
m_StartX = e.X
m_StartY = e.Y
m_EndX = e.X
m_EndY = e.Y
ElseIf m_Step = 2 Then
Rectangle(hdc, m_StartX, m_StartY, m_EndX, m_EndY)
Rectangle(hdc, m_StartX, m_StartY, e.X, e.Y)
m_Step = 0
End If
g.ReleaseHdc(hdc)
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As Windows.Forms.MouseEventArgs)
Dim g As Graphics = Me.CreateGraphics()
Dim prex As Single, prey As Single
If m_Step = 1 Then
Dim hdc As IntPtr = g.GetHdc()
prex = m_EndX
prey = m_EndY
SetROP2(hdc, 10)
Rectangle(hdc, m_StartX, m_StartY, prex, prey)
Rectangle(hdc, m_StartX, m_StartY, e.X, e.Y)
m_EndX = e.X
m_EndY = e.Y
g.ReleaseHdc(hdc)
End If
End Sub
Protected Overrides Sub OnPaint(ByVal e As Windows.Forms.PaintEventArgs)
Dim g As Graphics = e.Graphics
g.FillRectangle(Brushes.White, Me.ClientRectangle)
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -