Visual Basic 6向状态栏面板添加背景色

我正在修复基于Visual Basic 6代码构建的旧应用程序。要求在表单底部添加状态栏。我的状态栏如下:

Visual Basic 6向状态栏面板添加背景色

我可以正确显示文本,但我也想添加红色背景色。我发现StatusBar面板没有这样的选项。当我打开StatusBar的属性时,它显示如下:

Visual Basic 6向状态栏面板添加背景色

我发现我可以添加图片。但是当我添加红色图片时,文字将被图片覆盖。我被困住了。任何建议都会有所帮助。谢谢!

更新

我只是使用了comment中提供的@ÉtienneLaneville链接中的代码。添加了背景色和文本。

这是我调用该函数的代码:

name: str_1
type: (Int8,Int8,Int8)
value: (72,101,108,111,32,119,114,100,33,0)
swift string: Optional("Hello world!\0")

-----------------
name: str_2
type: (Int8,0)
swift string: Optional("Hello world!\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")

-----------------
name: str_3
type: UnsafeMutablePointer<Int8>
value: 0x000000010e8c5d40
swift string: Optional("Hello world!")

-----------------
name: str_4
type: (UInt8,UInt8,UInt8)
value: (72,0)
swift string: Optional("Hello world \0")

-----------------
name: str_5
type: UnsafeMutablePointer<UInt8>
value: 0x000000010e8c5d80
swift string: Optional("Hello world ")

-----------------
name: str_6
type: (UInt8,0)
swift string: Optional("Hello world!\0")

-----------------
name: str_7
name: str_8
type: UnsafeMutablePointer<UInt8>
value: 0x000000010e8c0ae0
swift string: Optional("")

-----------------
Optional("Hello world!\0")
Optional("Hello world!\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")
Optional("Hello world!")
Optional("Hello world \0")
Optional("Hello world ")
Optional("Hello world!\0")
Optional("")
Optional("")

但是文本位置如下:

Visual Basic 6向状态栏面板添加背景色

我必须像下面这样放置文本才能定位它,因为该任务目前很紧急,我没有时间进行更多研究。

    PanelText StatusBar1,9,"ATM (" & cntATM & ")",QBColor(12),QBColor(0)

下面是我的输出:

Visual Basic 6向状态栏面板添加背景色

更新2

我尝试了Brian M Stafford提供的代码。但是我得到了相同的结果。文本仍不在中心(或向左)。下面是我的代码和状态栏的屏幕截图:

Visual Basic 6向状态栏面板添加背景色

功能:

    PanelText StatusBar1,"ATM (" & cntATM & ")                           ",QBColor(0)

API:

Private Sub PanelText(sb As StatusBar,pic As PictureBox,Index As Long,aText As String,bkColor As Long,_
    fgColor As Long,lAlign As Integer)

    Dim R As RECT

    SendMessage sb.hWnd,SB_GETRECT,Index - 1,R
    With pic
        Set .Font = sb.Font
        .Move 0,(R.Right - R.Left + 2) * Screen.TwipsPerPixelX,(R.Bottom - R.Top) * Screen.TwipsPerPixelY
        .BackColor = bkColor
        .Cls
        .ForeColor = fgColor
        .CurrentY = (.Height - .TextHeight(aText)) \ 2

        Select Case lAlign
            Case 0      ' Left Justified
                .CurrentX = 0
            Case 1      ' Right Justified
                .CurrentX = .Width - .TextWidth(aText) - Screen.TwipsPerPixelX * 2
            Case 2      ' Centered
                .CurrentX = (.Width - .TextWidth(aText)) \ 2
        End Select

        pic.Print aText
        sb.Panels(Index).Text = aText
        sb.Panels(Index).Picture = .Image
    End With
End Sub

调用函数:

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Const WM_USER = &H400
Private Const SB_GETRECT = (WM_USER + 10)

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

我不知道为什么。可能是我遗漏了一些东西,或者是我为StatusBar1或picPanel(PictureBox)设置了一些属性值。

解决方案

我设置pictureBox,属性AutoRedraw = True,并设置StatusBar,Panel,Alignment = sbrLeft。一切正常。

ZENGMINGMIN2009 回答:Visual Basic 6向状态栏面板添加背景色

这是代码referenced in a comment,具有一些增强功能。一种增强功能是用于指定文本对齐方式的参数:

Private Sub StatusBarPanelText(sb As StatusBar,pic As PictureBox,index As Long,aText As String,bkColor As Long,fgColor As Long,lAlign As Integer)
    Dim r As RECT

    SendMessage sb.hWnd,SB_GETRECT,index - 1,r

    With pic
        Set .Font = sb.Font
        .Move 0,(r.Right - r.Left + 2) * Screen.TwipsPerPixelX,(r.Bottom - r.Top) * Screen.TwipsPerPixelY
        .BackColor = bkColor
        .Cls
        .ForeColor = fgColor
        .CurrentY = (.Height - .TextHeight(aText)) \ 2

        Select Case lAlign
            Case 0      ' Left Justified
                .CurrentX = 0
            Case 1      ' Right Justified
                .CurrentX = .Width - .TextWidth(aText) - Screen.TwipsPerPixelX * 2
            Case 2      ' Centered
                .CurrentX = (.Width - .TextWidth(aText)) \ 2
        End Select

        pic.Print aText
        sb.Panels(index).Text = aText
        sb.Panels(index).Picture = .Image
    End With
End Sub

这是Windows API代码:

    Private Type RECT
       Left As Long
       Top As Long
       Right As Long
       Bottom 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 Const WM_USER = &H400
    Private Const SB_GETRECT = (WM_USER + 10)

然后使用如下代码:

    Picture2.AutoRedraw = True
    Picture2.Visible = False

    StatusBarPanelText sbConfig,Picture2,4,& _
       Format(Value / 1024,"#,###") & " KB",vbRed,vbWhite,0
,

我知道我迟到了,这个特定问题可能不再重要,但无论如何我都在这里。在尝试为 W-95 之后的所有 Windows 版本编写 VB6 程序时,我遇到了一些与 Microsoft 决定更改 Windows 后续版本的命名配色方案相关的颜色问题。我发现使用安装了 Windows-98 和 VB6 的虚拟机,我可以本机更改状态栏的背景颜色。当使用 Win-98 而不是 XP 及更高版本时,该属性位于“属性”框中。我猜雷德蒙德的男孩们想加强他们对我们工作的看法,所以取消了这个选择。我知道这不是一个完美的解决方案,但它可能是您可以接受的。

本文链接:https://www.f2er.com/3093420.html

大家都在问