Windows 7任务栏状态,代码最少

编程入门 行业动态 更新时间:2024-10-28 11:27:43
本文介绍了Windows 7任务栏状态,代码最少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为已知窗口句柄设置Windows 7任务栏按钮的最短代码是什么?

目标是编写一个控制台实用程序,来自批处理脚本的控制台窗口任务栏项目的进度和状态(颜色)。当脚本执行不同的任务,其控制台窗口的任务栏项应该代表当前状态。

我得到的窗口句柄与GetConsoleWindow()函数,似乎需要加载COM和Shell API的东西,我不明白。我发现一个例子使用一个整个GUI应用程序与MFC来演示API,但大多数是太复杂的我的小工具,我不明白它足够删除我不需要的东西。 / p>

该工具应该在Windows 7上用VS2010(C ++)编译,但也可以在早期的Windows版本上运行(如果某个功能不可用,则不执行任何操作)。

解决方案

我创建了一个类来一次为项目的Win7任务栏设置进度。这是我挖出的代码:

#include< shobjidl.h> #include< windows.h> #pragma comment(lib,Shell32.lib) #pragma comment(lib,Ole32.lib) 类Win7TaskbarProgress { public: Win7TaskbarProgress(); virtual〜Win7TaskbarProgress(); void SetProgressState(HWND hwnd,TBPFLAG标志); void SetProgressValue(HWND hwnd,ULONGLONG ullCompleted,ULONGLONG ullTotal); private: bool Init(); ITaskbarList3 * m_pITaskBarList3; bool m_bFailed; }; Win7TaskbarProgress :: Win7TaskbarProgress() { m_pITaskBarList3 = NULL; m_bFailed = false; } Win7TaskbarProgress ::〜Win7TaskbarProgress() { if(m_pITaskBarList3) { m_pITaskBarList3-& CoUninitialize(); } } void Win7TaskbarProgress :: SetProgressState(HWND hwnd,TBPFLAG flag) { if(Init()) m_pITaskBarList3 - > SetProgressState(hwnd,flag); } void Win7TaskbarProgress :: SetProgressValue(HWND hwnd,ULONGLONG ullCompleted,ULONGLONG ullTotal) { if(Init()) m_pITaskBarList3-> ; SetProgressValue(hwnd,ullCompleted,ullTotal); } bool Win7TaskbarProgress :: Init() { if(m_pITaskBarList3) return true; if(m_bFailed) return false; //为此线程初始化COM ... CoInitialize(NULL); CoCreateInstance(CLSID_TaskbarList,NULL,CLSCTX_INPROC_SERVER,IID_ITaskbarList3,(void **)& m_pITaskBarList3); if(m_pITaskBarList3) return true; m_bFailed = true; CoUninitialize(); return false; }

What would be the shortest code to set the state of a Windows 7 taskbar button for a known window handle?

The goal is to write a console utility that changes the progress and state (colour) of the console window taskbar item from a batch script. While the script performs different tasks, the taskbar item of its console window should represent the current state.

I get the window handle with the GetConsoleWindow() function, but then it seems to require loads of COM and Shell API stuff that I don't understand. One example I've found uses a whole GUI application with MFC to demonstrate the API, but most of it is way too complicated for my little tool and I don't understand it well enough to remove the stuff I don't need.

The tool should compile on Windows 7 with VS2010 (C++) but also run on earlier Windows versions (doing nothing if a feature is not available).

解决方案

I created a class to set the progress in the Win7 taskbar for a project at one time. This is the code I dug up:

#include <shobjidl.h> #include <windows.h> #pragma comment(lib, "Shell32.lib") #pragma comment(lib, "Ole32.lib") class Win7TaskbarProgress { public: Win7TaskbarProgress(); virtual ~Win7TaskbarProgress(); void SetProgressState(HWND hwnd, TBPFLAG flag); void SetProgressValue(HWND hwnd, ULONGLONG ullCompleted, ULONGLONG ullTotal); private: bool Init(); ITaskbarList3* m_pITaskBarList3; bool m_bFailed; }; Win7TaskbarProgress::Win7TaskbarProgress() { m_pITaskBarList3 = NULL; m_bFailed = false; } Win7TaskbarProgress::~Win7TaskbarProgress() { if (m_pITaskBarList3) { m_pITaskBarList3->Release(); CoUninitialize(); } } void Win7TaskbarProgress::SetProgressState( HWND hwnd, TBPFLAG flag ) { if (Init()) m_pITaskBarList3->SetProgressState(hwnd, flag); } void Win7TaskbarProgress::SetProgressValue( HWND hwnd, ULONGLONG ullCompleted, ULONGLONG ullTotal ) { if (Init()) m_pITaskBarList3->SetProgressValue(hwnd, ullCompleted, ullTotal); } bool Win7TaskbarProgress::Init() { if (m_pITaskBarList3) return true; if (m_bFailed) return false; // Initialize COM for this thread... CoInitialize(NULL); CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_ITaskbarList3, (void **)&m_pITaskBarList3); if (m_pITaskBarList3) return true; m_bFailed = true; CoUninitialize(); return false; }

更多推荐

Windows 7任务栏状态,代码最少

本文发布于:2023-11-13 12:26:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584359.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:任务栏   状态   代码   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!