Programming Language/Windows API

keyboard[5]

D4tai1 2018. 8. 27.

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

 

1) my.h 소스

void my_input(HWND hWnd, WPARAM wParam, LPTSTR lpString, int *cnt);
void my_paint(HDC hdc, TCHAR *lpString, int *cnt);

 

 

2) my.cpp 소스

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

void my_input(HWND hWnd, WPARAM wParam, LPTSTR lpString, int *cnt) {
	if (wParam == VK_BACK && *cnt >= 0) {	
	//삭제가능한 문자가 존재할 때 backspace가 입력됬다면
		--(*cnt);
	}
	else {
		lpString[(*cnt)++] = wParam;
	}	

	lpString[*cnt] = NULL;
	InvalidateRgn(hWnd, NULL, TRUE);
}

void my_paint(HDC hdc, TCHAR *lpString, int *cnt) {
	TCHAR value[20];

	TextOut(hdc, 0, 0, lpString, _tcslen(lpString));

	
	_stprintf_s(value, _T("length : %d"), *cnt);
	TextOut(hdc, 0, 20, value, _tcslen(value));
}

 

+) 소스설명

 [1] if(wParam == VK_BACK && *cnt > 0)

  - 입력된 키가 backspace이고 1자 이상 입력됬다면, cnt 감소 후 그 곳에 NULL

 

 

 

 

 

3) keyboard[5].cpp 소스

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

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

	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_paint(hdc, str, &count);

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

 

 

4) 문제점

 [1]  backspace시 삭제되는건 개선했지만 Enter시 개행되지 않는다.

 

[실행화면]

 

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

caret  (0) 2018.08.27
keyboard[6]  (0) 2018.08.27
keyboard[4]  (0) 2018.08.27
keyboard[3]  (0) 2018.08.27
keyboard[2]  (0) 2018.08.27

댓글