`
izuoyan
  • 浏览: 9272328 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

用vc实现隐藏系统托盘图标

阅读更多

这几天在做一个msn隐藏的工具时,想实现隐藏msn系统托盘的图标,在网上搜索了一下,发现有人用vb实现的代码,依样画葫芦弄到了vc里面,但是发现我的到的托盘图标的描述基本上是乱码。今天在看ATL的东西时突然想起会不会是字串格式的问题呢?查msdn后发现有一个String Conversion Macro的主题,在使用了OLE2T后(还需要一个USES_CONVERSION;不然会有编译错误),终于得到了在vb中一样的字符串了。^O^ 

islq 2006-3-19

完整代码如下:

void CMyDlg::MoveTray(BOOL flag)
{
// TODO: Add your control notification handler code here
HWND hWnd,hWndTmp;
unsigned long lngPID;
long ret,lngButtons;
HANDLE hProcess;
LPVOID lngAddress;
long lngTextAdr,lngHwndAdr,lngHwnd,lngButtonID;
char strBuff[1024]={0};
char* str = NULL;

hWnd = ::FindWindow("Shell_TrayWnd", NULL);
hWnd = ::FindWindowEx(hWnd, 0, "TrayNotifyWnd", NULL);
hWndTmp = ::FindWindowEx(hWnd, 0, "SysPager", NULL);
if(!hWndTmp)
hWnd = ::FindWindowEx(hWnd, 0, "ToolbarWindow32", NULL);
else
hWnd = ::FindWindowEx(hWndTmp, 0, "ToolbarWindow32", NULL);
TRACE("\nhWnd=%x\n",hWnd);
ret = ::GetWindowThreadProcessId(hWnd, &lngPID);
hProcess = ::OpenProcess(PROCESS_ALL_ACCESS
|PROCESS_VM_OPERATION
|PROCESS_VM_READ
|PROCESS_VM_WRITE,
0,
lngPID);
lngAddress = VirtualAllocEx(hProcess, 0, 0x4096, MEM_COMMIT, PAGE_READWRITE);//0x02e60000
lngButtons = ::SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);

for(int i = 0 ;i< lngButtons - 1;i++){
ret = ::SendMessage(hWnd,TB_GETBUTTON,i,long(lngAddress));
ret = ::ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 16), &lngTextAdr, 4, 0);
if(lngTextAdr != -1){
ret = ::ReadProcessMemory(hProcess, LPVOID(lngTextAdr), strBuff, 1024, 0);
ret = ::ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 12), &lngHwndAdr, 4, 0);
ret = ::ReadProcessMemory(hProcess, LPVOID(lngHwndAdr), &lngHwnd, 4, 0);
ret = ::ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 4), &lngButtonID, 4, 0);

USES_CONVERSION;
str = OLE2T((LPOLESTR)(strBuff));
//MessageBox((const char*)(strTmp),NULL,MB_OK);//Just for test
if(strstr(str,"Messenger")>0){
if(flag){
::SendMessage(hWnd, TB_HIDEBUTTON , lngButtonID, 1);
}
else{
::SendMessage(hWnd, TB_HIDEBUTTON , lngButtonID, 0);
}
}
}
}
::VirtualFreeEx( hProcess, lngAddress, 0X4096, MEM_RELEASE);
::CloseHandle(hProcess);
}

一下是从网上找到的参考的vb代码

VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Left = 2760
TabIndex = 1
Top = 2280
Width = 1455
End
Begin VB.ListBox List1
Height = 1860
Left = 360
TabIndex = 0
Top = 240
Width = 3855
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Declare Function OpenProcess Lib "KERNEL32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "KERNEL32" (ByVal hObject As Long) As Long
Private Const READ_CONTROL As Long = &H20000
Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
Private Const STANDARD_RIGHTS_READ As Long = (READ_CONTROL)
Private Const STANDARD_RIGHTS_EXECUTE As Long = (READ_CONTROL)
Private Const STANDARD_RIGHTS_ALL As Long = &H1F0000
Private Const STANDARD_RIGHTS_WRITE As Long = (READ_CONTROL)
Private Const SYNCHRONIZE As Long = &H100000
Private Const PROCESS_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF)
Private Const PROCESS_TERMINATE As Long = (&H1)
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Const WM_USER As Long = &H400
Private Declare Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "USER32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Const TB_BUTTONCOUNT As Long = (WM_USER + 24)
Private Const TB_HIDEBUTTON As Long = (WM_USER + 4)
Private Const TB_GETBUTTON As Long = (WM_USER + 23)
Private Const TB_GETBITMAP As Long = (WM_USER + 44)
Private Const TB_DELETEBUTTON As Long = (WM_USER + 22)
Private Const TB_ADDBUTTONS As Long = (WM_USER + 20)
Private Const TB_INSERTBUTTON As Long = (WM_USER + 21)
Private Const TB_ISBUTTONHIDDEN As Long = (WM_USER + 12)
Private Const ILD_NORMAL As Long = &H0
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function WriteProcessMemory Lib "kernel32.dll" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32.dll" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function VirtualAllocEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function VirtualFreeEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal dwFreeType As Long) As Long
Private Const PROCESS_VM_OPERATION As Long = (&H8)
Private Const PROCESS_VM_READ As Long = (&H10)
Private Const PROCESS_VM_WRITE As Long = (&H20)
Private Const MEM_RESERVE As Long = &H2000
Private Const MEM_COMMIT As Long = &H1000
Private Const MEM_RELEASE As Long = &H8000
Private Const PAGE_READWRITE As Long = &H4

Private Sub Command1_Click()
Dim lngTemp As Long
Dim lngTray
Dim lngPID As Long
Dim lngPID2 As Long
Dim hProcess As Long
Dim lngProcess As Long
Dim lngAddress As Long
Dim lngCount As Long
Dim lngButtons As Long
Dim ret As Long
Dim lngTextAdr As Long
Dim lngHwndAdr As Long
Dim lngButtonID As Long
Dim hIcon As Long
Dim lngHwnd As Long
Dim lpFileName As String * 1024
Dim i As Long
Dim strBuff(1024) As Byte
Dim strText As String
Dim lngTrayDC As Long
lngTemp = FindWindow("Shell_TrayWnd", vbNullString)
lngTemp = FindWindowEx(lngTemp, 0, "TrayNotifyWnd", vbNullString)
lngTemp = FindWindowEx(lngTemp, 0, "SysPager", vbNullString)
lngTray = FindWindowEx(lngTemp, 0, "ToolbarWindow32", vbNullString)
Debug.Print "lngTray="; lngTray
ret = GetWindowThreadProcessId(lngTray, lngPID)
hProcess = OpenProcess(PROCESS_ALL_ACCESS Or PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, 0, lngPID)
lngAddress = VirtualAllocEx(hProcess, ByVal 0&, ByVal 4096&, MEM_COMMIT, PAGE_READWRITE)
lngButtons = SendMessage(lngTray, TB_BUTTONCOUNT, 0, 0)

For i = 0 To lngButtons - 1
ret = SendMessage(lngTray, TB_GETBUTTON, ByVal i, ByVal lngAddress)
ret = ReadProcessMemory(hProcess, ByVal lngAddress + 16, ByVal VarPtr(lngTextAdr), ByVal 4, ByVal 0&)
If lngTextAdr <> -1 Then
ret = ReadProcessMemory(hProcess, ByVal lngTextAdr, ByVal VarPtr(strBuff(0)), ByVal 1024, ByVal 0&)
ret = ReadProcessMemory(hProcess, ByVal lngAddress + 12, ByVal VarPtr(lngHwndAdr), ByVal 4, ByVal 0&)
ret = ReadProcessMemory(hProcess, ByVal lngHwndAdr, ByVal VarPtr(lngHwnd), ByVal 4, ByVal 0&)
ret = ReadProcessMemory(hProcess, ByVal lngAddress + 4, ByVal VarPtr(lngButtonID), ByVal 4, ByVal 0&)

strText = ConverNull(strBuff)
'lngHwnd 所属窗口handle
'strText 托盘图标文字
List1.AddItem lngHwnd & " - " & strText
End If
Next i
VirtualFreeEx hProcess, ByVal lngAddress, ByVal 4096&, MEM_RELEASE
CloseHandle hProcess
End Sub

Private Sub Form_Load()
Command1.Caption = "&Enumerate"
End Sub

Function ConverNull(ByVal s As String) As String
Dim nullpos As Long
nullpos = InStr(s, Chr$(0))
If nullpos > 0 Then
ConverNull = Left(s, nullpos - 1)
Else
ConverNull = s
End If
End Function

分享到:
评论

相关推荐

    VC程序代码之隐藏系统托盘

    `Shell_NotifyIcon`用于处理系统托盘图标,而`FindWindow`则用于查找指定类名或窗口标题的窗口句柄。 首先,要隐藏系统托盘,我们需要找到系统托盘的窗口句柄。这可以通过`FindWindow`函数实现,传入"Shell_TrayWnd...

    vc 实现系统托盘 Windows

    - 在VC++项目中,你需要添加一个图标资源(.ico文件),这将成为你的系统托盘图标。 - 使用Resource Editor或者第三方工具如Paint.NET或IcoFX创建并导入图标资源。 2. **定义消息处理函数**: - 创建一个窗口类...

    管理系统托盘中的所有图标的VC源码

    #### 一、系统托盘图标管理 **系统托盘**(也称为任务栏通知区域)是Windows操作系统中的一个重要组件,它允许应用程序放置图标以供用户快速访问或监视程序状态。本段代码展示了如何通过C++(使用Visual C++编译器)...

    visual c++源码隐藏窗体,在托盘区显示图标,右击弹出菜单

    最后,为了实现隐藏窗体并在托盘区显示图标,我们需要在窗口类的初始化函数中,调用`ShowWindow(SW_HIDE)`隐藏窗体,同时在窗口创建后立即设置系统托盘图标。 总的来说,实现Visual C++源码隐藏窗体并在系统托盘...

    增加系统托盘图标

    3. **创建隐藏窗口**:使用`CreateWindowEx()`创建一个隐藏的窗口实例,该窗口作为系统托盘图标的父窗口。 4. **初始化`NOTIFYICONDATA`结构体**:设置图标、提示文本、回调消息等信息,并指定隐藏窗口为父窗口。 ...

    vc程序最小化托盘实现

    其次,创建并设置系统托盘图标(Tray Icon)是关键步骤。这包括创建一个`HICON`图标资源,然后使用`Shell_NotifyIcon`函数添加到通知区域。`Shell_NotifyIcon`函数需要传递一个`NOTIFYICONDATA`结构体,其中包含了...

    系统托盘 右键菜单 vc6.0

    6. **删除图标**: 当程序退出时,别忘了使用`Shell_NotifyIcon`的`NIM_DELETE`标志移除系统托盘图标。 在VC6.0环境中,创建项目并选择MFC AppWizard,选择基于对话框的应用程序模板。在生成的代码基础上,我们可以...

    为程序创建系统托盘图标

    系统托盘图标需要关联一个隐藏的窗口,以便接收和处理消息。 3. **WM_DESTROY** 和 **WM_NCDESTROY**: 这两个消息表示窗口被销毁,我们需要在收到这些消息时删除系统托盘图标。 4. **WM_USER** 和 **WM_COMMAND**:...

    VC实现将对话框最小化到系统托盘

    本篇文章将详细介绍如何使用Visual C++(VC)6.0中的MFC框架来实现这一功能,并添加了右键点击系统托盘图标弹出菜单以及左键双击托盘图标弹出对话框的功能。 #### 准备工作 首先,确保已经安装了Visual C++ 6.0...

    vc 实现托盘小功能

    在VC++编程环境中,实现托盘小功能主要是为了在用户最小化窗口时,将应用程序隐藏到系统托盘区域,而不是完全关闭或隐藏在任务栏后面。这样,用户可以通过托盘图标快速访问应用,提高用户体验。以下是对实现这一功能...

    Vc托盘编程+源码

    **VC托盘编程**是Windows应用开发中的一个重要概念,它涉及到使用Microsoft Visual C++(简称VC)进行系统托盘图标程序的设计。系统托盘,也称为通知区域,位于任务栏右下角,通常用于显示各种应用程序的状态和提供...

    vc实现系统托盘.doc

    在VC++环境中,使用MFC库可以方便地实现系统托盘功能,这使得应用程序在用户关闭主窗口后,仍然可以在任务栏的右下角显示一个小图标,以便进行其他操作,如QQ的右下角小企鹅图标。以下是实现这一功能的具体步骤: 1...

    VC实现最小化按钮后以图标放到右下方托盘中

    在VC++编程环境中,开发一个应用使得当用户点击最小化按钮时,程序窗口不消失而是转化为系统托盘图标,这通常涉及到Windows API的使用。这个功能的实现主要分为以下几个步骤: 1. **最小化的原理**: - **窗口隐藏...

    系统托盘程序范例VC

    7. **图标行为管理**:系统托盘图标可以有隐藏、显示、闪烁等多种动态行为。通过调用`Shell_NotifyIcon`的适当标志,可以控制这些行为。 8. **图标通知标志**:例如,`NIF_MESSAGE`用于设置接收消息的窗口句柄,`...

    VC程序托盘实现简单的例子

    在VC++编程环境中,"VC程序托盘实现简单的例子"是一个常见的学习课题,它涉及到Windows应用程序设计中的一个重要组件——系统托盘图标。系统托盘,也称为通知区域,位于任务栏右下角,通常用于显示应用程序的状态并...

    VC+MFC 托盘图标闪烁与消息提示.rar.rar

    在这个"VC+MFC 托盘图标闪烁与消息提示.rar"的压缩包中,我们主要探讨的是如何在MFC应用中实现系统托盘图标的功能,包括图标闪烁和消息提示。 系统托盘图标通常位于任务栏右下角,可以为用户提供一种隐藏但又易于...

    vc实现体系托盘 像qq一样托盘

    在本文中,我们将深入探讨如何使用Microsoft Visual C++(VC++)来实现一个类似于QQ的系统托盘功能,即让程序最小化后隐藏到桌面右下角的托盘区。 首先,我们需要了解系统托盘的基本原理。系统托盘是通过Windows ...

    VC 托盘气泡图标

    在Windows编程中,"VC 托盘气泡图标"指的是使用Visual C++(简称VC)进行开发时,创建系统托盘区域的图标并利用气泡提示功能来展示信息的技术。系统托盘,也称为通知区域,是位于任务栏右下角的小图标集合,通常用于...

    隐藏和显示Windows任务栏及托盘图标(VC 源码).rar

    托盘图标通常与系统托盘区(也称为通知区域)关联,可以通过创建一个`NOTIFYICONDATA`结构体来实现。在Windows API中,`Shell_NotifyIcon`函数用于管理和显示这些图标。以下是一个基本示例: ```cpp #include #...

    VC托盘程序源码

    【VC托盘程序源码】是一个使用Microsoft Visual C++(简称VC)开发的MFC(Microsoft Foundation Classes)应用程序,它的核心功能是将程序窗口最小化到系统托盘区域。MFC是微软提供的一套C++类库,它封装了Windows ...

Global site tag (gtag.js) - Google Analytics