在WinCE下实现透明Label,搞了半天,终于在网上找到正解,还是很不容易的,现在把解决方案理一下。
步骤如下:
(0)建个空工程
(1)拖个PictureBox控件进来,名称为PictureBox1;
(2)拖个Label控件进来,名称为Label1;
(3)将Label控件放到PictureBox上面,并把Visable属性设置成False
(4)在Form1_Load事件里加入以下代码:
AddHandler PictureBox1.Paint, AddressOf Me.pictureBox1_Paint
(5)加入以下变量定义和DrawLabel函数
Dim targetOffset As Point Dim drawBounds As Rectangle Dim sizeTmp As SizeF Dim intleft As Integer 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
(6) 创建pictureBox1_Paint函数,加入以下代码:
Private Sub pictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) DrawLabel(Label1, Me, e.Graphics) End Sub
(7)运行一下吧,看看效果
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->