建立程式選單和快捷方式的程式碼
// Windows Header Files:
#include <windows.h>
// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <atlbase.h>
#include <shlobj.h>
#ifndef LIB_H
#define LIB_H
bool CreateProgramGroup(const char * szGroupName);
bool CreateLink(const char * szLinkName, const char * szProgPath,
bool bDesktop, const char * szGroupName /* = 0 */);
#endif
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
bool CreateProgramGroup(const char * szGroupName)
{
static HKEY hkey;
if(ERROR_SUCCESS != RegOpenKeyEx(HKEY_CURRENT_USER,
"Software/MicroSoft/Windows/CurrentVersion/Explorer/Shell Folders",
0, KEY_READ | KEY_WRITE, &hkey))
{
return false;
}
char szRootDir[MAX_PATH];
DWORD dwSize = MAX_PATH;
DWORD dwType = REG_SZ;
if(ERROR_SUCCESS != RegQueryValueEx(hkey, "Programs", NULL, &dwType, (PBYTE)&szRootDir, &dwSize))
{
RegCloseKey(hkey);
return false;
}
RegCloseKey(hkey);
char szFullDir[MAX_PATH];
sprintf(szFullDir, "%s/%s", szRootDir, szGroupName);
CreateDirectory(szFullDir, NULL);
return true;
}
bool CreateLink(const char * szLinkName, const char * szProgPath,
bool bDesktop, const char * szGroupName /* = 0 */)
{
CComPtr<IShellLink> spIShellLink;
HRESULT hr = ::CoCreateInstance(CLSID_ShellLink, NULL, 1,
IID_IShellLink, (LPVOID *)&spIShellLink);
if(FAILED(hr))
{
return false;
}
spIShellLink->SetPath(szProgPath);
spIShellLink->SetShowCmd(SW_SHOWNORMAL);
//獲取程式所在目錄,設定為工作目錄
char * szSep = strrchr(szProgPath, '/');
if(!szSep)
return false;
//獲取程式目錄
char szProgFolder[MAX_PATH];
memset(szProgFolder, 0, MAX_PATH);
strncpy(szProgFolder, szProgPath, szSep - szProgPath + 1);
spIShellLink->SetWorkingDirectory(szProgFolder);
CComPtr<IPersistFile> spIPersistFile;
hr = spIShellLink->QueryInterface(IID_IPersistFile, (void **)&spIPersistFile);
if(FAILED(hr))
{
return false;
}
HKEY hkey;
if(ERROR_SUCCESS != RegOpenKeyEx(HKEY_CURRENT_USER,
"Software/MicroSoft/Windows/CurrentVersion/Explorer/Shell Folders",
0, KEY_READ | KEY_WRITE, &hkey))
{
return false;
}
char szRootDir[MAX_PATH];
DWORD dwSize = MAX_PATH;
DWORD dwType = REG_SZ;
if(ERROR_SUCCESS != RegQueryValueEx(hkey, bDesktop ? "Desktop" : "Programs", NULL, &dwType, (PBYTE)&szRootDir, &dwSize))
{
RegCloseKey(hkey);
return false;
}
RegCloseKey(hkey);
char szFullDir[MAX_PATH];
if(szGroupName)
sprintf(szFullDir, "%s/%s/%s.lnk", szRootDir, szGroupName, szLinkName);
else
sprintf(szFullDir, "%s/%s.lnk", szRootDir, szLinkName);
WCHAR wszFullDir[MAX_PATH];
::MultiByteToWideChar(0, 0, szFullDir, -1, wszFullDir, MAX_PATH);
hr = spIPersistFile->Save(wszFullDir, FALSE);
if(FAILED(hr))
{
return false;
}
::SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_PATH|SHCNF_FLUSH, szFullDir, 0);
return true;
}
/**
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
CoInitialize(NULL);
char szDemoExeDir[MAX_PATH];
char szExeFileName[MAX_PATH];
GetModuleFileName(NULL, szExeFileName, MAX_PATH);
sprintf(szDemoExeDir, szExeFileName);
char szUnInstallDir[MAX_PATH];
sprintf(szUnInstallDir, szExeFileName);
CreateProgramGroup("Demo程式組");
CreateLink("Demo", szDemoExeDir, FALSE, "Demo程式組");
CreateLink("Demo", szDemoExeDir, TRUE, NULL);
CreateLink("解除安裝Demo", szUnInstallDir, FALSE, "Demo程式組");
CoUninitialize();
return 0;
}
*/
#include <windows.h>
// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <atlbase.h>
#include <shlobj.h>
#ifndef LIB_H
#define LIB_H
bool CreateProgramGroup(const char * szGroupName);
bool CreateLink(const char * szLinkName, const char * szProgPath,
bool bDesktop, const char * szGroupName /* = 0 */);
#endif
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
bool CreateProgramGroup(const char * szGroupName)
{
static HKEY hkey;
if(ERROR_SUCCESS != RegOpenKeyEx(HKEY_CURRENT_USER,
"Software/MicroSoft/Windows/CurrentVersion/Explorer/Shell Folders",
0, KEY_READ | KEY_WRITE, &hkey))
{
return false;
}
char szRootDir[MAX_PATH];
DWORD dwSize = MAX_PATH;
DWORD dwType = REG_SZ;
if(ERROR_SUCCESS != RegQueryValueEx(hkey, "Programs", NULL, &dwType, (PBYTE)&szRootDir, &dwSize))
{
RegCloseKey(hkey);
return false;
}
RegCloseKey(hkey);
char szFullDir[MAX_PATH];
sprintf(szFullDir, "%s/%s", szRootDir, szGroupName);
CreateDirectory(szFullDir, NULL);
return true;
}
bool CreateLink(const char * szLinkName, const char * szProgPath,
bool bDesktop, const char * szGroupName /* = 0 */)
{
CComPtr<IShellLink> spIShellLink;
HRESULT hr = ::CoCreateInstance(CLSID_ShellLink, NULL, 1,
IID_IShellLink, (LPVOID *)&spIShellLink);
if(FAILED(hr))
{
return false;
}
spIShellLink->SetPath(szProgPath);
spIShellLink->SetShowCmd(SW_SHOWNORMAL);
//獲取程式所在目錄,設定為工作目錄
char * szSep = strrchr(szProgPath, '/');
if(!szSep)
return false;
//獲取程式目錄
char szProgFolder[MAX_PATH];
memset(szProgFolder, 0, MAX_PATH);
strncpy(szProgFolder, szProgPath, szSep - szProgPath + 1);
spIShellLink->SetWorkingDirectory(szProgFolder);
CComPtr<IPersistFile> spIPersistFile;
hr = spIShellLink->QueryInterface(IID_IPersistFile, (void **)&spIPersistFile);
if(FAILED(hr))
{
return false;
}
HKEY hkey;
if(ERROR_SUCCESS != RegOpenKeyEx(HKEY_CURRENT_USER,
"Software/MicroSoft/Windows/CurrentVersion/Explorer/Shell Folders",
0, KEY_READ | KEY_WRITE, &hkey))
{
return false;
}
char szRootDir[MAX_PATH];
DWORD dwSize = MAX_PATH;
DWORD dwType = REG_SZ;
if(ERROR_SUCCESS != RegQueryValueEx(hkey, bDesktop ? "Desktop" : "Programs", NULL, &dwType, (PBYTE)&szRootDir, &dwSize))
{
RegCloseKey(hkey);
return false;
}
RegCloseKey(hkey);
char szFullDir[MAX_PATH];
if(szGroupName)
sprintf(szFullDir, "%s/%s/%s.lnk", szRootDir, szGroupName, szLinkName);
else
sprintf(szFullDir, "%s/%s.lnk", szRootDir, szLinkName);
WCHAR wszFullDir[MAX_PATH];
::MultiByteToWideChar(0, 0, szFullDir, -1, wszFullDir, MAX_PATH);
hr = spIPersistFile->Save(wszFullDir, FALSE);
if(FAILED(hr))
{
return false;
}
::SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_PATH|SHCNF_FLUSH, szFullDir, 0);
return true;
}
/**
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
CoInitialize(NULL);
char szDemoExeDir[MAX_PATH];
char szExeFileName[MAX_PATH];
GetModuleFileName(NULL, szExeFileName, MAX_PATH);
sprintf(szDemoExeDir, szExeFileName);
char szUnInstallDir[MAX_PATH];
sprintf(szUnInstallDir, szExeFileName);
CreateProgramGroup("Demo程式組");
CreateLink("Demo", szDemoExeDir, FALSE, "Demo程式組");
CreateLink("Demo", szDemoExeDir, TRUE, NULL);
CreateLink("解除安裝Demo", szUnInstallDir, FALSE, "Demo程式組");
CoUninitialize();
return 0;
}
*/
相關文章
- 使用VBS建立快捷方式的程式碼
- WordPress外掛入口選單建立的位置程式碼
- 單選按鈕美化效果程式碼
- 程式的建立和程式的狀態
- 判斷核取方塊和單選按鈕程式碼
- 如何使用程式碼建立和讀取 SAP CRM 訂單的 Text 資料
- jQuery實現的禁用右鍵選單程式碼jQuery
- jQuery操作單選按鈕程式碼示例jQuery
- jQuery操作select下拉選單程式碼jQuery
- javascript自定義右鍵選單程式碼JavaScript
- 《機器學習實戰》程式清單3-4 建立樹的函式程式碼機器學習函式
- jQuery結合ul和li實現的下拉選單例項程式碼jQuery單例
- 資料結構--單連結串列的建立和遍歷(程式碼優化)資料結構優化
- 禁用文字選擇、右鍵選單例項程式碼單例
- No MFC,Only API——選單的建立和使用 (轉)API
- css簡單水平導航選單程式碼例項CSS
- select下拉選單跳轉效果程式碼
- 遮蔽滑鼠右鍵選單例項程式碼單例
- jQuery操作select下拉選單常用程式碼jQuery
- css二級下拉選單程式碼例項CSS
- jQuery 省市級聯選單程式碼例項jQuery
- 蜂巢式導航選單程式碼例項
- js選項卡簡單程式碼例項JS
- js省市級聯選單程式碼例項JS
- select下拉選單美化程式碼例項
- 刪除和新增select下拉選單option項程式碼例項
- 如何使用 JavaScript 程式碼建立虛擬滑鼠點選事件JavaScript事件
- 建立一個簡單的小程式
- 可以固定的頂部的導航選單簡單例項程式碼單例
- 程式碼的清晰和簡潔之間的選擇
- js建立和訪問陣列元素程式碼JS陣列
- qt程式建立及模板程式碼分析QT
- jQuery實現的禁用右鍵選單程式碼例項jQuery
- 垂直可伸縮的導航選單例項程式碼單例
- Windows 程式的建立和終止Windows
- javascript新增和刪除select下拉選單option項程式碼例項JavaScript
- win10怎麼建立桌面快捷方式_win10新增程式快捷方式到桌面的步驟Win10
- 常用操作單選框radio程式碼整理