设置文本框的九种对齐方式(左上,中上,右上,左中,中中,右中,左下,中下,右下)

2124人阅读 评论(0) 收藏 举报

'Add a commandbutton and a textbox( multiline=true) and copy the following codes to form1:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
Private Const EM_SETRECT As Long = &HB3
Private Const EM_SETRECTNP As Long = &HB4
Dim i As Integer

Sub alignment(ByVal txtbox As TextBox, ByVal index As Integer, ByVal txt As String)
Dim r As RECT, atop As Long, h As Long
SendMessage txtbox.hwnd, EM_GETRECT, 0, r
Me.FontName = txtbox.FontName
Me.FontSize = txtbox.FontSize
Me.FontBold = txtbox.FontBold
txtbox.Text = txt
h = Me.TextHeight("H") / Screen.TwipsPerPixelY
If index / 3 = 0 Then atop = 0
If index / 3 = 1 Then atop = r.Bottom / 2 - h
If index / 3 = 2 Then atop = r.Bottom - h
r.Top = atop
Debug.Print r.Top; r.Bottom
txtbox.alignment = Array(vbLeftJustify, vbCenter, vbRightJustify)(index Mod 3)
SendMessage txtbox.hwnd, EM_SETRECTNP, 0, r
txtbox.Refresh
End Sub

Private Sub Command1_Click()
Dim x() As String
x = Split("左上,中上,右上,左中,中中,右中,左下,中下,右下", ",")
alignment Text1, i, x(i)
i = (i + 1) Mod 9
End Sub

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:630775次
    • 积分:8211
    • 等级:
    • 排名:第2405名
    • 原创:205篇
    • 转载:26篇
    • 译文:1篇
    • 评论:213条
    最新评论