gzy 发表于 2003-10-22 22:07:00

[VBA]VBA窗体上图标

很想在VB窗体上搞个图标,但一直找不到方法
到底有没有方法能够实现在VBA/VB窗体左上角文字之前显示一个小图标呢?

efan2000 发表于 2003-10-23 11:42:00

用下面的代码可以实现,在VB中直接使用窗体的Hwnd,而不需要GetActiveWindow获取当前活动窗口的句柄。
不过有个问题,在VBA中的标题前面部分被遮住了。


Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
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_SETICON = &H80
Private Const IMAGE_ICON = 1
Private Const LR_LOADFROMFILE = &H10

Private Sub UserForm_Activate()
    Dim Hwnd As Long
    Hwnd = GetActiveWindow
    Dim hIcon As Long
    hIcon = LoadImage(0&, "c:\光缆.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE)
    If hIcon <> 0 Then
      Call SendMessage(Hwnd, WM_SETICON, 0, ByVal hIcon)
    End If
End Sub

gzy 发表于 2003-10-23 13:15:00

多谢!

cag 发表于 2003-11-13 18:42:00

是不是一定要是ico的,而且是16X16的?

efan2000 发表于 2003-11-15 19:27:00

图标肯定要使用ICO类型的,但大小一般情况下是16*16,如果一定要与标准的风格相异时,也可以自已设定大小。

wyj7485 发表于 2005-6-30 17:43:00

先在窗体的标题前空6空格,再重新赋予无空格的标题就可以消除被遮现象.

王咣生 发表于 2005-7-1 01:14:00

reply

窗体标题被遮住一部分,只要刷新一下窗体就可以了,可以这样


Me.Caption = Me.Caption加到efan2000的代码中

winds357 发表于 2013-8-24 11:32:57

学习一下!
页: [1]
查看完整版本: [VBA]VBA窗体上图标