[API 탐방]


윈도우의 캡션을 설정하거나 가져오는

SetWindowText, GetWindowText




자, 먼저 SetWindowText API 함수와, GetWindowText API 함수의 원형부터 살펴보도록 합시다.

BOOL SetWindowText(HWND hWnd, LPCTSTR lpString);
int GetWindowText(HWND hWnd, LPTSTR lpString, int nMaxCount);

제일 처음 SetWindowText의 원형 부터 살펴보자면, 첫번째 인수로는 윈도우 또는 컨트롤의 핸들이 옵니다. 그러나, 다른 응용 프로그램에서 컨트롤의 캡션은 변경할 수 없습니다. 두번째 인수로는 바꿀 문자열을 말합니다. 반환값으로는 실패하면 0이 오며, 성공하면 0이 아닌값이 옵니다.


두번째로 GetWindowText의 원형을 살펴보면, 첫번째 인수로는 윈도우 또는 컨트롤의 핸들이 오며, 두번째 인수로는 윈도우의 캡션을 받을 버퍼가 위치합니다. 세번째 인수로는 버퍼의 크기를 말합니다. 반환값으로는 실패하면 0을 반환하며, 성공하면 해당 윈도우의 캡션의 길이를 반환합니다.


이제 한번, 메모장을 켜보고 메모장의 캡션을 얻어오고 SetWindowText API로 캡션을 수정한 뒤에, 다시 캡션을 얻어와보도록 하겠습니다.



위 사진은 메모장의 캡션이 변경되기 이전의 사진입니다.


Win32 API:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	TCHAR buffer[256];
	HWND hWndTitle = FindWindow(L"notepad", NULL);
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		GetWindowText(hWndTitle, buffer, 255);
		TextOut(hdc, 0, 0, buffer, lstrlen(buffer));
		SetWindowText(hWndTitle, L"이것은 메모장!");
		GetWindowText(hWndTitle, buffer, 255);
		TextOut(hdc, 0, 20, buffer, lstrlen(buffer));
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

C:

#include <stdio.h>
#include <windows.h>

int main()
{
	TCHAR buffer[256];
	HWND hWnd = FindWindow("notepad", NULL);

	GetWindowText(hWnd, buffer, sizeof(buffer));
	printf("바뀌기 전: %s\n", buffer);
	SetWindowText(hWnd, "이것은 메모장!");
	GetWindowText(hWnd, buffer, sizeof(buffer));
	printf("바뀌기 후: %s\n", buffer);
	return 0;
}


<Win32 API>


<C>


메모장을 킨 상태로, 프로그램을 컴파일 하여 실행시켰습니다. 첫번째 줄에 뜨는 문자열은 바뀌기 이전의 메모장의 캡션입니다. 그리고 두번째 줄에 뜨는 것은 바뀐 후의 메모장의 캡션입니다.