gpt4 book ai didi

fonts - VB6 从哪里得到它的默认字体

转载 作者:行者123 更新时间:2023-12-05 01:27:04 28 4
gpt4 key购买 nike

VB6从哪里得到它的默认 字体来自?

是系统字体吗?

是由地域决定的吗?

无论实际字体如何,它总是相同的大小吗?

最佳答案

应用程序的字体在控件的 Font 属性中设置。 VB6 有默认的 MS Sans Serif(8 号),它是 Windows 95/98 中的默认系统字体,这个名称在 VB6 中是硬编码的。 Windows XP 使用 Tahoma 8、Windows Vista 和更高版本的 Segoe UI 9。因此,如果您需要所有表单和其他控件的现代外观,请根据 Windows 版本更改字体。很难检测到它,所以这个子从列表中获取第一个现有字体:

'fonts and sizes
Const MODERN_FONTS_CSV = "Segoe UI/9,Tahoma/8,MS Sans Serif/8"

Sub ChangeFont(oFrm As VB.Form)
Dim i As Long
Dim mf() As String
Dim fontSize As Long
Dim fontName As String
Dim oCtrl As VB.Control
Dim oFont As New stdole.StdFont

mf = Split(MODERN_FONTS_CSV, ",") 'list of fonts and sizes as CSV
'trying if the font exists
i = 0
Do
fontName = Split(mf(i), "/")(0)
fontSize = CLng(Split(mf(i), "/")(1))
oFont.Name = Trim(fontName) 'does the font exist?
i = i + 1
'font exists or end of the list (last name is the default whether exists or not)
Loop Until StrComp(fontName, oFont.Name, vbTextCompare) = 0 Or i > UBound(mf)

'at first change font in the form
With oFrm.Font
.Name = fontName 'name
.size = fontSize 'size
'.charset = 238 - you can set charset, in some cases it could be necessary
End With
'loop through all controls in the form
'some controls doesn't have font property (timer, toolbar) - ignore error
On Error Resume Next
For Each oCtrl In oFrm.Controls
With oCtrl.Font
.Name = fontName 'name
.size = fontSize 'size
'.charset = 238 - charset, if you want
Err.Clear
End With
Next
On Error GoTo 0

End Sub

解决方案2 - 获取系统字体名称

此代码类似,但通过 API 读取系统字体名称和大小(感谢 Bob77)。嗯 - 这是准确的,但有一些缺点:
  • 您无法测试疯狂用户的所有疯狂设置。对于某些字体大小,您的程序可能无法使用。
  • 它获取为消息设置的字体名称和大小(VB6 中的 MsgBox 窗口),但用户可能对其他文本(菜单、标题...)使用不同的字体,但默认大小是相同的。
  • 用户可能设置了不支持您的语言的系统字体。
  • 对于 72 DPI 设备以外的设备(请参阅 fontSize 变量),它可能会得到错误的字体大小 - 应该修复。

  • 代码:
    Option Explicit

    Declare Function SystemParametersInfo Lib "USER32.DLL" _
    Alias "SystemParametersInfoA" (ByVal uAction As Long, _
    ByVal uiParam As Long, pvParam As Any, _
    ByVal fWinIni As Long) As Long

    Private Const LOGPIXELSY = 90
    Private Const SPI_GETNONCLIENTMETRICS = 41

    Private Declare Function GetDeviceCaps Lib "gdi32.dll" (ByVal hDC As Long, ByVal nIndex As Long) As Long

    Private Type LOGFONT
    lfHeight As Long
    lfWidth As Long
    lfEscapement As Long
    lfOrientation As Long
    lfWeight As Long
    lfItalic As Byte
    lfUnderline As Byte
    lfStrikeOut As Byte
    lfCharSet As Byte
    lfOutPrecision As Byte
    lfClipPrecision As Byte
    lfQuality As Byte
    lfPitchAndFamily As Byte
    lfFaceName(1 To 32) As Byte
    End Type

    Private Type NONCLIENTMETRICS
    cbSize As Long
    iBorderWidth As Long
    iScrollWidth As Long
    iScrollHeight As Long
    iCaptionWidth As Long
    iCaptionHeight As Long
    lfCaptionFont As LOGFONT
    iSMCaptionWidth As Long
    iSMCaptionHeight As Long
    lfSMCaptionFont As LOGFONT
    iMenuWidth As Long
    iMenuHeight As Long
    lfMenuFont As LOGFONT
    lfStatusFont As LOGFONT
    lfMessageFont As LOGFONT
    End Type


    Public Sub ChangeFont(oFrm As VB.Form)
    Dim i As Long
    Dim ncm As NONCLIENTMETRICS
    Dim fontSize As Long
    Dim fontName As String
    Dim oCtrl As VB.Control
    Dim oFont As New stdole.StdFont

    'get font properties
    ncm.cbSize = Len(ncm)
    SystemParametersInfo SPI_GETNONCLIENTMETRICS, 0, ncm, 0
    For i = 1 To 32
    fontName = fontName & Chr(ncm.lfMessageFont.lfFaceName(i))
    Next i

    'name
    fontName = Replace(fontName, Chr(0), "") 'trim
    'size
    fontSize = -(ncm.lfMessageFont.lfHeight * (72 / GetDeviceCaps(oFrm.hDC, LOGPIXELSY)))

    'at first change font in the form
    With oFrm.Font
    .Name = fontName 'name
    .Size = fontSize 'size
    '.charset = 238 - you can set charset, in some cases it could be necessary
    End With
    'loop through all controls in the form
    'some controls doesn't have font property (timer, toolbar) - ignore error
    On Error Resume Next
    For Each oCtrl In oFrm.Controls
    With oCtrl.Font
    .Name = fontName 'name
    .Size = fontSize 'size
    '.charset = 238 - charset, if you want
    Err.Clear
    End With
    Next
    On Error GoTo 0
    End Sub

    有关其他字体操作,请参阅 this module .

    其他问题

    Is it determined by locale?



    不,但是当在 Windows 设置中使用不同的语言环境和环境语言(德语 Windows 环境和捷克语语言环境)时,我遇到了特定于国家/地区的字符的问题。我不得不强制所有控件的代码页(见上面的代码)。

    Is it always the same size irrespective of the actual font?



    如果您在 Windows 环境中更改字体大小,则文本大小会以适当的方式更改。我强烈建议:测试您的应用程序的所有组合 - 来自 MODERN_FONTS_CSV 常量和 Windows 文本大小 100-150% 的字体。

    关于fonts - VB6 从哪里得到它的默认字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25620114/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com