Programming Language/Windows API

keyboard[6]

D4tai1 2018. 8. 27.

1. 키보드 입력 시 문자열 입력 후 출력

 

1) my.h 소스

void my_rect(RECT *rt);
void my_input(HWND hWnd, WPARAM wParam, LPTSTR lpString, int *cnt);
void my_draw(HDC hdc, LPTSTR lpString, RECT *rt, int *cnt);

 

 

2) my.cpp 소스

#include "stdafx.h"
#include "keyboard[6].h"
#include "my.h"

void my_rect(RECT *rt) {
	rt->left = 0;
	rt->top = 20;
	rt->right = 1000;
	rt->bottom = 1000;
}

void my_input(HWND hWnd, WPARAM wParam, LPTSTR lpString, int *cnt) {
	if (wParam == VK_BACK && *cnt > 0) {
		(*cnt)--;
	}
	else {
		lpString[(*cnt)++] = wParam;
	}
	
	lpString[*cnt] = NULL;

	InvalidateRgn(hWnd, NULL, TRUE);
}

void my_draw(HDC hdc, LPTSTR lpString, RECT *rt, int *cnt) {
	TCHAR length[20];

	_stprintf_s(length, _T("length = %d"), *cnt);
	TextOut(hdc, 0, 0, length, _tcslen(length));
	DrawText(hdc, lpString, _tcslen(lpString), rt, DT_TOP | DT_LEFT);
}

 

+) 소스설명

 [1] void my_rect(RECT *rt)

  - RECT 구조체 타입인 객체의 주소 rt를 가져와서 (좌, 상), (우, 하) 좌표를 입력한다.

 [2] DrawText(hdc, lpString, _tsclen(lpString), rt, DT_TOP | DT_LEFT)

  - 출력영역과 출력할 문자열, 출력할 문자열의 길이, 그릴영역의 주소, 출력방식(좌측 상단부터)에 맞춰 출력한다.

 

 

 

 

 

3) keyboard[6].cpp 소스

#include "stdafx.h"
#include "keyboard[6].h"
#include "my.h"

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;

	RECT rect;// = { 0, 20 ,1000, 1000 };

	static TCHAR str[100];
	static int count;

    switch (message)
    {
	case WM_CREATE:
	{
		count = 0;
	}
	break;
	
	case WM_CHAR:
	{
		hdc = GetDC(hWnd);

		my_input(hWnd, wParam, str, &count);

		ReleaseDC(hWnd, hdc);
	}
	break;

    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 메뉴 선택을 구문 분석합니다.
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
			hdc = BeginPaint(hWnd, &ps);
			
			my_rect(&rect);
			my_draw(hdc, str, &rect, &count);

            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

 

 

4) 문제점

 [1]  앞의 문제점은 개선되었지만 커서가 없어서 현재 위치를 알 수 없다.

 

[실행화면]

 

'Programming Language > Windows API' 카테고리의 다른 글

shape  (0) 2018.08.27
caret  (0) 2018.08.27
keyboard[5]  (0) 2018.08.27
keyboard[4]  (0) 2018.08.27
keyboard[3]  (0) 2018.08.27

댓글