VB最小化到通知欄,托盤 的方法 WinAPI實現

2021-07-25 05:35:48 字數 3580 閱讀 6030

直接複製**稍微修改一下就可以用了

option explicit

public const defaulticonindex = 1 '圖示預設索引

public const wm_lbuttondown = &h201 '按滑鼠左鍵

public const wm_rbuttondown = &h204 '按滑鼠右鍵

public const nim_add = 0 '新增圖示

public const nim_modify = 1 '修改圖示

public const nim_delete = 2 '刪除圖示

public const nif_message = 1 'message 有效

public const nif_icon = 2 '圖示操作(新增,修改,刪除)有效

public const nif_tip = 4 ' tooltip(提示)有效

'api declare

'圖示操作

public declare function shell_notifyicon lib "shell32.dll" alias "shell_notifyicona" (byval dwmessage as long, lpdata as notifyicondata) as long

'判斷視窗是否最小化

public declare function isiconic lib "user32" (byval hwnd as long) as long '如果是最小化,返回值》0

'設定視窗位置和狀態的功能

public declare function setwindowpos lib "user32" (byval hwnd as long, byval hwndinsertafter as long, byval x as long, byval y as long, byval cx as long, byval cy as long, byval wflags as long) as long

'定義型別

'通知欄圖示狀態

public type notifyicondata

cbsize as long

hwnd as long

uid as long

uflags as long

ucallbackmessage as long

hicon as long

sztip as string * 64

end type

'函式定義

'新增圖示到通知欄

public function icon_add(ihwnd as long, stips as string, hicon as long, iconid as long) as long

dim iconva as notifyicondata

with iconva

.hwnd = ihwnd

.sztip = stips + chr$(0)

.hicon = hicon

.uid = iconid

.ucallbackmessage = wm_lbuttondown

.cbsize = len(iconva)

.uflags = nif_message or nif_icon or nif_tip

icon_add = shell_notifyicon(nim_add, iconva)

end with

end function

'刪除通知欄圖示

public function icon_del(ihwnd as long, lindex as long) as long

dim iconva as notifyicondata

dim l as long

with iconva

.hwnd = ihwnd

.uid = lindex

.cbsize = len(iconva)

end with

icon_del = shell_notifyicon(nim_delete, iconva)

end function

'修改通知欄圖示

public function icon_modify(ihwnd as long, stips as string, hicon as long, iconid as long) as long

dim iconva as notifyicondata

with iconva

.hwnd = ihwnd

.sztip = stips + chr$(0)

.hicon = hicon

.uid = iconid

.ucallbackmessage = wm_lbuttondown

.cbsize = len(iconva)

.uflags = nif_message or nif_icon or nif_tip

icon_modify = shell_notifyicon(nim_modify, iconva)

end with

end function

option explicit

dim minflag as boolean

private sub form_load()

minflag = false

end sub

private sub form_mousedown(button as integer, shift as integer, x as single, y as single)

dim l

l = x \ 15

if l = wm_rbuttondown then

me.popupmenu m_main

elseif l = wm_lbuttondown then

call icon_modify(form1.hwnd, form1.caption, loadpicture("f:\\ico\1.ico"), 0)

end if

end sub

private sub form_resize()

if isiconic(me.hwnd) > 0 and minflag = false then '如果最小化

minflag = true

me.visible = false

call icon_add(me.hwnd, me.caption, me.icon, 0)

me.windowstate = vbnormal

end if

end sub

private sub m_menu_click(index as integer)

select case index

case 0:

call icon_del(form1.hwnd, 0)

form1.show

minflag = false

exit sub

case 1:

call icon_del(form1.hwnd, 0)

endend select

end sub

最小化到托盤

notifyicon notifyicon1 void inittraymenu 在建構函式時呼叫此函式 void notifyicon1 click object sender,eventargs e private void menuitem1 click object sender,syste...

C 最小化到托盤的方法

1.在窗體設計的工具箱中拖出乙個notifyicon 自動命名為notifyicon1,如有需要可以更改名字,這個控制項是最小化到托盤的圖示,可以先將它的icon屬性設定為你自己的圖示。2.建立窗體resize方法。private void mainform resize object sender...

C 最小化到托盤

define wm showtask wm user 1 void ctestdlg onsyscommand uint nid,lparam lparam else cdialog onsyscommand nid,lparam begin message map ctestdlg,cdialog...