⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shortcut.cpp

📁 大量windows shell编程例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                                 reinterpret_cast<LPVOID*>(&pShellLink));
   if(FAILED(hr))
      return hr;

   // Get the IPersistFile interface to load the LNK file
   hr = pShellLink->QueryInterface(IID_IPersistFile, reinterpret_cast<LPVOID*>(&pPF));
   if(FAILED(hr))
   {
      pShellLink->Release();
      return hr;
   }

   // Load the shortcut (Unicode name)
   MultiByteToWideChar(CP_ACP, 0, szLnkFile, -1, wszLnkFile, MAX_PATH);
   hr = pPF->Load(wszLnkFile, STGM_READ);
   if(FAILED(hr))
   {
      pPF->Release();
      pShellLink->Release();
      return hr;
   }

   // Resolve the link
   hr = pShellLink->Resolve(NULL, SLR_ANY_MATCH);
   if(FAILED(hr))
   {
      pPF->Release();
      pShellLink->Release();
      return hr;
   }

   // Extract the information to fill lpss
   if(lpss != NULL)
   {
      TCHAR szPath[MAX_PATH] = {0};
      TCHAR szDesc[MAX_PATH] = {0};
      TCHAR szIcon[MAX_PATH] = {0};
      WORD w = 0;
      WORD wIcon = 0;
      WIN32_FIND_DATA wfd;

      pShellLink->GetPath(szPath, MAX_PATH, &wfd, SLGP_SHORTPATH);
      pShellLink->GetDescription(szDesc, MAX_PATH);
      pShellLink->GetHotkey(&w);
      pShellLink->GetIconLocation(szIcon, MAX_PATH, reinterpret_cast<int*>(&wIcon));

      lpss->pszTarget = szPath;
      lpss->pszDesc = szDesc;
      lpss->pszIconPath = szIcon;
      lpss->wHotKey = w;
      lpss->wIconIndex = wIcon;
   }

   pPF->Release();
   pShellLink->Release();
   return hr;
}


void OnBrowse(HWND hDlg, WPARAM wID)
{
   TCHAR szFile[MAX_PATH] = {0};

   OPENFILENAME ofn;
   ZeroMemory(&ofn, sizeof(OPENFILENAME));
   ofn.lStructSize = sizeof(OPENFILENAME);
   if(wID == IDC_SHORTCUT)
   {
      ofn.lpstrFilter = __TEXT("Shortcuts\0*.lnk\0");
      ofn.Flags = OFN_NODEREFERENCELINKS;
   }
   else
      ofn.lpstrFilter = __TEXT("All files\0*.*\0");

   ofn.nMaxFile = MAX_PATH;
   ofn.lpstrInitialDir = __TEXT("c:\\");
   ofn.lpstrFile = szFile;
   if(!GetOpenFileName(&ofn))
      return;
   else
      SetDlgItemText(hDlg, wID, ofn.lpstrFile);
   return;
}


void MakeReportView(HWND hwndList, LPTSTR* psz, int iNumOfCols)
{
   RECT rc;

   DWORD dwStyle = GetWindowStyle(hwndList);
   SetWindowLong(hwndList, GWL_STYLE, dwStyle | LVS_REPORT);
   GetClientRect(hwndList, &rc);

   // Handle pairs of entries. Array size is assumed to be 2 * iNumOfCols
   for(int i = 0 ; i < 2 * iNumOfCols ; i = i + 2)
   {
      LV_COLUMN lvc;
      ZeroMemory(&lvc, sizeof(LV_COLUMN));
      lvc.mask = LVCF_TEXT | LVCF_WIDTH;
      lvc.pszText = psz[i];
      if(reinterpret_cast<int>(psz[i + 1]) == 0)
         lvc.cx = rc.right / iNumOfCols;
      else
         lvc.cx = reinterpret_cast<int>(psz[i + 1]);

      ListView_InsertColumn(hwndList, i, &lvc);
   }
   return;
}


void AddStringToReportView(HWND hwndList, LPTSTR psz, int iNumOfCols)
{
   LV_ITEM lvi;
   ZeroMemory(&lvi, sizeof(LV_ITEM));
   lvi.mask = LVIF_TEXT;
   lvi.pszText = psz;
   lvi.cchTextMax = lstrlen(psz);
   lvi.iItem = 0;
   ListView_InsertItem(hwndList, &lvi);

   // Other columns
   for(int i = 1 ; i < iNumOfCols ; i++)
   {
      psz += lstrlen(psz) + 1;
      ListView_SetItemText(hwndList, 0, i, psz);
   }
   return;
}


void HotkeyToString(WORD wHotKey, LPTSTR pszBuf)
{
   BYTE bKey = LOBYTE(wHotKey);
   BYTE bMod = HIBYTE(wHotKey);

   if(bMod & HOTKEYF_CONTROL)
      lstrcpy(pszBuf, __TEXT("Ctrl"));

   if(bMod & HOTKEYF_SHIFT)
      if(lstrlen(pszBuf))
         lstrcat(pszBuf, __TEXT(" + Shift"));
      else
         lstrcpy(pszBuf, __TEXT("Shift"));

   if(bMod & HOTKEYF_ALT)
      if(lstrlen(pszBuf))
         lstrcat(pszBuf, __TEXT(" + Alt"));
      else
         lstrcpy(pszBuf, __TEXT("Alt"));

   TCHAR s[2] = {0};
   wsprintf(s, __TEXT("%c"), bKey);
   if(lstrlen(pszBuf))
   {
      lstrcat(pszBuf, __TEXT(" + "));
      lstrcat(pszBuf, s);
   }
   else
      lstrcpy(pszBuf, s);
}


void DoCreateShortcut(HWND hDlg)
{
   SHORTCUTSTRUCT ss;
   ZeroMemory(&ss, sizeof(SHORTCUTSTRUCT));
   TCHAR szTarget[MAX_PATH] = {0};
   TCHAR szDesc[MAX_PATH] = {0};

   // Get the hotkey
   ss.wHotKey = static_cast<WORD>(SendDlgItemMessage(hDlg, IDC_HOTKEY, HKM_GETHOTKEY, 0, 0));

   // Get target and description
   GetDlgItemText(hDlg, IDC_TARGET, szTarget, MAX_PATH);
   GetDlgItemText(hDlg, IDC_DESCRIPTION, szDesc, MAX_PATH);
   ss.pszTarget = szTarget;
   ss.pszDesc = szDesc;

   // Determine the shortcut file name
   // Get the target folder & final backslash
   HWND hwndCbo = GetDlgItem(hDlg, IDC_SPECIAL);
   int i = ComboBox_GetCurSel(hwndCbo);
   DWORD nFolder = ComboBox_GetItemData(hwndCbo, i);

   TCHAR szPath[MAX_PATH] = {0};
   SHGetSpecialFolderPath(hDlg, szPath, nFolder, FALSE);
   if(szPath[lstrlen(szPath) - 1] != '\\')
      lstrcat(szPath, __TEXT("\\"));

   TCHAR szLnkFile[MAX_PATH] = {0};
   GetDlgItemText(hDlg, IDC_LNKFILE, szLnkFile, MAX_PATH);
   lstrcat(szPath, szLnkFile);
   lstrcat(szPath, __TEXT(".lnk"));

   // Create
   SHCreateShortcutEx(szPath, &ss);

   // Update UI
   SetDlgItemText(hDlg, IDC_SHORTCUT, szPath);
   return;
}


void DoResolveShortcut(HWND hDlg, LPTSTR pszFile)
{
   TCHAR szLnkFile[MAX_PATH] = {0};
   if(pszFile == NULL)
     GetDlgItemText(hDlg, IDC_SHORTCUT, szLnkFile, MAX_PATH);
   else
     lstrcpy(szLnkFile, pszFile);

   // Resolve the shortcut
   SHORTCUTSTRUCT ss;
   HRESULT hr = SHResolveShortcut(szLnkFile, &ss);
   if(FAILED(hr))
      return;

   //////////////////////////////////////////////
   // Update UI

   // Create the string for the listview
   TCHAR pszBuf[1024] = {0};
   LPTSTR psz = pszBuf;

   lstrcpy(psz, ss.pszTarget);
   lstrcat(psz, __TEXT("\0"));
   psz += lstrlen(psz) + 1;

   lstrcpy(psz, ss.pszDesc);
   lstrcat(psz, __TEXT("\0"));
   psz += lstrlen(psz) + 1;

   // Try to get the text version of the hotkey
   TCHAR szKey[30] = {0};
   HotkeyToString(ss.wHotKey, szKey);

   lstrcpy(psz, szKey);
   lstrcat(psz, __TEXT("\0"));

   // Add a new item to the report list view (3 columns)
   HWND hwndList = GetDlgItem(hDlg, IDC_VIEW);
   AddStringToReportView(hwndList, pszBuf, 3);
   return;
}


void HandleFileDrop(HWND hDlg, HDROP hDrop)
{
   // Check the window being dropped on
   POINT pt;
   DragQueryPoint(hDrop, &pt);
   ClientToScreen(hDlg, &pt);
   HWND hwndDrop = WindowFromPoint(pt);
   if(hwndDrop != GetDlgItem(hDlg, IDC_VIEW))
   {
      Msg(__TEXT("Sorry, you have to drop over the list view control!"));
      return;
   }

   // Now check the files
   int iNumOfFiles = DragQueryFile(hDrop, -1, NULL, 0);
   for(int i = 0 ; i < iNumOfFiles; i++)
   {
      TCHAR szFileName[MAX_PATH] = {0};
      DragQueryFile(hDrop, i, szFileName, MAX_PATH);
      DoResolveShortcut(hDlg, szFileName);
   }

   DragFinish(hDrop);
}

/*  End of file: Shortcut.cpp  */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -