WIN7和WIN10也有右键复制路径

1次阅读
没有评论

可以支持文件,把路径变成反斜杠,用于粘贴在别地方去,如代码中,

#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);
}
}
}

正文完
 0
116博客
版权声明:本篇文章由 116博客 于2024-11-28发表,共计1944字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码