SummaryIn the past, we often copy the program on the PC after synchronization and select "paste shortcut" to add a shortcut menu on smartphone, this article introduces another method for creating shortcuts on smartphone through programming.
KeywordsSmartphone, shortcut, Windows Mobile,. NET Compact framework, C #
Although you can use powerful vs to create a cab installation file, you can simply click twice to add a shortcut to the installation file, but sometimes we also need to implement this function in our own program. For example, we hope that our program does not need to be installed, but there is a shortcut to facilitate user operation.
It is easy to copy a shortcut menu on the Pocket PC. Just select your file in the file browser and press it long. Select copy in the menu ):
Then go to the folder you want to paste and select Paste shortcut cut (paste shortcut:
However, there is no paste shortcut cut option on smartphones. At this time, creating shortcuts is a little troublesome. You can copy and paste files on smartohone on a PC only after the files are synchronized to the computer.
In C ++, we can use
Bool shcreateshortcut (lptstr szshortcut,
Lptstr sztarget );
Or
DWORD
Shcreateshortcutex (lptstr lpszdir, lptstr lpsztarget, lptstr szshortcut,
Lpdword maid cut );
To implement this function. Of course, you can use P/invoke in the managed code to call these two functions, for example:
Shcreateshortcut (@ "\ windows \ Startup \" +
Path. getfilenamewithoutextension (applicationpath) + ". lnk", "\" "+
Applicationpath + "\"");
[Dllimport ("coredll. dll", entrypoint = "shcreateshortcut")]
Private Static extern bool shcreateshortcut (string shortcut, string target );
However, we hope to make it more elegant in the form of C # implementation, and omit the overhead of P/invoke.
In fact, in Windows CE and Windows Mobile, the shortcut file itself is a simple plain text file. If you use NotePad to open a shortcut (. Ink file) on those Ce-based systems (such as smartphone), you will see text content similar to the following:
26 # "\ windows \ bubblebreaker.exe"
Its basic structure is as follows:
{Number of characters in the target path command} # "{target path command }".
TipsThe number of characters contained in the target path command.Not includedQuotation marks. Although similar to Windows Shell, quotation marks can be omitted when the path does not contain spaces, we recommend that you always use quotation marks.
Note that the target path command may contain parameters. You need to use spaces to separate the parameters from the path. If there are N parameters, then, both N and the parameter should be separated by spaces, which is also the reason for the habit of using the path in quotation marks. For example:
61 # "\ Program Files \ Windows Media
Player \ wmplayer.exe "\ fullscreen
Indicates that the path is \ Program Files \ Windows Media Player \ wmplayer.exe, and a parameter "\ fullscreen" is passed"
With the above understanding of the shortcut file structure, we can easily create our own shortcuts:
Streamwriter SW;
Filestream FS;
/** // <Summary>
/// Create a shortcut
/// </Summary>
/// <Param name = "destination"> shortcuts </param>
/// <Param name = "shortcut cut"> shortcut name </param>
/// <Param name = "Parameters"> shortcut parameters </param>
/// <Returns> return true if creation is successful </returns>
Private bool createshortcut (string destination, string shortcut, string parameters)
{
Try
{
Bool isquoted = false;
// Check whether the target path contains spaces
Destination = destination. Trim ();
If (destination. indexof ('')>-1)
Isquoted = true;
String link = destination;
Int Len = destination. length;
If (isquoted)
Link = "\" "+ link + "\"";
If (! String. isnullorempty (parameters ))
{
// If there is a parameter, the length of the parameter and the space between the parameter and the path are added.
Len + = parameters. Length + 1;
Link + = "" + parameters;
}
// Open the file stream and write the above information
FS = new filestream (shortcut,
Filemode. Create,
Fileaccess. Write,
Fileshare. readwrite );
Sw = new streamwriter (FS );
Sw. writeline (Len. tostring () + "#" + link );
}
Catch {}
Finally
{
// Do not forget to close the resource
Sw. Close ();
FS. Close ();
}
Return true;
}
Now we use it to create a shortcut in the Start Menu:
Createshortcut (@ "\ storage card \ shortcut_sp.exe", @ "\ windows \ Start Menu \ MyApp. lnk ","");
PS: I have configured the memory card directory of the simulator as the output directory of the program.
The program running effect is as follows:
Click "creat" to create a shortcut
Then we can find it in the Start Menu: start --> all programs --> more
If the effect is reached, there is no installation package, and no P/invoke, you can also create your own shortcuts on the smartphone.
The complete code is downloaded here
Enjoy!
Freesc Huang
Yellow winter <fox23> @ HUST http://blog.csdn.net/fox_click/archive/2008/04/15/4684378.aspx
2008/4/15