📄 ch22_18.htm
字号:
<html><head><title>Win32::Shortcut (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch22_17.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch22_19.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">22.18. Win32::Shortcut</h2><p><a name="INDEX-3384" /><a name="INDEX-3385" /><a name="INDEX-3386" />This module allows you to create andmanipulate Windows shortcut files (<em class="emphasis">.lnk</em> files)through Perl. The methods and properties of this module apply toshortcut objects created by <tt class="literal">new</tt>:</p><blockquote><pre class="code">use Win32::Shortcut;$link = Win32::Shortcut->new( );</pre></blockquote><p>This creates the shortcut object <tt class="literal">$link</tt>, on whichyou can set properties and save into a file. If you supply a filenameas an argument to <tt class="literal">new</tt>, the file will be loadedinto the shortcut object.</p><p>The object can also be accessed as if it was a normal hash reference.The following properties (hash keys) are available:</p><blockquote><pre class="code">$link->{'File'} $link->{'Path'}$link->{'ShortPath'}$link->{'WorkingDirectory'}$link->{'Arguments'}$link->{'Description'}$link->{'ShowCmd'}$link->{'Hotkey'}$link->{'IconLocation'}$link->{'IconNumber'}</pre></blockquote><p>See <a href="ch22_18.htm#perlnut2-CHP-22-SECT-18.1">Section 22.18.1, "Shortcut Properties"</a> for a description ofeach property.</p><p>The following example assumes you have a shortcut file named<em class="emphasis">test.lnk</em> in your current directory. This simplescript will tell you where this shortcut points to:</p><blockquote><pre class="code">use Win32::Shortcut;$link=new Win32::Shortcut( );$link->Load("test.lnk");print "Shortcut to: $link->{'Path'} $link->{'Arguments'} \n";$link->Close( );</pre></blockquote><p>But you can also modify its values: </p><blockquote><pre class="code">use Win32::Shortcut;$link=new Win32::Shortcut( );$link->Load("test.lnk");$link->{'Path'}=~s/C:/D:/i; # Move the target from C: to D:$link->{'ShowCmd'}=SW_NORMAL; # Runs in a normal window</pre></blockquote><p>The methods provided by Win32::Shortcut are as follows.</p><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>new Win32::Shortcut [<em class="replaceable">file</em>]</pre><p>Creates a new shortcut object. If a filename is passed in<em class="replaceable"><tt>file</tt></em>, automatically<tt class="literal">Load</tt> s this file also. Returns the object created,or <tt class="literal">undef</tt> on error.</p></div><a name="INDEX-3387" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Close</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">link</em>->Close( )</pre><p><a name="INDEX-3387" />Closes a shortcut object. It is notstrictly required to close the objects you created, since theWin32::Shortcut objects are automatically closed when the programends (or when you otherwise destroy such an object).</p><p>Also note that a shortcut is not automatically saved when it isclosed, even if you modified it. You have to call<tt class="literal">Save</tt> in order to apply modifications to a shortcutfile.</p></div><a name="INDEX-3388" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Load</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">link</em>->Load(<em class="replaceable">file</em>)</pre><p><a name="INDEX-3388" />Loads the content of the shortcut filenamed <em class="replaceable"><tt>file</tt></em> in a shortcut object and fillsthe properties of the object with its values. Returns<tt class="literal">undef</tt> on error, or true if everything wassuccessful.</p></div><a name="INDEX-3389" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Resolve</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">link</em>->Resolve([<em class="replaceable">flag</em>])</pre><p><a name="INDEX-3389" />Attempts to automaticallyresolve a shortcut and returns the resolved path, or<tt class="literal">undef</tt> on error; if there is no resolution, thepath is returned unchanged. Note that the path is automaticallyupdated in the <tt class="literal">Path</tt> property of the shortcut.</p><p>By default, this method acts quietly, but if you pass a value of<tt class="literal">0</tt> in the <em class="replaceable"><tt>flag</tt></em>parameter, it will eventually post a dialog box prompting the userfor more information. For example:</p><blockquote><pre class="code"># If the target doesn't exist...if(! -f $link->Path) { # Save the actual target for comparison $oldpath = $link->Path; # Try to resolve it (with dialog box) $newpath = $link->Resolve(0); die "Not resolved..." if $newpath == $oldpath;}</pre></blockquote></div><a name="INDEX-3390" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Save</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">link</em>->Save([<em class="replaceable">file</em>])</pre><p><a name="INDEX-3390" />Saves the content of the shortcutobject into the file named <em class="replaceable"><tt>file</tt></em>. If<em class="replaceable"><tt>file</tt></em> is omitted, the filename is takenfrom the <tt class="literal">File</tt> property of the object (which, ifnot changed, is the name of the last <tt class="literal">Load</tt> ed file).</p><p>If no file was loaded, and the <tt class="literal">File</tt> propertydoesn't contain a valid filename, the method returns<tt class="literal">undef</tt>, which is also returned on error. A truevalue is returned if everything was successful.</p></div><a name="INDEX-3391" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Set</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>$<em class="replaceable">link</em>->Set(<em class="replaceable">path</em>, <em class="replaceable">arguments</em>, <em class="replaceable">workingdirectory</em>, <em class="replaceable">description</em>, <em class="replaceable">showcmd</em>, <em class="replaceable">hotkey</em>, <em class="replaceable">iconlocation</em>, <em class="replaceable">iconnumber</em>)</pre></td><td align="right" /></tr></table><p><p><a name="INDEX-3391" />Sets all the properties of the shortcutobject with a single command. This method is supplied for convenienceonly; you can also set these values by changing the values of theproperties. For example:</p><blockquote><pre class="code">$link->Set("C:\PERL5\BIN\PERL.EXE", "-v", "C:\PERL5\BIN", "Prints out the version of Perl", SW_SHOWMAXIMIZED, hex('0x0337'), "C:\WINDOWS\SYSTEM\COOL.DLL", 1);</pre></blockquote><p>This is the same as: </p><blockquote><pre class="code">$link->Path("C:\PERL5\BIN\PERL.EXE");$link->Arguments("-v");$link->WorkingDirectory("C:\PERL5\BIN");$link->Description("Prints out the version of Perl");$link->ShowCmd(SW_SHOWMAXIMIZED);$link->Hotkey(hex('0x0337'));$link->IconLocation("C:\WINDOWS\SYSTEM\COOL.DLL");$link->IconNumber(1);</PRE></pre></blockquote></div><a name="perlnut2-CHP-22-SECT-18.1" /><div class="sect2"><h3 class="sect2">22.18.1. Shortcut Properties</h3><p>The properties of a shortcut object can be accessed as: </p><blockquote><pre class="code">$link->{'<em class="replaceable"><tt>property</tt></em>'}</pre></blockquote><p>For example, assuming that you have created a shortcut object with: </p><blockquote><pre class="code">$link=new Win32::Shortcut( );</pre></blockquote><p>you can see its description with: </p><blockquote><pre class="code">print $link->{'Description'};</pre></blockquote><p>You can, of course, also set it like this: </p><blockquote><pre class="code">$link->{'Description'}="This is a description";</pre></blockquote><p>The shortcut properties also have corresponding methods that can alsoset or read their values.</p><p>The properties of a shortcut reflect the content of the ShortcutProperties dialog box, which can be obtained by right-clicking on ashortcut file in the Windows 95 (or NT 4.0) Explorer and choosing"Properties." Shortcut propertiesare:</p><dl><dt><i><em class="emphasis">Arguments</em></i></dt><dd>The arguments associated with the shell link object. They are passedto the targeted program (see <em class="emphasis">Path</em>) when it getsexecuted. In fact, joined with <em class="emphasis">Path</em>, thisparameter forms the "Target" fieldof a Shortcut Properties dialog box.</p></dd><dt><i><em class="emphasis">Description</em></i></dt><dd>An optional description given to the shortcut. Not implemented inShortcut Properties dialog box.</p></dd><dt><i><em class="emphasis">File</em></i></dt><dd>The filename of the shortcut file opened with <tt class="literal">Load</tt>and/or the filename under which the shortcut will be saved with<tt class="literal">Save</tt> (if the <em class="replaceable"><tt>file</tt></em>argument is not specified).</p></dd><dt><i><em class="emphasis">Hotkey</em></i></dt><dd>The hotkey associated with the shortcut, in the form of a two-bytenumber, of which the first byte identifies the modifiers (Ctrl, Alt,Shift, etc.), and the second is the ASCII code of the character key.Corresponds to the "Shortcut key"field of a Shortcut Properties dialog box.</p></dd><dt><i><em class="emphasis">IconLocation</em></i></dt><dd>The file that contains the icon for the shortcut.</p></dd><dt><i><em class="emphasis">IconNumber</em></i></dt><dd>The number of the icon for the shortcut in the file pointed to by<em class="emphasis">IconLocation</em>, in case more that one icon iscontained in that file.</p></dd><dt><i><em class="emphasis">Path</em></i></dt><dd>The target of the shortcut. This (joined with<em class="emphasis">Arguments</em>) is the content of the"Target" field in a ShortcutProperties dialog box.</p></dd><dt><i><em class="emphasis">ShortPath</em></i></dt><dd>Same as <em class="emphasis">Path</em>, but expressed in a DOS-readableformat (8.3-character filenames). It is available as read-only (well,you can change it, but it has no effect on the shortcut; change<em class="emphasis">Path</em> instead) once you <tt class="literal">Load</tt> ashortcut file.</p></dd><dt><i><em class="emphasis">ShowCmd</em></i></dt><dd>The condition of the window in which the program will be executed(can be Normal, Minimized, or Maximized). Corresponds to the"Run" field of a ShortcutProperties dialog box. Allowed values are:</p><a name="ch22-186-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>Value</p></th><th><p>Meaning</p></th><th><p>Constant</p></th></tr><tr><td><p><tt class="literal">1</tt></p></td><td><p>Normal window</p></td><td><p>SW_SHOWNORMAL</p></td></tr><tr><td><p><tt class="literal">3</tt></p></td><td><p>Maximized</p></td><td><p>SW_SHOWMAXIMIZED</p></td></tr><tr><td><p><tt class="literal">7</tt></p></td><td><p>Minimized</p></td><td><p>SW_SHOWMINNOACTIVE</p></td></tr></table><p></dd><dt><i><em class="emphasis">WorkingDirectory</em></i></dt><dd>The directory in which the targeted program will be executed.Corresponds to the "Start in" fieldof a Shortcut Properties dialog box.<a name="INDEX-3392" /><a name="INDEX-3393" /><a name="INDEX-3394" /> </p></dd></dl></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch22_17.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch22_19.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">22.17. Win32::Service</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">22.19. Win32 Extensions</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -