[NOTEPAD] Simplify GetSelectionText (#5083)

Use EM_GETHANDLE message to get text. CORE-18837
This commit is contained in:
Katayama Hirofumi MZ 2023-02-20 22:00:49 +09:00 committed by GitHub
parent f7f2ed201a
commit 0cd80c17f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -242,68 +242,41 @@ BOOL HasFileExtension(LPCTSTR szFilename)
int GetSelectionTextLength(HWND hWnd)
{
DWORD dwStart = 0;
DWORD dwEnd = 0;
DWORD dwStart = 0, dwEnd = 0;
SendMessage(hWnd, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
return dwEnd - dwStart;
}
int GetSelectionText(HWND hWnd, LPTSTR lpString, int nMaxCount)
{
DWORD dwStart = 0;
DWORD dwEnd = 0;
DWORD dwSize;
HRESULT hResult;
LPTSTR lpTemp;
if (!lpString)
{
return 0;
}
DWORD dwStart = 0, dwEnd = 0;
INT cchText = GetWindowTextLength(hWnd);
LPTSTR pszText;
HLOCAL hLocal;
HRESULT hr;
SendMessage(hWnd, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
if (dwStart == dwEnd)
{
if (!lpString || dwStart == dwEnd || cchText == 0)
return 0;
}
dwSize = GetWindowTextLength(hWnd) + 1;
lpTemp = HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(TCHAR));
if (!lpTemp)
{
hLocal = (HLOCAL)SendMessage(hWnd, EM_GETHANDLE, 0, 0);
pszText = (LPTSTR)LocalLock(hLocal);
if (!pszText)
return 0;
}
dwSize = GetWindowText(hWnd, lpTemp, dwSize);
hr = StringCchCopyN(lpString, nMaxCount, pszText + dwStart, dwEnd - dwStart);
LocalUnlock(hLocal);
if (!dwSize)
{
HeapFree(GetProcessHeap(), 0, lpTemp);
return 0;
}
hResult = StringCchCopyN(lpString, nMaxCount, lpTemp + dwStart, dwEnd - dwStart);
HeapFree(GetProcessHeap(), 0, lpTemp);
switch (hResult)
switch (hr)
{
case S_OK:
{
return dwEnd - dwStart;
}
case STRSAFE_E_INSUFFICIENT_BUFFER:
{
return nMaxCount - 1;
}
default:
{
return 0;
}
}
}