ALINHAMENTO VERTICAL DO LABEL EM VB6
Alguem sabe como faço ara que um texto fique alinhado ao centro (na vertical) de um label ou de um textbox?
Só existe alinhamento horizontal no vb6. se é o que entendi.
é isso mesmo, além do alinhamento horizontal, eu estava querendo alinha o texto no meio do label e do textbox. ouvi falar que tem uns códigos que faz isso..
Olá,
Fiz aqui uma rotina mas só funciona para label[ô]s
Fale aà se serviu
Té+
Fiz aqui uma rotina mas só funciona para label[ô]s
Option Explicit
Private Sub Command1_Click()
Call AlinhamentoVertical(Label1)
End Sub
Private Sub AlinhamentoVertical(ByRef ctl As Control)
On Error GoTo ErrorCode
If Not ((TypeOf ctl Is Label)) Then Exit Sub
Dim lngHeight As Long
lngHeight = ctl.Height
ctl.AutoSize = True
ctl.Top = Int((lngHeight - ctl.Height) / 2) + ctl.Top
ErrorCode:
Exit Sub
End Sub
Fale aà se serviu
Té+
Amigo, esse código está ajustando o label ao tamanho do texto, ele diminui o tamanho do label tanto na vertical, como na horizontal, até que o label fique do tamanho do texto que estiver dentro dele.
Queria que o tamanho do label continuasse conforme estava e somente o texto fosse para o centro do label na vertical.
Valew
Queria que o tamanho do label continuasse conforme estava e somente o texto fosse para o centro do label na vertical.
Valew
coloca esse usercontrol, acabei se colocar a função de alinhamento vertical para você ...
WOLFFIRE, desculpe, mas nunca trabalhei com usercontrol, como faço ai, coloco o laber no próprio form que estou trabalhando e como chamo a função para alinhar?
colega cleber desculpe a pergunta mas porque deseja que seja desta forma, pois assim vc só conseguirá usando ou um user control ou colocar o label dentro de uma picturebox simulando o que vc deseja.
A questão é que não entendi direito como usar esse user control, não sei onde coloco o meu label.
A label que tenho no momento, ela está dentro de um form.
O que realmente quero saber, é como usar esse user control que Wolffire me enviou. Quero saber se coloco esse user control em meu projeto e chamo ele no form através de uma rotina ou tenho que colocar o label direto nesse user control.
Se puderem me ajudar, não estou sabendo usar o user control.
Abraços!
A label que tenho no momento, ela está dentro de um form.
O que realmente quero saber, é como usar esse user control que Wolffire me enviou. Quero saber se coloco esse user control em meu projeto e chamo ele no form através de uma rotina ou tenho que colocar o label direto nesse user control.
Se puderem me ajudar, não estou sabendo usar o user control.
Abraços!
copie o user control cole ele na pasta do seu programa
no lado direito em project, clique com o botão direito do mouse em cima de Project , depois clique em add--> User Control
vai abrir uma janela clique na existing, procure a pasta do seu projeto e selecione o user control e clique em abrir.
no lado esquerdo nas ferramentas repare que apareceu o user control, agora basta inclui-lo no form como vc faz com o label.
no lado direito em project, clique com o botão direito do mouse em cima de Project , depois clique em add--> User Control
vai abrir uma janela clique na existing, procure a pasta do seu projeto e selecione o user control e clique em abrir.
no lado esquerdo nas ferramentas repare que apareceu o user control, agora basta inclui-lo no form como vc faz com o label.
Ok, obrigado Marcelo, porém, quando vou inserir o controle user control que aparece na janela esquerda, em ferramentas, que coloco para inserir ele no form, a linha abaixo, que está em vermelho dá erro.
O erro é conforme a imagem e está na linha em destaque de vermelho, copiei abaixo todo o código do user control.
Option Explicit
Const DT_LEFT = &H0
Const DT_CENTER = &H1
Const DT_RIGHT = &H2
Public Enum VerticalAlignment
aTop = 0
aMiddle = 1
aBottom = 2
End Enum
Private m_HorizontalAlignment As AlignmentConstants
Private m_VerticalAlignment As AlignmentConstants
Private m_Caption As String
Private m_AutoSize As Boolean
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function DrawText Lib [Ô]user32.dll[Ô] Alias [Ô]DrawTextA[Ô] (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, ByRef lpRect As RECT, ByVal wFormat As Long) As Long
Private Declare Function SetRect Lib [Ô]user32.dll[Ô] (ByRef lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function CreateSolidBrush Lib [Ô]gdi32.dll[Ô] (ByVal crColor As Long) As Long
Private Declare Function CreateRectRgn Lib [Ô]gdi32.dll[Ô] (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function FrameRgn Lib [Ô]gdi32[Ô] (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function DeleteObject Lib [Ô]gdi32[Ô] (ByVal hObject As Long) As Long
Public Property Get VerticalAlignment() As VerticalAlignment
VerticalAlignment = m_VerticalAlignment
End Property
Public Property Let VerticalAlignment(NewValue As VerticalAlignment)
m_VerticalAlignment = NewValue
Call PropertyChanged([Ô]VerticalAlignment[Ô])
Call UserControl_Show
End Property
Public Property Get HorizontalAlignment() As AlignmentConstants
HorizontalAlignment = m_HorizontalAlignment
End Property
Public Property Let HorizontalAlignment(NewValue As AlignmentConstants)
m_HorizontalAlignment = NewValue
Call PropertyChanged([Ô]HorizontalAlignment[Ô])
Call UserControl_Show
End Property
Public Property Get Appearance() As AppearanceConstants
Appearance = UserControl.Appearance
End Property
Public Property Let Appearance(NewValue As AppearanceConstants)
UserControl.Appearance = NewValue
Call PropertyChanged([Ô]Appearance[Ô])
Call UserControl_Show
End Property
Public Property Get AutoSize() As Boolean
AutoSize = m_AutoSize
End Property
Public Property Let AutoSize(NewValue As Boolean)
m_AutoSize = NewValue
Call PropertyChanged([Ô]AutoSize[Ô])
Call UserControl_Show
End Property
Public Property Get BackColor() As OLE_COLOR
BackColor = UserControl.BackColor
End Property
Public Property Let BackColor(NewValue As OLE_COLOR)
UserControl.BackColor = NewValue
Call PropertyChanged([Ô]BackColor[Ô])
Call UserControl_Show
End Property
Public Property Get ForeColor() As OLE_COLOR
ForeColor = UserControl.ForeColor
End Property
Public Property Let ForeColor(NewValue As OLE_COLOR)
UserControl.ForeColor = NewValue
Call PropertyChanged([Ô]ForeColor[Ô])
Call UserControl_Show
End Property
Public Property Get BackStyle() As BackStyleCts
BackStyle = UserControl.BackStyle
End Property
Public Property Let BackStyle(NewValue As BackStyleCts)
UserControl.BackStyle = NewValue
Call PropertyChanged([Ô]BackStyle[Ô])
Call UserControl_Show
End Property
Public Property Get Caption() As String
Caption = m_Caption
End Property
Public Property Let Caption(ByVal Text As String)
m_Caption = Text
Call UserControl_Show
Call PropertyChanged([Ô]Caption[Ô])
End Property
Public Property Get hwnd() As Long
hwnd = UserControl.hwnd
End Property
Public Property Get Font() As Font
Set Font = UserControl.Font
End Property
Public Property Set Font(Value As Font)
Set UserControl.Font = Value
Call PropertyChanged([Ô]Font[Ô])
Call UserControl_Show
End Property
Public Property Get Enabled() As Boolean
Enabled = UserControl.Enabled
End Property
Public Property Let Enabled(NewValue As Boolean)
UserControl.Enabled = NewValue
Call PropertyChanged([Ô]Enabled[Ô])
End Property
Private Sub UserControl_InitProperties()
m_Caption = Ambient.DisplayName
Call UserControl_Show
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
VerticalAlignment = PropBag.ReadProperty([Ô]VerticalAlignment[Ô], m_VerticalAlignment)
HorizontalAlignment = PropBag.ReadProperty([Ô]HorizontalAlignment[Ô], m_HorizontalAlignment)
Appearance = PropBag.ReadProperty([Ô]Appearance[Ô], UserControl.Appearance)
AutoSize = PropBag.ReadProperty([Ô]AutoSize[Ô], m_AutoSize)
BackColor = PropBag.ReadProperty([Ô]BackColor[Ô], UserControl.BackColor)
ForeColor = PropBag.ReadProperty([Ô]ForeColor[Ô], UserControl.ForeColor)
BackStyle = PropBag.ReadProperty([Ô]BackStyle[Ô], UserControl.BackStyle)
Caption = PropBag.ReadProperty([Ô]Caption[Ô], m_Caption)
Enabled = PropBag.ReadProperty([Ô]Enabled[Ô], UserControl.Enabled)
Set Font = PropBag.ReadProperty([Ô]Font[Ô], UserControl.Font)
Call UserControl_Show
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty([Ô]VerticalAlignment[Ô], m_VerticalAlignment)
Call PropBag.WriteProperty([Ô]Appearance[Ô], UserControl.Appearance)
Call PropBag.WriteProperty([Ô]AutoSize[Ô], m_AutoSize)
Call PropBag.WriteProperty([Ô]BackStyle[Ô], UserControl.BackStyle)
Call PropBag.WriteProperty([Ô]BackColor[Ô], UserControl.BackColor)
Call PropBag.WriteProperty([Ô]ForeColor[Ô], UserControl.ForeColor)
Call PropBag.WriteProperty([Ô]Caption[Ô], m_Caption, Ambient.DisplayName)
Call PropBag.WriteProperty([Ô]Enabled[Ô], UserControl.Enabled)
Call PropBag.WriteProperty([Ô]Font[Ô], UserControl.Font, Ambient.Font)
End Sub
Private Sub UserControl_Resize()
Call UserControl_Show
End Sub
Private Sub UserControl_Show()
Dim R As RECT
Dim st As Long
Dim hBrush As Long
Dim hRgn As Long
With UserControl
.Cls
If m_AutoSize Then
.Width = TextWidth(m_Caption)
.Height = TextHeight(m_Caption)
End If
Select Case m_VerticalAlignment
Case 0
Call SetRect(R, 1, 1, ScaleWidth / Screen.TwipsPerPixelX - 1, ScaleHeight / Screen.TwipsPerPixelY + 1)
Case 1
Call SetRect(R, 1, (ScaleHeight / Screen.TwipsPerPixelY - (UserControl.TextHeight([Ô]A[Ô]) / Screen.TwipsPerPixelY)) / 2, ScaleWidth / Screen.TwipsPerPixelX - 1, ScaleHeight / Screen.TwipsPerPixelY + 1)
Case 2
Call SetRect(R, 1, ScaleHeight / Screen.TwipsPerPixelY - (UserControl.TextHeight([Ô]A[Ô]) / Screen.TwipsPerPixelY), ScaleWidth / Screen.TwipsPerPixelX - 1, ScaleHeight / Screen.TwipsPerPixelY + 1)
End Select
Select Case m_HorizontalAlignment
Case vbLeftJustify
st = DT_LEFT
Case vbCenter
st = DT_CENTER
Case vbRightJustify
st = DT_RIGHT
End Select
Call DrawText(.hdc, m_Caption, Len(m_Caption), R, st)
If .BackStyle = 0 Then
.BackStyle = 0
.MaskColor = .BackColor
Set .MaskPicture = .Image
End If
.Refresh
End With
End Sub
O erro é conforme a imagem e está na linha em destaque de vermelho, copiei abaixo todo o código do user control.
Option Explicit
Const DT_LEFT = &H0
Const DT_CENTER = &H1
Const DT_RIGHT = &H2
Public Enum VerticalAlignment
aTop = 0
aMiddle = 1
aBottom = 2
End Enum
Private m_HorizontalAlignment As AlignmentConstants
Private m_VerticalAlignment As AlignmentConstants
Private m_Caption As String
Private m_AutoSize As Boolean
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function DrawText Lib [Ô]user32.dll[Ô] Alias [Ô]DrawTextA[Ô] (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, ByRef lpRect As RECT, ByVal wFormat As Long) As Long
Private Declare Function SetRect Lib [Ô]user32.dll[Ô] (ByRef lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function CreateSolidBrush Lib [Ô]gdi32.dll[Ô] (ByVal crColor As Long) As Long
Private Declare Function CreateRectRgn Lib [Ô]gdi32.dll[Ô] (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function FrameRgn Lib [Ô]gdi32[Ô] (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function DeleteObject Lib [Ô]gdi32[Ô] (ByVal hObject As Long) As Long
Public Property Get VerticalAlignment() As VerticalAlignment
VerticalAlignment = m_VerticalAlignment
End Property
Public Property Let VerticalAlignment(NewValue As VerticalAlignment)
m_VerticalAlignment = NewValue
Call PropertyChanged([Ô]VerticalAlignment[Ô])
Call UserControl_Show
End Property
Public Property Get HorizontalAlignment() As AlignmentConstants
HorizontalAlignment = m_HorizontalAlignment
End Property
Public Property Let HorizontalAlignment(NewValue As AlignmentConstants)
m_HorizontalAlignment = NewValue
Call PropertyChanged([Ô]HorizontalAlignment[Ô])
Call UserControl_Show
End Property
Public Property Get Appearance() As AppearanceConstants
Appearance = UserControl.Appearance
End Property
Public Property Let Appearance(NewValue As AppearanceConstants)
UserControl.Appearance = NewValue
Call PropertyChanged([Ô]Appearance[Ô])
Call UserControl_Show
End Property
Public Property Get AutoSize() As Boolean
AutoSize = m_AutoSize
End Property
Public Property Let AutoSize(NewValue As Boolean)
m_AutoSize = NewValue
Call PropertyChanged([Ô]AutoSize[Ô])
Call UserControl_Show
End Property
Public Property Get BackColor() As OLE_COLOR
BackColor = UserControl.BackColor
End Property
Public Property Let BackColor(NewValue As OLE_COLOR)
UserControl.BackColor = NewValue
Call PropertyChanged([Ô]BackColor[Ô])
Call UserControl_Show
End Property
Public Property Get ForeColor() As OLE_COLOR
ForeColor = UserControl.ForeColor
End Property
Public Property Let ForeColor(NewValue As OLE_COLOR)
UserControl.ForeColor = NewValue
Call PropertyChanged([Ô]ForeColor[Ô])
Call UserControl_Show
End Property
Public Property Get BackStyle() As BackStyleCts
BackStyle = UserControl.BackStyle
End Property
Public Property Let BackStyle(NewValue As BackStyleCts)
UserControl.BackStyle = NewValue
Call PropertyChanged([Ô]BackStyle[Ô])
Call UserControl_Show
End Property
Public Property Get Caption() As String
Caption = m_Caption
End Property
Public Property Let Caption(ByVal Text As String)
m_Caption = Text
Call UserControl_Show
Call PropertyChanged([Ô]Caption[Ô])
End Property
Public Property Get hwnd() As Long
hwnd = UserControl.hwnd
End Property
Public Property Get Font() As Font
Set Font = UserControl.Font
End Property
Public Property Set Font(Value As Font)
Set UserControl.Font = Value
Call PropertyChanged([Ô]Font[Ô])
Call UserControl_Show
End Property
Public Property Get Enabled() As Boolean
Enabled = UserControl.Enabled
End Property
Public Property Let Enabled(NewValue As Boolean)
UserControl.Enabled = NewValue
Call PropertyChanged([Ô]Enabled[Ô])
End Property
Private Sub UserControl_InitProperties()
m_Caption = Ambient.DisplayName
Call UserControl_Show
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
VerticalAlignment = PropBag.ReadProperty([Ô]VerticalAlignment[Ô], m_VerticalAlignment)
HorizontalAlignment = PropBag.ReadProperty([Ô]HorizontalAlignment[Ô], m_HorizontalAlignment)
Appearance = PropBag.ReadProperty([Ô]Appearance[Ô], UserControl.Appearance)
AutoSize = PropBag.ReadProperty([Ô]AutoSize[Ô], m_AutoSize)
BackColor = PropBag.ReadProperty([Ô]BackColor[Ô], UserControl.BackColor)
ForeColor = PropBag.ReadProperty([Ô]ForeColor[Ô], UserControl.ForeColor)
BackStyle = PropBag.ReadProperty([Ô]BackStyle[Ô], UserControl.BackStyle)
Caption = PropBag.ReadProperty([Ô]Caption[Ô], m_Caption)
Enabled = PropBag.ReadProperty([Ô]Enabled[Ô], UserControl.Enabled)
Set Font = PropBag.ReadProperty([Ô]Font[Ô], UserControl.Font)
Call UserControl_Show
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty([Ô]VerticalAlignment[Ô], m_VerticalAlignment)
Call PropBag.WriteProperty([Ô]Appearance[Ô], UserControl.Appearance)
Call PropBag.WriteProperty([Ô]AutoSize[Ô], m_AutoSize)
Call PropBag.WriteProperty([Ô]BackStyle[Ô], UserControl.BackStyle)
Call PropBag.WriteProperty([Ô]BackColor[Ô], UserControl.BackColor)
Call PropBag.WriteProperty([Ô]ForeColor[Ô], UserControl.ForeColor)
Call PropBag.WriteProperty([Ô]Caption[Ô], m_Caption, Ambient.DisplayName)
Call PropBag.WriteProperty([Ô]Enabled[Ô], UserControl.Enabled)
Call PropBag.WriteProperty([Ô]Font[Ô], UserControl.Font, Ambient.Font)
End Sub
Private Sub UserControl_Resize()
Call UserControl_Show
End Sub
Private Sub UserControl_Show()
Dim R As RECT
Dim st As Long
Dim hBrush As Long
Dim hRgn As Long
With UserControl
.Cls
If m_AutoSize Then
.Width = TextWidth(m_Caption)
.Height = TextHeight(m_Caption)
End If
Select Case m_VerticalAlignment
Case 0
Call SetRect(R, 1, 1, ScaleWidth / Screen.TwipsPerPixelX - 1, ScaleHeight / Screen.TwipsPerPixelY + 1)
Case 1
Call SetRect(R, 1, (ScaleHeight / Screen.TwipsPerPixelY - (UserControl.TextHeight([Ô]A[Ô]) / Screen.TwipsPerPixelY)) / 2, ScaleWidth / Screen.TwipsPerPixelX - 1, ScaleHeight / Screen.TwipsPerPixelY + 1)
Case 2
Call SetRect(R, 1, ScaleHeight / Screen.TwipsPerPixelY - (UserControl.TextHeight([Ô]A[Ô]) / Screen.TwipsPerPixelY), ScaleWidth / Screen.TwipsPerPixelX - 1, ScaleHeight / Screen.TwipsPerPixelY + 1)
End Select
Select Case m_HorizontalAlignment
Case vbLeftJustify
st = DT_LEFT
Case vbCenter
st = DT_CENTER
Case vbRightJustify
st = DT_RIGHT
End Select
Call DrawText(.hdc, m_Caption, Len(m_Caption), R, st)
If .BackStyle = 0 Then
.BackStyle = 0
.MaskColor = .BackColor
Set .MaskPicture = .Image
End If
.Refresh
End With
End Sub
Tópico encerrado , respostas não são mais permitidas