如何实现 Masked Edit 控件的文字为“右端对齐”? |
如何实现 Masked Edit 控件的文字内容为“右端对齐”? 我估计要用到 API 函数,但不知道 Masked Edit 控件的文字内容为“右端对齐”的参数是什么,请大家帮忙解决,谢谢大家!
|
|
回复内容 |
【northwolves】: 不好办.
【northwolves】: 这是LISTBOX 右对齐的代码:
'To the form, add the following code:
Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ?1996-2006 VBnet, Randy Birch, All Rights Reserved. ' Some pages may also contain other copyrights by the author. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Distribution: You can freely use this code in your own ' applications, but you may not reproduce ' or publish this code on any web site, ' online service, or distribute as source ' on any media without express permission. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Const LB_SETTABSTOPS As Long = &H192 Private Const WM_GETFONT = &H31
Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type
Private Type SIZE cx As Long cy As Long End Type
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 Declare Sub CopyMemory Lib "kernel32" _ Alias "RtlMoveMemory" _ (Destination As Any, _ Source As Any, _ ByVal Length As Long) Private Declare Function GetDialogBaseUnits Lib "user32" () As Long
Private Declare Function GetTextExtentPoint32 Lib "gdi32" _ Alias "GetTextExtentPoint32A" _ (ByVal hDC As Long, _ ByVal lpString As String, _ ByVal cbString As Long, _ lpSize As SIZE) As Long
Private Declare Function GetDC Lib "user32" _ (ByVal hWnd As Long) As Long Private Declare Function ReleaseDC Lib "user32" _ (ByVal hWnd As Long, _ ByVal hDC As Long) As Long
Private Declare Function SelectObject Lib "gdi32" _ (ByVal hDC As Long, _ ByVal hObject As Long) As Long Private Declare Function GetClientRect Lib "user32" _ (ByVal hWnd As Long, _ lpRect As RECT) As Long
Private Sub Form_Load() List1.AddItem vbTab & "123" List1.AddItem vbTab & "123456" List1.AddItem vbTab & "123456789" List1.AddItem vbTab & "123456789012" List1.AddItem vbTab & "123456789012345" List1.AddItem vbTab & "123456789012345678" List1.AddItem vbTab & "123456789012345678901"
End Sub
Private Sub Command1_Click()
Dim hwndLB As Long Dim rc As RECT ReDim tabarray(0 To 0) As Long 'Assign list handle to a variable. 'A good rule of thumb is if you are 'using a property more than three 'times in a routine, it becomes more 'efficient to assign and use a variable 'rather than re-reference the property. hwndLB = List1.hWnd Call GetClientRect(hwndLB, rc) 'calculate the tab to align with 'the right-most edge. tabarray(0) = -((rc.Right - rc.Left) / CalcPixelsPerDlgUnit(hwndLB)) 'Clear any existing tabs and set the 'new tabstop Call SendMessage(List1.hWnd, LB_SETTABSTOPS, 0&, ByVal 0&) Call SendMessage(List1.hWnd, LB_SETTABSTOPS, 1&, tabarray(0)) List1.Refresh End Sub
Private Function CalcPixelsPerDlgUnit(hwndLB As Long) As Single
'Returns the number of pixels-per-dialog 'unit for the given font. ' 'Provided to VBnet by Brad Martinez
Dim hFont As Long Dim hFontOld As Long Dim hDC As Long Dim sz As SIZE Dim cxAvLBChar As Long 'average LB char width, in pixels Dim cxDlgBase As Long 'horizontal dialog box base units Const sChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
'get the device contect of the listbox hDC = GetDC(hwndLB) If hDC Then 'select hwndLB's HFONT into its DC (VB 'does not select a control's Font into its DC) hFont = SendMessage(hwndLB, WM_GETFONT, 0, ByVal 0&) hFontOld = SelectObject(hDC, hFont) If GetTextExtentPoint32(hDC, sChars, Len(sChars), sz) Then 'get the list box average char width 'and the system's horizontal dialog 'base units cxAvLBChar = sz.cx / Len(sChars) cxDlgBase = GetDialogBaseUnits And &HFFFF& 'calculate and return the number of 'pixels per dialog unit for the list CalcPixelsPerDlgUnit = (2 * cxAvLBChar) / cxDlgBase End If Call SelectObject(hDC, hFontOld) Call ReleaseDC(hwndLB, hDC) End If End Function
【foshan】: 谢谢大家!上楼的代码太复杂了。我想能否通过改变 Masked Edit 控件的样式来达到 Masked Edit 控件的文字内容为“右端对齐”。请大家继续帮忙,谢谢大家!
【lnhsgj】: northwolves(狼行天下) 的方法很好用,我试过了.
【foshan】: 将 northwolves(狼行天下) 提供的代码移植到 Masked Edit 控件,不能实现右对齐。 继续求助,谢谢大家!
|
|
|
|