文字の幅を論理単位で取得 <TOP>
文字列を論理単位で取得します。
GetCharWidth32 文字の幅を論理単位で取得
GetDC デバイスコンテキストのハンドルを取得
ReleaseDC デバイスコンテキストを解放
'================================================================ '= 文字の幅を論理単位で取得
'= (GetCharWidth32.bas) '================================================================ #include "Windows.bi" ' 文字の幅を論理単位で取得(昇順に連続した文字[AからEまで、SからTまで、など]の幅を一括して取得) Declare Function Api_GetCharWidth32& Lib "gdi32" Alias "GetCharWidthA" (ByVal hDC&, ByVal iFirstChar&, ByVal iLastChar&, lpBuffer As Any) ' 指定されたウィンドウのクライアント領域または画面全体を表すディスプレイデバイスコンテキストのハンドルを取得 Declare Function Api_GetDC& Lib "user32" Alias "GetDC" (ByVal hWnd&) ' デバイスコンテキストを解放 Declare Function Api_ReleaseDC& Lib "user32" Alias "ReleaseDC" (ByVal hWnd&, ByVal hDC&) Var Shared Edit1 As Object Var Shared Text1 As Object Var Shared Button1 As Object Edit1.Attach GetDlgItem("Edit1") : Edit1.SetFontSize 14 Text1.Attach GetDlgItem("Text1") : Text1.SetFontSize 14 Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14 '================================================================ '= '================================================================ Declare Function sGetTextWidth(strText As String) As Long Function sGetTextWidth(strText As String) As Long Var tWidth As Long Var TotalWidth As Long Var i As Integer Var hDC As Long Var Ret As Long hDC = Api_GetDC(Edit1.GethWnd) For i = 1 To Len(strText) Ret = Api_GetCharWidth32(hDC, Asc(Mid$(strText, i, 1)), Asc(Mid$(strText, i, 1)), tWidth) TotalWidth = TotalWidth + tWidth Next i
sGetTextWidth = TotalWidth
Ret = Api_ReleaseDC(Edit1.GethWnd, hDC)
End Function
'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
Text1.SetWindowText Str$(sGetTextWidth(Edit1.GetWindowText))
End Sub
'================================================================
'=
'================================================================
While 1
WaitEvent
Wend
Stop
End