#include <stdio.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
char temp[80];
PAINTSTRUCT ps;
HDC hdc;
static int count;
switch(uMsg)
{
case WM_CREATE:
count = 0;
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
sprintf(temp, "Count = %d", count);
TextOut(hdc, 50, 50, temp, strlen(temp));
EndPaint(hWnd, &ps);
break;
case WM_LBUTTONDOWN:
count++;
InvalidateRect(hWnd,NULL,TRUE);
break;
case WM_RBUTTONDOWN:
count--;
InvalidateRect(hWnd,NULL,TRUE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
#include <stdio.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
char temp[80];
PAINTSTRUCT ps;
HDC hdc;
static int count;
static int m_bFlag;
switch(uMsg)
{
case WM_CREATE:
count = 0;
m_bFlag= 1;
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
if(m_bFlag)
strcpy(temp,"Toggle");
else
strcpy(temp,"");
TextOut(hdc, 50, 50, temp, strlen(temp));
EndPaint(hWnd, &ps);
break;
case WM_LBUTTONDOWN:
m_bFlag = !m_bFlag;
// 화면을 즉각즉각 재출력을 해주는 함수(자주쓰이는 함수)
InvalidateRect(hWnd,NULL,TRUE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
/*
Data를 Toggle하는 방법
1. 1,0 toggle
int flag = 1;
flag = 1 - flag (flag = !flag)
2. 1,-1 toggle
int flag = 1;
flag = flag*-1 (flag *= -1)
3. 0,1,2 순차적으로 반복하는 round toggle
int flag = 0;
flag++;
flag %= 3;
변수이름에 m_을 표시하는 이유
Member변수라는 뜻이다. 이는 class로 프로그램을 작성할때 주로 사용하는
Naming Rule이며, MFC프로그래밍 스타일의 습관을 그대로 따랐다.
*/
#include <stdio.h>
POINT CenterPoint(RECT& r);
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
static char m_Str[80];
static POINT m_Point;
static RECT m_Bound;
switch(uMsg)
{
case WM_CREATE:
strcpy(m_Str, "Catch me~~");
GetClientRect(hWnd, &m_Bound);
m_Point = CenterPoint(m_Bound);
// string 길이의 반만큼 옆으로 가도록 함
m_Point.x -= (strlen(m_Str)*8)/2;
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
TextOut(hdc, m_Point.x, m_Point.y, m_Str, strlen(m_Str));
EndPaint(hWnd, &ps);
break;
case WM_LBUTTONDOWN:
m_Point.x -= 20;
InvalidateRect(hWnd,NULL,TRUE);
break;
case WM_RBUTTONDOWN:
m_Point.x += 20;
InvalidateRect(hWnd,NULL,TRUE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
POINT CenterPoint(RECT& r)
{
POINT p;
p.x = (r.left + r.right)/2;
p.y = (r.top + r.bottom)/2;
return p;
}
/*
<windef.h>에 보면 다음과 같이 선언
typedef struct tagPOINT
{
LONG x;
LONG y;
} POINT;
typedef struct tagRECT
{
LONG left;
LONG top;
LONG right;
LONG bottom;
} RECT;
BOOL GetClientRect {
HWND hWnd, // handle to window
LPRECT lpRect // address of structure for client coordinates
}
*) 윈도우 타이틀바를 제외한 전체 영역을 client area 라한다. 이 client area를
갖다주는 함수가 GetClientRect 함수이다.
*) 좌표는 언제나 POINT 타입으로 표현된다.
*) GetTextMetrics() 함수는 시스템 폰트의 크기를 알수있다.
*/