拉风的Lee 2019-04-18 13:46 采纳率: 33.3%
浏览 1493
已采纳

vb.net 如何让label中的文字旋转90°显示呢?

Public Class MyLabel

Inherits Label   

    Protected Overrides Sub OnPaint(e As PaintEventArgs) 

    e.Graphics.RotateTransform(45)

    e.Graphics.DrawEllipse(New Pen(Color.Red), New Rectangle(2, 2, 10, 10))

 MyBase.OnPaint(e)

    End Sub

    End Class

以上代码只能绘制一个label,并不能旋转90°,如何实现90°旋转呢??


  • 写回答

2条回答 默认 最新

  • threenewbee 2019-04-18 15:16
    关注

    采纳本回答,留下email,发给你完整代码

    其他人需要,可以从 https://download.csdn.net/download/caozhy/11126264 下载

    图片说明

    使用我的程序,只要填加一个类和FormLoad/Closing少量代码,把你label的名字前缀设置为vlbl开头,运行的时候自动旋转。不需要单独设置,不需要新控件

    
    Public Class Form1
    
        Private sclist As New List(Of SubClassing)
    
        Private Const WM_PAINT As Integer = &HF
    
        Private Sub sc_CallBackProc(ByVal h As IntPtr, ByRef m As System.Windows.Forms.Message)
            Select Case m.Msg
                Case WM_PAINT
                    Dim l As Label = Controls.OfType(Of Label).First(Function(x) x.Handle = h)
                    If Not (l Is Nothing) Then
                        Dim g As Graphics = l.CreateGraphics()
                        g.FillRectangle(New SolidBrush(l.BackColor), 0, 0, l.Width, l.Height)
                        Dim mat = g.Transform
                        Dim rmat = g.Transform
                        rmat.RotateAt(90, New PointF(l.Width / 2, l.Height / 2))
                        g.Transform = rmat
                        g.DrawString(l.Text, l.Font, Brushes.Black, New PointF(0, 0))
                        g.Transform = mat
                    End If
            End Select
        End Sub
    
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            For Each item As SubClassing In sclist
                item.ReleaseHandle()
            Next
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For Each l As Label In Controls.OfType(Of Label)()
                If l.Name.StartsWith("vlbl") Then
                    With l
                        l.AutoSize = False
                        l.Size = New Size(l.Size.Width, l.Size.Width)
                        Dim sc As SubClassing = New SubClassing(l.Handle)
                        sc.SubClass = True
                        AddHandler sc.CallBackProc, AddressOf sc_CallBackProc
                        sclist.Add(sc)
                    End With
                End If
            Next
        End Sub
    End Class
    
    
    '来自 https://www.codeproject.com/articles/3234/subclassing-in-net-the-pure-net-way
    
    Public Class SubClassing
        Inherits System.Windows.Forms.NativeWindow
    
        'Event Declaration. This event will be raised when any  
        'Message will be posted to the Control
        Public Event CallBackProc(ByVal hwnd As IntPtr, ByRef m As Message)
    
        'Flag which indicates that either Event should be 
        'raised or not
        Private m_Subclassed As Boolean = False
    
        'During Creation of Object of this class, Pass the Handle 
        'of Control which you want to SubClass
        Public Sub New(ByVal handle As IntPtr)
            MyBase.AssignHandle(handle)
        End Sub
    
        'Terminate The SubClassing
        'There is no need to Create this Method. Cuz, 
        'when you will create the Object
        'Of this class, You will have the Method Named ReleaseHandle.
        'Just call that as you can see in this Sub
    
        'Public Sub RemoveHandle()
        '    MyBase.ReleaseHandle()
        'End Sub
    
        'To Enable or Disable Receiving Messages
        Public Property SubClass() As Boolean
            Get
                Return m_Subclassed
            End Get
            Set(ByVal Value As Boolean)
                m_Subclassed = Value
            End Set
        End Property
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m_Subclassed Then 'If Subclassing is Enabled 
                MyBase.WndProc(m)
                RaiseEvent CallBackProc(MyBase.Handle, m) 'then RaiseEvent
            End If
        End Sub
    
        Protected Overrides Sub Finalize()
            MyBase.Finalize()
        End Sub
    End Class
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器