Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

codingfarm

5. 리소스 - 문자열 테이블(string table) 본문

Windows/윈도우즈 API

5. 리소스 - 문자열 테이블(string table)

scarecrow1992 2021. 5. 1. 23:42

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <windows.h>
#include "resource.h"
 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hInst;
LPCTSTR lpszClass = TEXT("StrTable");
 
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance
    , LPSTR lpszCmdParam, int nCmdShow)
{
    HWND hWnd;
    MSG Message;
    WNDCLASS WndClass;
 
    g_hInst = hInstance;
 
    WndClass.cbClsExtra = 0;
    WndClass.cbWndExtra = 0;
    WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    WndClass.hInstance = hInstance;
    WndClass.lpfnWndProc = (WNDPROC)WndProc;
    WndClass.lpszClassName = lpszClass;
    WndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
    WndClass.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&WndClass);
 
    hWnd = CreateWindow(lpszClass, lpszClass, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, (HMENU)NULL, hInstance, NULL);
    ShowWindow(hWnd, nCmdShow);
 
    while (GetMessage(&Message, NULL00)) {
        TranslateMessage(&Message);
        DispatchMessage(&Message);
 
    }
    return Message.wParam;
}
 
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    TCHAR str[256];
    switch (iMessage) {    
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        LoadString(g_hInst, IDS_STRING1, str, 256);
        TextOut(hdc, 1010, str, lstrlen(str));
        EndPaint(hWnd, &ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}
cs

리소스 ID를 인식하기 위해 resource.h를 포함시켰으며 WM_PAINT에서 문자열 리소스를 읽어와 화면으로 출력하였다.

문자열 리소스를 읽을 때는 다음 함수를 사용한다.

int LoadString( HINSTANCE hInstance, UINT uID, LPTSTR lpBuffer, int nBufferMax );
  • hInstance : 문자열 리소스를 가진 인스턴스 핸들
  • uID : 읽어올 문자열의 ID
  • lpBuffer : 문자열을 읽을 버퍼
  • nBufferMax : 버퍼의 길이

 

 

문자열 테이블을 쓰는 이유

  1. 문자열 자체가 코드와 분리되어 문자열만 따로 관리할 수 있으며 프로젝트를 유지하는데 큰 도움을 준다.
  2. 각 언어에 대한 리소스를 분리하여, 다국어 버전을 쉽게 만들 수 있다.
  3. 문자열이 소스와 분리되어, 문자열을 고쳐도 소스를 다시 컴파일할 필요가 없다.

 

 

 

 

 

 

 

'Windows > 윈도우즈 API' 카테고리의 다른 글

6. 그래픽 - 그리기 모드  (0) 2021.05.05
6. 그래픽 - DC의 정보 수정  (0) 2021.05.04
5. 리소스 - 액셀러레이터(Accelerator)  (0) 2021.05.01
5. 리소스 - 메뉴  (0) 2021.05.01
5. 리소스  (0) 2021.05.01
Comments