好友
阅读权限10
听众
最后登录1970-1-1
|
本帖最后由 tonyfeng 于 2023-6-2 08:39 编辑
可以支持文件,把路径变成反斜杠,用于粘贴在别地方去,如代码中,
右键复制路径.zip
(6.41 KB, 下载次数: 70)
#pragma once
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
#define _AFXDLL
#include <iostream>
#include <afxwin.h> // MFC 核心组件和标准组件
void CopyToClipboard(HWND hWndOwner, LPCTSTR strText)
{
hWndOwner = NULL;
if (!OpenClipboard(hWndOwner))
{
return;
}
EmptyClipboard();
size_t cbStr = (_tcslen(strText) + 1) * sizeof(TCHAR);
HGLOBAL hData = GlobalAlloc(GMEM_MOVEABLE, cbStr);
memcpy_s(GlobalLock(hData), cbStr, strText, cbStr);
GlobalUnlock(hData);
// For the appropriate data formats...
UINT uiFormat = (sizeof(TCHAR) == sizeof(WCHAR)) ? CF_UNICODETEXT : CF_TEXT;
if (::SetClipboardData(uiFormat, hData) == NULL)
{
DWORD dwErro = GetLastError();
}
CloseClipboard();
GlobalFree(hData);
}
//单字节转宽字节
wchar_t* AnsiToUnicode(const char* szStr)
{
int nLen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0);
if (nLen == 0)
{
return NULL;
}
wchar_t* pResult = new wchar_t[nLen];
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen);
return pResult;
}
bool tianjiareg()
{
TCHAR path[MAX_PATH] = { 0 };
GetModuleFileName(NULL, path, MAX_PATH);
CString str;
CString str1 = TEXT(" \"%1\"");
str.Format(L"\"%s\"", path);
str = str + str1;
HKEY hKey = nullptr;
if (ERROR_SUCCESS != RegCreateKey(HKEY_CLASSES_ROOT, TEXT("Folder\\shell\\复制路径\\command\\"), &hKey))
{
return TRUE;
}
if (ERROR_SUCCESS != RegSetValue(hKey, nullptr, REG_SZ, str, 4))
{
return TRUE;
}
if (hKey == nullptr)
{
return TRUE;
}
RegCloseKey(hKey);
return TRUE;
}
bool tianjifileareg()
{
TCHAR path[MAX_PATH] = { 0 };
GetModuleFileName(NULL, path, MAX_PATH);
CString str;
CString str1 = TEXT(" \"%1\"");
str.Format(L"\"%s\"", path);
str = str + str1;
HKEY hKey = nullptr;
if (ERROR_SUCCESS != RegCreateKey(HKEY_CLASSES_ROOT, TEXT("*\\shell\\复制路径\\command\\"), &hKey))
{
return TRUE;
}
if (ERROR_SUCCESS != RegSetValue(hKey, nullptr, REG_SZ, str, 4))
{
return TRUE;
}
if (hKey == nullptr)
{
return TRUE;
}
RegCloseKey(hKey);
return TRUE;
}
int main(int argc, char* argv[])
{
tianjiareg();
tianjifileareg();
if (argc == 2)
{
CString path = AnsiToUnicode(argv[1]);
int str_len = path.GetLength();
if (str_len > 0)
{
path.Replace('\\', '/');
CopyToClipboard(NULL, path);
}
}
}
右键复制路径.zip
(6.41 KB, 下载次数: 198)
|
免费评分
-
查看全部评分
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|