Associate your own application to the right-click menu

zhaozj2021-02-16  45

Associate your own application to the right-click menu

In Windows2000, a shortcut menu will appear when the user right-click on a file in My Computer or Explorer. Unlike the location and object, the content of the shortcut menu is not the same. Users can customize some of the shortcut menu according to their own needs.

Suppose the user wants to associate its own application in the right-click shortcut menu of the TXT file type (the user's application is named mytxtapp.exe, in the D-root directory), the steps are as follows:

1. Find and open the hkey_classes_root / .txt ot key in the registry to find the default key value item to "txtfile".

2, find and open the hkey_classes_root / txtFile subkey in the registry, add a "My Text Application" subkey to the shell subkey of the subkey, and set the default key value item of the subkey " Open with my text program "," Open with my text "will appear as the name of the new add menu item appears in the right-click menu. The text users in the double quotation mark can set it at will.

3. Add a Command subkey to the HKEY_CLASSES_ROOT / TXTFILE / Shell / My Text Application Mracter and set the default key value item of the Command subkey to "D: /MytxtApp.exe% 1". The data is the program executed when the corresponding menu item is selected in the shortcut menu, and the% 1 indicates that the current file is automatically loaded when the application starts.

[Applications]

Use the VC Application Wizard AppWizard to establish a dialog-based program to accept all the default options.

Add a button control in the main dialog box, double-click the button, accept the default name, add the following code in the message response function of this button:

HKEY HRESULT;

CSTRING STR1, STR2;

Long Value;

LPCTSTR DATASET1 = "TXTFILE // SHELL // My Text Application";

LPCTSTSTR DATASET2 = "TXTFILE // SHELL // My Text App // Command";

Str1 = "Open with my text";

STR2 = "D: //mytxtapp.exe% 1";

DWORD DWPOS;

// Create "My Text Application" subkey

Value = regreateKeyex (HKEY_CLASS_ROOT, DATASET1, 0, NULL, REG_OPTION_NON_VOLATILE, Key_CREATE_SUB_KEY | Key_All_access, Null, & HRESULT, & DWPOS

// Set the data item of the "default" data item of the subkey to "Open with my text program"

Value = regSetValueex (HRESULT, NULL, 0, REG_SZ, (const Byte *) (LPCTSTSTSTSTSTS); Str1.getLength ());

// Create "Command" subkey

Value = regreateKeyex (HKEY_CLASS_ROOT, DATASET2, 0, NULL, REG_OPTION_NON_VOLATILE, Key_CREATE_SUB_KEY | Key_All_Access, Null, & HRESULT, & DWPOS

// Set the data of the "default" data item of the subkey as the path to the user application

Value = RegSetValueex (HRESULT, NULL, 0, REG_SZ, (Const Byte *) (LPCTSTSTSTSTSTS STR2, STR2.GETLENGTH ());

:: regcloseKey (HRESULT);

This code is used to associate users' applications.

Add a button control in the main dialog box, double-click the button, accept the default name, add the following code in the message response function of this button:

HKEY HKEY; LPCTSTR DATASET1 = "TXTFILE // SHELL // My Text Application";

LPCTSTR DATASET2 = "TXTFILE // SHELL";

Long Ret1 = :: regopenkeyex (HKEY_CLASS_ROOT, DATASET1, 0, Key_Read, & HKey);

/ / Delete "Command" subkey

:: RegdeleteKey (HKEY, LPCTSTR ("Command");

:: regcloseKey (HKEY);

Ret1 = :: regopenkeyex (HKEY_CLASS_ROOT, DATASET2, 0, Key_Read, & HKey);

// Delete "My Text Application" subkey

:: RegdeleteKey (HKEY, LPCTSTR ("My Text Application");

:: regcloseKey (HKEY);

This code is used to restore the default data in the registry.

At this point, we have completed the right-click menu in the extension of the .txt file, but the menu item does not appear on the right-click menu of the .txt file. If the user wants to associate its own application in the right-click menu of all file types, you should operate under the HKEY_CLASSES_ROOT / * / SHELL sub-key, the operation steps are the same. For related to other types of files, this type is pushed.

Above

Windows2000

with

VC6

In the middle.

转载请注明原文地址:https://www.9cbs.com/read-25033.html

New Post(0)