WinCE VB.NET – 解決 Label 背景無法套用 PictureBox 的方法

WinCE上開發軟體和PC上開發軟體用法上有一些限制…

這次遇到了Label 背景無法套用 PictureBox 的問題

因為From也無法設定背景….

只好另尋方法…

還好…董哥找到了解決方式…

原文出處:MakeIT II

Step1. 先加入定義變數


Imports System.Drawing.Imaging
Imports System.Drawing

Public Class Form1
Dim targetOffset As Point
Dim drawBounds As Rectangle
Dim sizeTmp As SizeF
Dim intleft As Integer

 

Step2. 加入兩個副程式


Private Sub DrawLabel(ByRef labSource As Label, ByRef picTarget As Form, ByRef grx As Graphics)
targetOffset = picTarget.Location
drawBounds = labSource.Bounds

drawBounds.X -= picTarget.Location.X
drawBounds.Y -= picTarget.Location.Y

If (labSource.TextAlign = ContentAlignment.TopLeft) Then
grx.DrawString(labSource.Text, labSource.Font, New SolidBrush(labSource.ForeColor), drawBounds)
ElseIf (labSource.TextAlign = ContentAlignment.TopCenter) Then
sizeTmp = grx.MeasureString(labSource.Text, labSource.Font)
intleft = Me.Width / 2 - CInt(sizeTmp.Width) / 2
intleft -= picTarget.Location.X
Dim rect As Rectangle = New Rectangle(Left, labSource.Top, CInt(sizeTmp.Width), CInt(labSource.Height))
grx.DrawString(labSource.Text, labSource.Font, New SolidBrush(labSource.ForeColor), rect)
ElseIf (labSource.TextAlign = ContentAlignment.TopRight) Then
sizeTmp = grx.MeasureString(labSource.Text, labSource.Font)
intleft = labSource.Width - CInt(sizeTmp.Width) + labSource.Left
Left -= picTarget.Location.X
Dim rect As Rectangle = New Rectangle(Left, labSource.Top, CInt(sizeTmp.Width), CInt(sizeTmp.Height))
grx.DrawString(labSource.Text, labSource.Font, New SolidBrush(labSource.ForeColor), rect)
End If
End Sub

 

Public Sub PictureBox_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
''刷新文字說明
DrawLabel(Label1, Me, e.Graphics)
''若有多個Label,↑這邊加入
End Sub

 

Step3. 呼叫方式


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

AddHandler PictureBox1.Paint, AddressOf PictureBox_Paint
           ''要新增的事件           要執行的事件程序名
End Sub

 

Step4. 注意事項


‧在 PictureBox 或是 Form 上先擺上一個隱藏的Label,也就是visable=false

 

0 留言

發表留言 »

姓名
信箱
網站