給你個udp多播例子,廣播不是很清楚,呵呵
創(chuàng)新互聯(lián)建站-專業(yè)網站定制、快速模板網站建設、高性價比和靜網站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式和靜網站制作公司更省心,省錢,快速模板網站建設找我們,業(yè)務覆蓋和靜地區(qū)。費用合理售后完善,十載實體公司更值得信賴。
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Inherits System.Windows.Forms.Form
Dim port As String
Dim ipadd As String
Dim ipend As IPEndPoint
Dim sendudp As New UdpClient()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim ipadress As IPAddress
ipadress = IPAddress.Parse(TextBox1.Text)
'sendport = Int32.Parse(TextBox2.Text)
'ipend = New IPEndPoint(ipadress, sendport)
Try
sendudp.JoinMulticastGroup(ipadress)
MessageBox.Show("啟動完成!")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
PrintDocument1.Print()
使用PrintDocument進行打印“Brush, 50, 80”50左邊距離,80上面距離
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim mypen As Pen = New Pen(Color.Blue, 2)
e.Graphics.DrawString("取號單", New Font("Microsoft Sans Serif", 18, FontStyle.Bold), New Pen(Color.Black, 1).Brush, 30, 30)
e.Graphics.DrawString("XXXXXX", New Font("Microsoft Sans Serif", 24, FontStyle.Regular), New Pen(Color.Black, 1).Brush, 50, 80)
e.Graphics.DrawString("您的號碼是:", New Font("Microsoft Sans Serif", 16, FontStyle.Regular), New Pen(Color.Black, 1).Brush, 30, 135)
e.Graphics.DrawString("打印時間:" Now(), New Font("Microsoft Sans Serif", 10, FontStyle.Regular), New Pen(Color.Black, 1).Brush, 80, 330)
End Sub
Public?Class?Form2
Dim?test?As?String
Public?Sub?New(ByVal?_test?As?String)
test?=?_test
End?Sub
End?Class
Form1 中 New Form2("abc") 即可傳參給 Form2 中的 test。
但在 VB.NET 中,沒必要這么麻煩,只需要聲明為 Public,即可直接方法,如:
Public?Class?Form2
Public?test?As?String
End?Class
Form1 中直接 Form2.test = "abc" 即可。
就
是
這
個
了
!
Hand類的代碼:
Public MustInherit Class Hand
Protected gp As GraphicsPath = New GraphicsPath()
Protected gpBase As GraphicsPath = Nothing
Protected midX As Integer = 150 ‘默認的窗體
Protected midY As Integer = 150 ‘中心位置
‘構造器,得到窗體中心位置
Public Sub New(ByVal theForm As Form1)
midX = (theForm.ClientRectangle.Left + theForm.ClientRectangle.Right) / 2
midY = (theForm.ClientRectangle.Top + theForm.ClientRectangle.Bottom) / 2
End Sub
MustOverride Sub Transform(ByVal d As DateTime)
‘繪制指針路徑
Overridable Sub Draw(ByVal g As Graphics)
Dim aPen As Pen = New Pen(Brushes.Black, 4F)
g.DrawPath(aPen, gp)
g.FillPath(Brushes.Black, gp)
aPen.Dispose()
End Sub
‘使用矩陣實現(xiàn)路徑(gp)的旋轉
Public Sub Rotate(ByVal angle As Double)
gp = CType(gpBase.Clone(), GraphicsPath)
Dim mTransform As Matrix = New Matrix()
mTransform.RotateAt(CType(angle,Single),NewPointF(midX,midY))
gp.Transform(mTransform)
End Sub
End Class
為了節(jié)省篇幅,上面的代碼省略了引入命名空間的語句。
下面是分針(MinuteHand)類的定義:
Public Class MinuteHand
Inherits Hand
‘構造器,生成繪制分針的路徑(gp)
Public Sub New(ByVal myForm As Form1)
MyBase.New(myForm)
gp.AddLine(midX, midY, midX, 45)
gp.AddLine(midX, 45, midX - 3, 50)
gp.AddLine(midX - 3, 50, midX + 3, 50)
gp.AddLine(midX + 3, 50, midX, 45)
gpBase = CType(gp.Clone(), GraphicsPath)
End Sub
‘Transform方法取得系統(tǒng)當前時間,并旋轉時鐘指針。
Public Overrides Sub Transform(ByVal d As DateTime)
Dim minuteTime As Double = (CDbl(d.Minute) + CDbl(d.Second / 60))
Dim angle As Double = (CDbl(minuteTime) / 60) * 360
Rotate(angle)
End Sub
End Class
對所有的指針旋轉的方法都是相同的,因此在基類中實現(xiàn)。由于時針和秒針的實現(xiàn)與分針相似,所不同者,只在于構造器中繪制的指針路徑不同和Transform方法中轉動的角度不同,在這里就不在贅述了。
另外還需要提一下的是畫時鐘表面的代碼,時鐘表面用ClockFace類來實現(xiàn)。這個類首先畫一個圓代表時鐘,然后畫上米老鼠的圖案,最后在相應的位置畫上數(shù)字1~12代表12個小時。
Public Sub Draw(ByVal g As Graphics)
DrawClockFace(g)
DrawImage(g)
DrawNumbers(g)
DrawPin(g)
End Sub
下面是ClockFace類的屬性:
Private ClockRectangle As Rectangle
Private ClockFont As Font = New Font("Arial", 12)
Private midPoint As Point
Private ClockImage As Bitmap
Private Const IMAGEX As Integer = 50
Private Const IMAGEY As Integer = 50
DrawClockFace方法用來畫時鐘表面:
Private Sub DrawClockFace(ByVal g As Graphics)
g.FillEllipse(Brushes.White, ClockRectangle.Left + 10, ClockRectangle.Top + 10, ClockRectangle.Width - 20, ClockRectangle.Height - 20)
g.DrawEllipse(Pens.Black, ClockRectangle.Left + 10, ClockRectangle.Top + 10, ClockRectangle.Width - 20, ClockRectangle.Height - 20)
End Sub
然后用Graphics對象的DrawImage方法畫出米老鼠的圖片:
Private Sub DrawImage(ByVal g As Graphics)
Dim nWidth As Integer = ClockImage.Width
Dim nHeight As Integer = ClockImage.Height
Dim destRect As Rectangle = New Rectangle(midPoint.X - IMAGEX / 2, midPoint.Y - IMAGEY / 2, IMAGEX, IMAGEY)
g.DrawImage(ClockImage, destRect)
End Sub
數(shù)字在時鐘上的位置是用sin和cos函數(shù)計算的:
Private Sub DrawNumbers(ByVal g As Graphics)
Dim count As Integer = 1
Dim a As Double
For a = 0 To 2 * Math.PI Step 2 * Math.PI / 12
Dim x As Double = (ClockRectangle.Width - 70) / 2 * Math.Cos(a - Math.PI / 3) + (ClockRectangle.Width - 70) / 2 + 25
Dim y As Double = (ClockRectangle.Width - 70) / 2 * Math.Sin(a - Math.PI / 3) + (ClockRectangle.Width - 70) / 2 + 20
g.DrawString(Convert.ToString(count), ClockFont, Brushes.Black, CType(x, Single), CType(y, Single), New StringFormat())
count += 1
Next
End Sub
最后是窗體文件(Form1.vb):
Public Class Form1
Inherits System.Windows.Forms.Form
Private MyMinuteHand As MinuteHand
Private MyHourHand As HourHand
Private MySecondHand As SecondHand
Private TheClockFace As ClockFace
Private FirstTick As Boolean = False
‘在窗體的OnPaint事件中取得Graphics對象
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
If (FirstTick = False) Then Exit Sub
Dim g As Graphics = e.Graphics
TheClockFace.Draw(g)
MyHourHand.Draw(g)
MyMinuteHand.Draw(g)
MySecondHand.Draw(g)
TheClockFace.DrawPin(g)
End Sub
‘計時器事件
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
MySecondHand.Transform(DateTime.Now)
MyHourHand.Transform(DateTime.Now)
MyMinuteHand.Transform(DateTime.Now)
FirstTick = True
Invalidate()
vb.net中如何結束一個線程
一般而言,如果您想終止一個線程,您可以使用System.Threading.Thread類的Abort方法. 例如:
Dim worker As ThreadStart = New ThreadStart(AddressOf workerthreadmethod)
Dim t As Thread = New Thread(worker)
t.Start()
MessageBox.Show("Wait for a while for the thread to start.")
MessageBox.Show(t.ThreadState.ToString())
t.Abort()
MessageBox.Show(t.ThreadState.ToString())
t.Join()
MessageBox.Show(t.ThreadState.ToString())
當然,在調用Abort方法后,線程并不是立刻終止,要等線程的所有finally快中的代碼完成后才會完全終止. 所以在主線程中可以用Join方法來同步,當線程還未完全終止時,t.Join()將處于等待,直到t線程完全結束后再繼續(xù)執(zhí)行后面的語句。
Abort方法是會導致線程跳出一個異常錯誤的,你需要在代碼中捕獲該異常。下面是一個比較完整的VB.NET線程例子:
Imports System
Imports System.Threading
Public Class MyTestApp
Public Shared Sub Main()
Dim t As New Thread(New ThreadStart(AddressOf MyThreadMethod))
'Start the thread
t.Start()
MsgBox("Are you ready to kill the thread?")
'Kill the child thread and this will cause the thread raise an exception
t.Abort()
' Wait for the thread to exit
t.Join()
MsgBox("The secondary thread has terminated.")
End Sub
Shared Sub MyThreadMethod()
Dim i As Integer
Try
Do While True
Thread.CurrentThread.Sleep(1000)
Console.WriteLine("This is the secondary thread running.")
Loop
Catch e As ThreadAbortException
MsgBox("This thread is going to be terminated by the Abort method in the Main function")
End Try
End Sub
End Class
Thread.Abort()方法用來永久銷毀一個線程,而且將拋出ThreadAbortException異常。使終結的線程可以捕獲到異常但是很難控制恢復,僅有的辦法是調用Thread.ResetAbort()來取消剛才的調用,而且只有當這個異常是由于被調用線程引起的異常。因此,A線程可以正確的使用Thread.Abort()方法作用于B線程,但是B線程卻不能調用Thread.ResetAbort()來取消Thread.Abort()操作。