[C/C++] Windows下類比滑鼠右鍵操作

來源:互聯網
上載者:User

原文地址:http://blog.csdn.net/ariesjzj/article/details/8016476

用Windows提供的Shell類,使用者可以在C++中查詢和執行滑鼠右鍵的快顯功能表。如下面的例子通過類比滑鼠右鍵操作來達到將notepad.exe pin到taskbar的目的。注意其中的項名是帶快速鍵符的。

#define _CRT_SECURE_NO_WARNINGS#include <cstdio>#include <cstdlib>#include <cassert>#include <cstring>#include <windows.h>#include <iostream>#include <string>#include <shellapi.h>#include <Shobjidl.h>#include <shlobj.h>#import <Shell32.dll> using namespace std;int InvokeItemInPopupMenu(const char * path, const char * file_name, const char * item_name){CoInitialize(NULL); Shell32::IShellDispatchPtr ptrShell;  ptrShell.CreateInstance(__uuidof(Shell32::Shell));  _variant_t var((short)Shell32::ssfRECENT);  Shell32::FolderPtr ptrFolder = ptrShell->NameSpace(path); Shell32::FolderItemPtr ptrItem = ptrFolder->ParseName(file_name); Shell32::FolderItemVerbsPtr t_verbs = ptrItem->Verbs();for (long i = 0; i < t_verbs->Count; ++i) {Shell32::FolderItemVerbPtr t_verb = t_verbs->Item(i);cout << t_verb->Name << endl;if (!strcmp(t_verb->Name, item_name)) {t_verb->DoIt();Sleep(100);}}ptrItem.Release(); ptrFolder.Release(); ptrShell.Release();  CoUninitialize(); return 0; }int main(){InvokeItemInPopupMenu("C:\\Windows", "notepad.exe", "Pin to Tas&kbar");return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.