#include <windows.h>
#include <stdlib.h>
#include <string.h>
void WINAPI CaretPos(int *xCaret,int *yCaret);
long WINAPI WndProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);
HWND hWndMain;
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
MSG Message;
if(!InitWindowsClass(hInstance))
return FALSE;
if(!InitWindows(hInstance,nCmdShow))
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd, UINT iMessage, UINT wParam,LONG lParam)
{
#define BufSize 30
static char cCharBuf[BufSize];
static int nNumChar = 0;
static int nArrayPos = 0;
static int nLnHeight;
static int nCharWidth;
static int xCaret,yCaret;
int x;
HDC hDC;
TEXTMETRIC tm;
PAINTSTRUCT PtStr;
switch(iMessage)
{
case WM_CHAR:
{
if (wParam == VK_BACK)
{
if (nArrayPos == 0)
MessageBox(hWnd,"当前位置是文本起始位置,不能回退!",NULL,MB_OK);
else
{
nArrayPos -= 1;
CaretPos(&xCaret,&yCaret);
nNumChar -= 1;
InvalidateRect(hWnd,NULL,TRUE);
}
break;
}
if (wParam == VK_ESCAPE)
{
MessageBox(hWnd,"你不能按ESC,请继续其他操作!",NULL,MB_OK);
break;
}
if (nNumChar >= BufSize)
{
MessageBox(hWnd,"缓冲区已满,不能再输入",NULL,MB_OK);
break;
}
for (x = nNumChar ; x>nArrayPos; x -= 1)
cCharBuf[x] = cCharBuf[x-1];
cCharBuf[nArrayPos] = (unsigned char)wParam;
nArrayPos ++;
nNumChar ++;
InvalidateRect(hWnd,NULL,TRUE);
}
break;
case WM_CREATE:
{
hDC = GetDC(hWnd);
GetTextMetrics(hDC,&tm);
nLnHeight=tm.tmHeight + tm.tmExternalLeading;
nCharWidth = tm.tmAveCharWidth;
ReleaseDC(hWnd,hDC);
}
break;
case WM_SETFOCUS:
{
CreateCaret(hWnd,0,0,nLnHeight);
CaretPos(&xCaret,&yCaret);
ShowCaret(hWnd);
}
break;
case WM_KILLFOCUS:
DestroyCaret();
break;
case WM_KEYDOWN:
{
switch(wParam)
{
case VK_END:
nArrayPos = nNumChar;
CaretPos(&xCaret,&yCaret);
break;
case VK_HOME:
nArrayPos = 0;
CaretPos(&xCaret,&yCaret);
break;
case VK_DELETE:
if (nArrayPos==nNumChar)
MessageBox(hWnd,"缓冲区已空,没有字符可删除",NULL,MB_OK);
else
{
for (x=nArrayPos; x<nNumChar; x++)
cCharBuf[x] = cCharBuf[x+1];
nNumChar -= 1;
InvalidateRect(hWnd,NULL,TRUE);
}
break;
case VK_LEFT:
if (nArrayPos > 0)
{ nArrayPos -= 1;
CaretPos(&xCaret,&yCaret);}
else
MessageBox(hWnd,"已到起始位置",NULL,MB_OK);
break;
case VK_RIGHT:
if (nArrayPos < nNumChar)
nArrayPos ++;
else
MessageBox(hWnd,"已到末尾位置",NULL,MB_OK);
break;
}
}
break;
case WM_PAINT:
hDC = BeginPaint(hWnd, &PtStr);
TextOut(hDC,nCharWidth,nLnHeight,cCharBuf,nNumChar);
EndPaint(hWnd,&PtStr);
break;
case WM_DESTROY:
PostQuitMessage(0);break;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
return 0;
}
void WINAPI CaretPos(int *xCaret,int *yCaret)
{
SetCaretPos(*xCaret,*yCaret);
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{ WNDCLASS WndClass;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
WndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = hInstance;
WndClass.lpfnWndProc = WndProc;
WndClass.lpszClassName = "WinText";
WndClass.lpszMenuName = NULL;
WndClass.style = CS_HREDRAW|CS_VREDRAW;
return RegisterClass( &WndClass); }
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{
HWND hWnd;
hWnd = CreateWindow("WinText",
"文本显示示例程序",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if(!hWnd)
return FALSE;
hWndMain = hWnd;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}