다른 명령
- Windows 10에서 Caret 위치 텍스트 출력 프로그램
다음은 Windows 10에서 캐럿(커서) 위치의 텍스트를 출력하는 C++과 AutoIt 프로그램 예제입니다.
- C++ 버전
```cpp
- include <windows.h>
- include <iostream>
- include <string>
std::string GetTextAroundCaret(HWND hWnd) {
DWORD startPos, endPos;
CHAR buffer[1024] = {0};
// 현재 포커스가 있는 윈도우 핸들 가져오기
if (hWnd == NULL) {
hWnd = GetFocus();
}
// Edit 컨트롤인지 확인
char className[256];
GetClassNameA(hWnd, className, sizeof(className));
if (strcmp(className, "Edit") != 0) {
return "Not an Edit control";
}
// 현재 선택 영역 가져오기
SendMessageA(hWnd, EM_GETSEL, (WPARAM)&startPos, (LPARAM)&endPos);
// 텍스트 길이 가져오기
int textLength = GetWindowTextLengthA(hWnd);
// 전체 텍스트 가져오기
GetWindowTextA(hWnd, buffer, textLength + 1);
// 캐럿 주변 텍스트 추출 (예: 캐럿 앞 10자, 뒤 10자)
int start = max(0, (int)startPos - 10);
int end = min(textLength, (int)startPos + 10);
return std::string(buffer + start, buffer + end);
}
int main() {
std::cout << "Caret 위치 텍스트를 가져오는 프로그램 (10초간 실행)" << std::endl;
for (int i = 0; i < 10; i++) {
HWND foregroundWindow = GetForegroundWindow();
HWND focusWindow = GetFocus();
std::string text = GetTextAroundCaret(focusWindow);
std::cout << "Caret 주변 텍스트: " << text << std::endl;
Sleep(1000); // 1초 대기
}
return 0;
} ```
- AutoIt 버전
```autoit
- include <WinAPI.au3>
- include <EditConstants.au3>
Func GetCaretText()
Local $hWnd = _WinAPI_GetFocus()
If $hWnd = 0 Then Return "포커스된 창이 없습니다."
; 클래스 이름 확인
Local $sClass = _WinAPI_GetClassName($hWnd)
If $sClass <> "Edit" Then Return "Edit 컨트롤이 아닙니다: " & $sClass
; 현재 선택 위치 가져오기
Local $iStart = _SendMessage($hWnd, $EM_GETSEL)
Local $iEnd = BitShift($iStart, 16)
$iStart = BitAND($iStart, 0xFFFF)
; 전체 텍스트 가져오기
Local $sText = ControlGetText("[ACTIVE]", "", $hWnd)
If @error Then Return "텍스트를 가져올 수 없습니다."
; 캐럿 주변 텍스트 추출 (앞 10자, 뒤 10자)
Local $iTextLen = StringLen($sText)
Local $iFrom = Max(0, $iStart - 10)
Local $iTo = Min($iTextLen, $iStart + 10)
Return StringMid($sText, $iFrom + 1, $iTo - $iFrom)
EndFunc
- 메인 프로그램
ToolTip("캐럿 위치 텍스트를 가져오는 AutoIt 프로그램 (10초간 실행)", 0, 0)
For $i = 1 To 10
Local $sText = GetCaretText()
ConsoleWrite("캐럿 주변 텍스트: " & $sText & @CRLF)
Sleep(1000)
Next
Exit ```
- 사용 방법
1. **C++ 버전**:
- Visual Studio 등으로 컴파일 후 실행 - 10초 동안 1초 간격으로 포커스된 Edit 컨트롤의 캐럿 위치 주변 텍스트 출력
2. **AutoIt 버전**:
- AutoIt 스크립트로 저장 후 실행 - 10초 동안 1초 간격으로 콘솔에 캐럿 위치 주변 텍스트 출력
- 주의 사항
- 이 프로그램은 표준 Edit 컨트롤에서만 작동합니다. - RichEdit, Chrome, 워드프로세서 등 다른 컨트롤에서는 작동하지 않을 수 있습니다. - 관리자 권한이 필요한 경우도 있을 수 있습니다.