換成form1的Load事件就不能畫圖了:
為志丹等地區(qū)用戶提供了全套網頁設計制作服務,及志丹網站建設行業(yè)解決方案。主營業(yè)務為成都網站設計、做網站、志丹網站設計,以傳統(tǒng)方式定制建設網站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
是因為form1在Load的時候,它還不能創(chuàng)建圖形界面,也就不能執(zhí)行 Me.CreateGraphics
分類: 電腦/網絡 程序設計 其他編程語言
問題描述:
VB6中的form1.circle (100,200),rgb(0,255,0)的語句如何在VB中使用???
急用?。。。。。。。?!
解析:
VB與VB不同。
VB已經有專門繪圖的類。
可以定義筆刷然后用Drawing類中的方法繪制。
Private Sub DrawEllipse()
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics as System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.DrawEllipse(myPen, New Rectangle(0,0,200,300))
myPen.Dispose()
formGraphics.Dispose()
End Sub
Private Sub DrawRectangle()
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics as System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.DrawRectangle(myPen, New Rectangle(0,0,200,300))
myPen.Dispose()
formGraphics.Dispose()
End Sub
項目中要用圖形來顯示一個閥門的開度,以及控制閥的開度。本來是要用什么公司買的控件中的餅圖之類的實現(xiàn),不過我覺得也是麻煩,就想,還是自己畫吧。
首先添加一個TrackBar,名字是“TrackBar1”,一個label,名字是“L_A_SHANG”,一個GroupBox,名字是“GB_RIGHT”,,然后添加拉動滾動條時的處理函數(shù)
'上位機控制
Dim m_start As Integer
Dim RcDraw As System.Drawing.Rectangle
Private Sub TrackBar1_Scroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
m_start = (100 - TrackBar1.Value)
Me.L_A_SHANG.Text = Me.TrackBar1.Value.ToString
GB_RIGHT.Invalidate() '重畫GB_RIGHT
End Sub
添加GB_RIGHT重畫時的處理函數(shù)
Private Sub GB_RIGHT_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GB_RIGHT.Paint
RcDraw.X = TrackBar1.Location.X - 40
RcDraw.Y = 20
RcDraw.Height = 100
RcDraw.Width = 20
e.Graphics.DrawRectangle(New Pen(Color.Blue, 5), RcDraw)
Dim i = TrackBar1.Location.X - 40
For i = TrackBar1.Location.X - 40 To TrackBar1.Location.X - 20
e.Graphics.DrawLine(New Pen(Color.Blue, 5), i, m_start + 20, i, 120)
Next
Me.L_A_SHANG.Text = Me.TrackBar1.Value.ToString + "%"
End Sub
到此以及可以實現(xiàn)拉動條的時候,圖像跟著變化,并顯示百分比。
Imports System.Drawing.Imaging
Public Class Form1
Dim imageName As String = "C:\Documents and Settings\...\1126.jpg "
Dim i As Image = Image.FromFile(imageName)
Dim g As Graphics = Graphics.FromImage(i) '此處從背景圖創(chuàng)建Greaphics
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'劃線
Dim BluePen As New Pen(Color.Blue, 5)
BluePen.DashStyle = Drawing2D.DashStyle.Solid
g.DrawLine(BluePen, 100.0F, 170, 500.0F, 170)
g.Dispose()
PictureBox1.Image = i
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'退出
Me.Close()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'存盤
i.Save( "C:\testimage.jpg ", ImageFormat.Jpeg)
i.Dispose()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
PictureBox1.Image = i
End Sub
End Class