Programming Language/Windows API

keyboard[마지막]

D4tai1 2018. 9. 10.

1. 연습장만들기

- 입력하는 글자를 출력

- Enter 시 줄바꿈

- BackSpace 시 지우기

- 라인 수 보이기

- 가로 길이, 세로 길이, 총 문자수 보이기

 

2. 소스

1) source.h

 

void my_input(HWND hWnd, HDC hdc, WPARAM wParam, TCHAR (*lpString)[100], int *nLength, int *size, int *y);
void my_paint(HDC hdc, int *nLength, int *size, int *y, TCHAR(*lpString)[100]);

 

2) source.cpp

 

#include "stdafx.h"
#include "practice2_3.h"
#include "source.h"


void my_input(HWND hWnd, HDC hdc, WPARAM wParam, TCHAR(*lpString)[100], int *nLength, int *size, int *y) {
	if (nLength[0] == -1) {
		nLength[0]++;
		(*size)++;
	}

	if (wParam == VK_RETURN && *y < 80) {
		*y += 1;
		nLength[*y]--;
	}


	if (wParam == VK_BACK && 0 <= nLength[*y]) {
		if (nLength[*y] == 0 && 0 < *y) {
			(*y)--;	//위로한칸
		}
		else if(lpString[*y][1] != VK_BACK) {
			nLength[*y]--;	//한칸빼기
		}
		(*size)--;
	}
	else {
		lpString[*y][(nLength[*y])++] = wParam;
		(*size)++;
	}
	lpString[*y][nLength[*y]] = NULL;
	
	InvalidateRgn(hWnd, NULL, TRUE);

}

void my_paint(HDC hdc, int *nLength, int *size, int *y, TCHAR(*lpString)[100]) {
	TCHAR tmp[40];
	TCHAR set_nu[10];
	_stprintf_s(tmp, _T("가로 : %d자,  세로 : %d줄,  전체 : %d자"), nLength[*y], *y + 1, *size);
	
	TextOut(hdc, 0, 0, tmp, _tcslen(tmp));
	for (int i = 0; i <= (*y); i++) {
		_stprintf_s(set_nu, _T("%2d) "), i + 1);
		TextOut(hdc, 0, i * 20 + 20, set_nu, _tcslen(set_nu));
		TextOut(hdc, 20, i * 20 + 20, lpString[i], nLength[i]);
	}
	
}

 

3) practice2_3.cpp

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

		static TCHAR str[100][100];
		static int count[10];
		static int ypos;
		static int size;

	case WM_CREATE:
	{
		int i = 0;
		while (i < 100) {
			count[i++] = 0;
		}
		size = 0;
		ypos = 0;
	}
	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_CHAR:
	{
		hdc = GetDC(hWnd);
		my_input(hWnd, hdc, wParam, str, count, &size, &ypos);
		ReleaseDC(hWnd, hdc);
	}
	break;
    case WM_PAINT:
        {
            hdc = BeginPaint(hWnd, &ps);
            // TODO: 여기에 hdc를 사용하는 그리기 코드를 추가합니다.
			my_paint(hdc, count, &size, &ypos, str);
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

 

 

3. 실행화면

 

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

도형 자동이동  (0) 2018.09.30
마우스로 도형 옮기기  (0) 2018.09.30
shape  (0) 2018.08.27
caret  (0) 2018.08.27
keyboard[6]  (0) 2018.08.27

댓글