Create shortcuts in the Application

Source: Internet
Author: User

1 Introduction
In Windows 3. X, software installers usually use the Dynamic Data Exchange (DDE) method to create a program group (Program Group) in the program manager (program manager ). This method is no longer applicable to Windows 95/98/NT 32 Operating Systems with significant improvements in the system kernel and user interface. Currently, almost all installers add new menu items in the "Start" menu, or create a new shortcut on the desktop) (sometimes, we need to create the corresponding food, food, and pop-up window. Why ?, In this way, you can start the software easily and quickly.
It is not complicated to create these menu items or shortcuts manually. it is introduced in the general Windows user manual. I believe everyone is familiar with it and will not go into details here. On the basis of relevant information, I found a way to complete the above work in the application program through practical exploration. This is exactly what is necessary to develop and install programs.

2 essence of shortcuts
Windows shortcuts are actually a data file with the extension lnk, which contains all objects used to access a Windows Object (that is, all objects that can be viewed in the resource manager, including files, folders, such as the path and name of the target object, working directory, command line parameters to be passed, initial display status, icons, and shortcut keys during running. By right-clicking a shortcut and selecting "properties" in the pop-up menu, you can observe these properties of the shortcut.
If the shortcut data file is stored in the C:/Windows/desktop subdirectory, the shortcut is displayed on the desktop, and if it is stored in the C: in the/Windows/"Start Menu"/programs subdirectory, this shortcut appears as a menu item of the "Start" menu. The folder on the desktop and the menu Group of the "Start" menu are the sub-directories under the two sub-directories.

3 programming ideas
The shortcut of Windows Shell is based on the Component Object Model COM (Component Object modal) of OLE technology. Using the com model, an application can call some functions of another application. For technical details, see the relevant literature.
After understanding the basic principles above, it is easier to create a shortcut for Windows. Use OLE to call the cocreateinstance () function to create an iid_ishelllink instance and obtain its interface pointer. You can use this interface pointer to set its attributes. To make this information a quick data file (*. to save the lnk format, you also need to obtain the iid_ipersistfile interface pointer from the iid_ishelllink object, so that you can call its member function save () to save the information previously set.
As for how to delete shortcuts and create and delete folders, you only need to simply call the shfileoperation () file operation function.
In addition, after completing the preceding operations, you must call the shchangenovel () function to notify the Windows Shell of changes to update the display status in time.

4. Application Example
To demonstrate how to use the above ideas, we use ms vc ++ 5.0 to compile the following example Program (for example ). This example is a dialog box-based application. Two circular buttons are used to set the folder or shortcut location for creation/deletion. The following four buttons are used to perform different operations. In addition, a simple dialog box is required for the program to enter the name of the folder or shortcut to be created.

The code to be added after the program is created (in the box) is as follows ):
// Sortcut. cpp:
Bool csortcutapp: initinstance ()
{
......
Coinitialize (null );
Csortcutdlg DLG;
M_pmainwnd = & DLG;
......
Couninitialize ();
Return false;
}
// Sortcutdlg. cpp:
# Include "stdafx. H"
# Include "sortcut. H"
# Include "sortcutdlg. H"

# Include "namedlg. H"

# Ifdef _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif

// Pidl of the Start folder
Int nbeginat = csidl_topics topdirectory;
......
/// Browse folders
Bool browseforfolder (
Lpitemidlist pidlroot, // view pidl at the beginning
Lpitemidlist * ppidldestination,
// Pidl selected when Browsing ends
Lpcstr lpsztitle) // prompt text in the browser dialog box
{Browseinfo brinfo;

Zeromemory (& brinfo, sizeof (brinfo ));
Brinfo. hwndowner = hwnd_desktop;
Brinfo. pidlroot = pidlroot;
Brinfo. lpsztitle = lpsztitle;

// Browse folders
* Ppidldestination = shbrowseforfolder (& brinfo );
// The cancel button is selected.
If (null = * ppidldestination)
Return false;
Return true;
}
// Obtain the target application name of the shortcut
Selectmenuitem (lpstr szfilename)
{
Openfilename ofn;
Static char szfilter [] = "programs/0 *. EXE/0 ";

Zeromemory (& ofn, sizeof (openfilename ));
Ofn. lstructsize = sizeof (openfilename );
Ofn. hwndowner = hwnd_desktop;
Ofn. lpstrfilter = szfilter;
Ofn. nfilterindex = 0;
Ofn. nmaxfile = max_path;
Ofn. lpstrtitle = "select the target application :";
Ofn. lpstrfile = szfilename;
Ofn. Flags = ofn_filemustexist |
Ofn_pathmustexist | ofn_explorer;
// File browsing
If (! Getopenfilename (& ofn) // The cancel button is selected.
Return false;
Return true;
}
// Obtain the name of the shortcut to be created
Bool getshortcutcrt (lpstr szpath)
{
Lpitemidlist pidlbeginat, pidldestination;

// Obtain the pidl of the Start Menu or desktop.
Shgetspecialfolderlocation (hwnd_desktop,
Nbeginat, & pidlbeginat );
// Obtain the location of the shortcut to be created
If (! Browseforfolder (pidlbeginat, & pidldestination,
"Select the location of the shortcut :"))
Return false;
// Convert pidl to the path name
Shgetpathfromidlist (pidldestination, szpath );
// Obtain the shortcut name
Cnamedlg name_dlg;
If (name_dlg.domodal () = idcancel)
Return false;
// Add the shortcut name and extension. lnk to the path name
// Form a complete shortcut data file name
Wsprintf (szpath + lstrlen (szpath), "// % S. lnk ",
Name_dlg.m_strname );
Return true;
}
// Create a shortcut
Bool CreateLink (
Lpstr szpath, // target application name of the shortcut
Lpstr szlink) // shortcut data file name (*. lnk)
{
Hresult hres;
Ishelllink * PSL;
Ipersistfile * PPF;
Word wsz [max_path];
// Create an ishelllink instance
Hres = cocreateinstance (clsid_shelllink, null,
Clsctx_inproc_server, iid_ishelllink,
(Void **) & PSL );
If (failed (hres ))
Return false;
// Set the target application
Psl-> setpath (szpath );
// Set the shortcut key (SHIFT + Ctrl + 'R ')
Psl-> sethotkey (makeword ('R ',
Hotkeyf_shift | hotkeyf_control ));
// Obtain its ipersistfile interface from ishelllink
// Used to save the shortcut data file (*. lnk)
Hres = PSL-> QueryInterface (iid_ipersistfile,
(Void **) & PPF );
If (failed (hres ))
Return false;
// Ensure that the data file name is in ANSI format
Multibytetowidechar (cp_acp, 0, szlink,-1,
Wsz, max_path );
// Call ipersistfile: Save
// Save the shortcut data file (*. lnk)
Hres = PPF-> Save (wsz, stgm_readwrite );
// Release the ipersistfile and ishelllink Interfaces
PPF-> release ();
Psl-> release ();
Return true;
}
// Delete a folder
Bool deletefolder (lpstr pszfolder)
{
Shfileopstruct Fos;

Zeromemory (& FOS, sizeof (FOS ));
FOS. hwnd = hwnd_desktop;
FOS. wfunc = fo_delete;
FOS. fflags = fof_silent | fof_allowundo;
FOS. pfrom = pszfolder;

// Delete the folder and its content
If (0! = Shfileoperation (& FOS ))
Return false;
Return true;
}
// Obtain the shortcut for deletion
Bool getshortcutdel (
Lpstr lpszinitdir, // select the start directory of the file
Lpstr lpszshortcut) // shortcut name
{
Openfilename ofn;
Char szfilter [] = "Shortcuts/0 *. lnk/0 ";

Zeromemory (& ofn, sizeof (openfilename ));
Ofn. lstructsize = sizeof (openfilename );
Ofn. hwndowner = hwnd_desktop;
Ofn. lpstrfilter = szfilter;
Ofn. nfilterindex = 0;
Ofn. nmaxfile = max_path;
Ofn. lpstrtitle = "select the shortcut to delete :";
Ofn. lpstrfile = lpszshortcut;
Ofn. lpstrinitialdir = lpszinitdir;
Ofn. Flags = ofn_filemustexist |
Ofn_pathmustexist | ofn_explorer |
Ofn_nodereferencelinks;
// File browsing
If (! Getopenfilename (& ofn) // The cancel button is selected.
Return false;
Return true;
}
// Delete a shortcut data file (*. lnk)
Bool deletelink (lpstr lpszshortcut)
{
Shfileopstruct Fos;

Zeromemory (& FOS, sizeof (FOS ));
FOS. hwnd = hwnd_desktop;
FOS. wfunc = fo_delete;
FOS. pfrom = lpszshortcut;
FOS. PTO = NULL;
FOS. fflags = fof_silent | fof_allowundo;
// Delete the shortcut (*. lnk)
If (0! = Shfileoperation (& FOS ))
Return false;
Return true;
}
// Notify the shell about changes
Void policyshell (long weventid, // event flag
Lpstr szpath) // path
{
Shchangenotify (weventid,
Shcnf_flush | shcnf_path,
Szpath, 0 );
// Obtain the parent directory of szpath
Char * P;
For (P = szpath + lstrlen (szpath)-1;
* P! = '//';
P --);
* P = '/0 ';
Shchangenovel (shcn_updatedir
| Shcn_interrupt,
Shcnf_flush | shcnf_path, szpath, 0 );
}
//////////////////////////////////////// ///////////
// Csortcutdlg Dialog
Csortcutdlg: csortcutdlg (cwnd * pparent/* = NULL */)
: Cdialog (csortcutdlg: IDD, pparent)
{
// {Afx_data_init (csortcutdlg)
// Set the circular button "desktop" to the selected status
M_nlocation = 0;
......
}
......
Void csortcutdlg: oncreategroup ()
{
Lpitemidlist pidlbeginat, pidldestination;
Char szpath [max_path];

// Obtain the pidl of the Start Menu or desktop.
Shgetspecialfolderlocation (hwnd_desktop,
Nbeginat, & pidlbeginat );
// Obtain the parent folder of the new folder
If (! Browseforfolder (pidlbeginat,
& Pidldestination,
"Select the new Folder/menu group location :"))
Return;
// Convert pidl to the path name
Shgetpathfromidlist (pidldestination, szpath );
// Obtain the name of the new folder
Cnamedlg name_dlg;
If (name_dlg.domodal () = idcancel)
Return;
// Create a complete new folder name
Wsprintf (szpath + lstrlen (szpath), "// % s ",
Name_dlg.m_strname );
// Create a folder (subdirectory)
If (! Createdirectory (szpath, null ))
{
MessageBox ("failed to create folder! ");
Return;
}
// Notify the shell about changes
Policyshell (shcn_mkdir | shcn_interrupt,
Szpath );
}
Void csortcutdlg: oncreateitem ()
{
Char szpath [max_path] = "";
// Target application name of the shortcut
Char szlink [max_path] = "";
// The data file name of the shortcut
// Obtain the target application name of the shortcut
If (! Selectmenuitem (szpath ))
Return;
// Obtain the folder where the new shortcut is located
// Form the data file name
If (! Getshortcutcrt (szlink ))
Return;
// Create a shortcut
If (! CreateLink (szpath, szlink ))
Return;
// Notify the shell about changes
Policyshell (shnning_create | shnning_interrupt,
Szlink );
}
Void csortcutdlg: ondeletegroup ()
{
Lpitemidlist pidlbeginat, pidlfolder;
Char szpath [max_path] = "";

// Obtain the pidl of the Start Menu or desktop.
Shgetspecialfolderlocation (hwnd_desktop,
Nbeginat, & pidlbeginat );
// Get the folder to be deleted
If (! Browseforfolder (pidlbeginat, & pidlfolder,
"Select the folder/menu Group to delete :"))
Return;
// Convert pidl to a path name
Shgetpathfromidlist (pidlfolder, szpath );
// Delete a folder
If (! Deletefolder (szpath ))
Return;
// Notify the shell about changes
Policyshell (shcn_rmdir | shcn_interrupt,
Szpath );
}
Void csortcutdlg: ondeleteitem ()
{
Lpitemidlist pidlbeginat;
Char szshortcut [max_path] = "",
Szpath [max_path] = "";

// Obtain the pidl of the Start Menu or desktop.
Shgetspecialfolderlocation (hwnd_desktop,
Nbeginat, & pidlbeginat );
// Convert pidl to a path name
Shgetpathfromidlist (pidlbeginat, szpath );
// Obtain the shortcut for deletion
If (! Getshortcutdel (szpath, szshortcut ))
Return;
// Delete shortcuts
If (! Deletelink (szshortcut ))
Return;
// Notify shell about changes
Policyshell (shnning_delete | shnning_interrupt,
Szshortcut );
}
Void csortcutdlg: ondesktop ()
{
// Set the start folder to desktop
Nbeginat = csidl_topics topdirectory;
}
Void csortcutdlg: onstartmenu ()
{
// Set the start folder to the Start Menu.
Nbeginat = csidl_startmenu;
}

 

Excerpt:

Http://sanjianxia.diy.myrice.com/vc/index.htm

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.