[C/C ++] simulate right-click operation in Windows

Source: Internet
Author: User

Address: http://blog.csdn.net/ariesjzj/article/details/8016476

With the shell class provided by Windows, you can query and execute the shortcut menu in C ++. For example, the following example uses the mouse's right-click operation to pin notepad.exe to taskbar. Note that the item name contains a shortcut key.

#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;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.