Board logo

标题: [API] 为什么我实现不了?请帮忙看看 [打印本页]

作者: explor28    时间: 2008-3-13 15:30     标题: 为什么我实现不了?请帮忙看看

我用API函数,写了一个更改窗体图标的程序。网上搜索了一下,把人家的代码直接复制过来,然后更改成自己需要的,结果实现不了。请各位高手帮忙看看!
拜托各位高手了!

附件里是我的源代码

附件: 更改图标.rar (2008-3-13 15:30, 11.14 KB) / 该附件被下载次数 452
http://bbs.lihuasoft.net/attachment.php?aid=1379
作者: Nothing    时间: 2008-3-13 23:48

不用使用API,直接使用FROM的图标属性就可以了
作者: explor28    时间: 2008-3-14 09:25

这个是一个DEMO,我想在其他系统里边实现这个功能。
我在一个系统平台进行了二次开发,想在系统启动的时候用这种方式把平台的图标更换成我自己定义的,能获取到平台窗口句柄,所以考虑用API(估计也只有API可以实现)。昨天已经实现DEMO的更改图标,但是用到平台下就不行了,不知道为什么,郁闷!请高手指教
作者: Nothing    时间: 2008-3-14 13:38

试试下面的代码

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 Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Const GWL_HWNDPARENT = (-8)
   
Private Const SM_CXICON = 11
Private Const SM_CYICON = 12
Private Const SM_CXSMICON = 49
Private Const SM_CYSMICON = 50
Private Const LR_DEFAULTCOLOR = &H0
Private Const LR_MONOCHROME = &H1
Private Const LR_COLOR = &H2
Private Const LR_COPYRETURNORG = &H4
Private Const LR_COPYDELETEORG = &H8
Private Const LR_LOADFROMFILE = &H10
Private Const LR_LOADTRANSPARENT = &H20
Private Const LR_DEFAULTSIZE = &H40
Private Const LR_VGACOLOR = &H80
Private Const LR_LOADMAP3DCOLORS = &H1000
Private Const LR_CREATEDIBSECTION = &H2000
Private Const LR_COPYFROMRESOURCE = &H4000
Private Const LR_SHARED = &H8000&
Private Const IMAGE_ICON = 1
Private Const WM_SETICON = &H80
Private Const ICON_SMALL = 0
Private Const ICON_BIG = 1
Private Const GW_OWNER = 4
Private Declare Function LoadImageAsString Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal uType As Long, ByVal cxDesired As Long, ByVal cyDesired As Long, ByVal fuLoad As Long) As Long
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
   
  Private Sub SetIcon(ByVal hwnd As Long, ByVal sIconFileName As String, Optional ByVal bSetAsAppIcon As Boolean = True)
          Dim lhWndTop As Long
          Dim lhWnd As Long
          Dim cx As Long
          Dim cy As Long
          Dim hIconLarge As Long
          Dim hIconSmall As Long
            
          If (bSetAsAppIcon) Then
          ' Find VB's hidden parent window:
                  lhWnd = hwnd
                  lhWndTop = lhWnd
                          Do While Not (lhWnd = 0)
                          lhWnd = GetWindow(lhWnd, GW_OWNER)
                          If Not (lhWnd = 0) Then
                                  lhWndTop = lhWnd
                          End If
                  Loop
          End If
            
          cx = GetSystemMetrics(SM_CXICON)
          cy = GetSystemMetrics(SM_CYICON)
          hIconLarge = LoadImageAsString(App.hInstance, sIconFileName, IMAGE_ICON, cx, cy, LR_LOADFROMFILE)
          If (bSetAsAppIcon) Then
          SendMessageLong lhWndTop, WM_SETICON, ICON_BIG, hIconLarge
          End If
          SendMessageLong hwnd, WM_SETICON, ICON_BIG, hIconLarge
            
          cx = GetSystemMetrics(SM_CXSMICON)
          cy = GetSystemMetrics(SM_CYSMICON)
          hIconSmall = LoadImageAsString(App.hInstance, sIconFileName, IMAGE_ICON, cx, cy, LR_LOADFROMFILE)
          If (bSetAsAppIcon) Then
          SendMessageLong lhWndTop, WM_SETICON, ICON_SMALL, hIconSmall
          End If
          SendMessageLong hwnd, WM_SETICON, ICON_SMALL, hIconSmall
   
  End Sub
   
   



Private Sub Command1_Click()

Dim IconPath As Variant
Dim hIcon As Long
IconPath = App.Path + "\Vector.ico"
If IconPath = False Then Exit Sub
If IconPath = "" Then
    hIcon = 0
ElseIf Dir(IconPath) = "" Then
    hIcon = 0
ElseIf Err.Number <> 0 Then
    hIcon = 0
Else
    hIcon = ExtractIcon(0, IconPath, 0)
End If



Form2.Show
SetIcon Form2.hwnd, IconPath
End Sub

作者: explor28    时间: 2008-3-15 21:34

感谢感谢
这正是我需要的
我按
作者: explor28    时间: 2008-3-15 21:46

感谢感谢

非常感谢你的帮助

参照你的代码,我已经实现了




欢迎光临 编程开发论坛 (http://bbs.lihuasoft.net/) Powered by Discuz! 6.0.0