#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,UINT wParam,LONG lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow){
HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
char lpszClassName[]="基本绘图";
char lpszTitle[]="四川师范大学计算机科学学院";
wndclass.style=0;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=lpszClassName;
if(!RegisterClass(&wndclass)){
MessageBeep(0);
return FALSE;
}
hwnd=CreateWindow(
lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg,NULL,0,0)){
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,UINT wParam,LONG lParam){
static long nXchar,nYchar;
int x=0,y=0;
HDC hDC;
HPEN hPen;
HBRUSH hBrush;
TEXTMETRIC tm;
PAINTSTRUCT PtStr;
static char textbuf0[]="you had hitted Up key";
static char textbuf1[]="you had hitted Shift key";
static char textbuf2[]="you had hitted Ctrl key";
static char textbuf3[]="you had hitted Shift+B key";
static char textbuf4[]="you had hitted Ctrl+A key";
//static char textbuf5[]=" ";
static bool nupdown=false;
static bool nshiftdown=false;
static bool nctrldown=false;
static bool nctrladown=false;
static bool nshiftbdown=false;
switch(message){
case WM_KEYDOWN:
switch(wParam){
case VK_UP:
nupdown=true;
break;
case VK_SHIFT:
nshiftdown=true;
break;
case VK_CONTROL:
nctrldown=true;
break;
default :
break;
}
case WM_CHAR:
if(wParam=='B'||wParam=='b'){
if(nshiftdown){
nshiftdown=false;
nshiftbdown=true;
}
}
else if(wParam=='a'||wParam=='A'){
if(nctrldown){
nctrldown=false;
nctrladown=true;
}
}
break;
case WM_KEYUP:
if(nupdown) InvalidateRect(hwnd,NULL,true);
if(nshiftdown) InvalidateRect(hwnd,NULL,true);
if(nctrldown) InvalidateRect(hwnd,NULL,true);
if(nshiftbdown) InvalidateRect(hwnd,NULL,true);
if(nctrladown) InvalidateRect(hwnd,NULL,true);
//break;
case WM_PAINT:
hDC=BeginPaint(hwnd,&PtStr);
hBrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
hPen = (HPEN)GetStockObject(WHITE_PEN);
SelectObject(hDC,hPen);
SelectObject(hDC,hBrush);
SetTextColor(hDC,RGB(255,0,0));
GetTextMetrics(hDC,&tm);
nXchar=tm.tmAveCharWidth;
nYchar=tm.tmHeight+tm.tmExternalLeading;
x=nXchar;
//Rectangle(hDC,0,0,300,200);
if(nupdown){
y=nYchar*0;
TextOut(hDC,x,y,textbuf0,lstrlen(textbuf0));
nupdown=false;
}
else if(nshiftdown){
y=nYchar*1;
TextOut(hDC,x,y,textbuf1,lstrlen(textbuf1));
nshiftdown=false;
}
else if(nctrldown){
y=nYchar*2;
TextOut(hDC,x,y,textbuf2,lstrlen(textbuf2));
nctrldown=false;
}
else if(nshiftbdown){
y=nYchar*3;
TextOut(hDC,x,y,textbuf3,lstrlen(textbuf3));
nshiftbdown=false;
}
else if(nctrladown){
y=nYchar*4;
TextOut(hDC,x,y,textbuf4,lstrlen(textbuf4));
nctrladown=false;
}
DeleteObject(hPen);
DeleteObject(hBrush);
EndPaint(hwnd,&PtStr);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}