查看完整版本: 为什么我实现不了?请帮忙看看

explor28 2008-3-13 15:30

为什么我实现不了?请帮忙看看

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

附件里是我的源代码

Nothing 2008-3-13 23:48

不用使用API,直接使用FROM的图标属性就可以了

explor28 2008-3-14 09:25

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

Nothing 2008-3-14 13:38

试试下面的代码[color=#000000]

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



[color=#0000ff]Private[/color] [color=#0000ff]Sub[/color] Command1_Click[color=#0000cc]([/color][color=#0000cc])[/color]

[color=#0000ff]Dim[/color] IconPath As Variant
[color=#0000ff]Dim[/color] hIcon As Long
IconPath [color=#0000cc]=[/color] App[color=#0000cc].[/color]Path [color=#0000cc]+[/color] [color=#ff00ff]"\Vector.ico"[/color]
[color=#0000ff]If[/color] IconPath [color=#0000cc]=[/color] [color=#0000ff]False[/color] [color=#0000ff]Then[/color] [color=#0000ff]Exit[/color] [color=#0000ff]Sub[/color]
[color=#0000ff]If[/color] IconPath [color=#0000cc]=[/color] [color=#ff00ff]""[/color] [color=#0000ff]Then[/color]
    hIcon [color=#0000cc]=[/color] 0
[color=#0000ff]ElseIf[/color] Dir[color=#0000cc]([/color]IconPath[color=#0000cc])[/color] [color=#0000cc]=[/color] [color=#ff00ff]""[/color] [color=#0000ff]Then[/color]
    hIcon [color=#0000cc]=[/color] 0
[color=#0000ff]ElseIf[/color] [color=#ff0000]Err[/color][color=#0000cc].[/color]Number [color=#0000cc]<[/color][color=#0000cc]>[/color] 0 [color=#0000ff]Then[/color]
    hIcon [color=#0000cc]=[/color] 0
[color=#0000ff]Else[/color]
    hIcon [color=#0000cc]=[/color] ExtractIcon[color=#0000cc]([/color]0[color=#0000cc],[/color] IconPath[color=#0000cc],[/color] 0[color=#0000cc])[/color]
[color=#0000ff]End[/color] [color=#0000ff]If[/color]



Form2[color=#0000cc].[/color]Show
SetIcon Form2[color=#0000cc].[/color]hwnd[color=#0000cc],[/color] IconPath
[color=#0000ff]End[/color] [color=#0000ff]Sub[/color]
[/color]

explor28 2008-3-15 21:34

感谢感谢
这正是我需要的
我按

explor28 2008-3-15 21:46

感谢感谢

非常感谢你的帮助

参照你的代码,我已经实现了
页: [1]
查看完整版本: 为什么我实现不了?请帮忙看看