VS2013 打字本 windows程序设计

// WIN32PROJECT2.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "Win32Project2.h"

#define MAX_LOADSTRING 100

// 全局变量: 
HINSTANCE hInst;								// 当前实例
TCHAR szTitle[MAX_LOADSTRING];					// 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING];			// 主窗口类名

// 此代码模块中包含的函数的前向声明: 
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
	_In_opt_ HINSTANCE hPrevInstance,
	_In_ LPTSTR    lpCmdLine,
	_In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	// TODO:  在此放置代码。
	MSG msg;
	HACCEL hAccelTable;

	// 初始化全局字符串
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_WIN32PROJECT2, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// 执行应用程序初始化: 
	if (!InitInstance(hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT2));

	// 主消息循环: 
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int)msg.wParam;
}



//
//  函数:  MyRegisterClass()
//
//  目的:  注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT2));
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32PROJECT2);
	wcex.lpszClassName = szWindowClass;
	wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   函数:  InitInstance(HINSTANCE, int)
//
//   目的:  保存实例句柄并创建主窗口
//
//   注释: 
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	WNDCLASSEX wcex;
	HWND hWnd;
	TCHAR szWindowClass[] = L"窗口示例";
	TCHAR szTitle[] = L"文本编辑器";

	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style = 0;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = szWindowClass;
	wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	if (!RegisterClassEx(&wcex))
		return FALSE;
	hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

	if (!hWnd)
	{
		return FALSE;
	}

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	return TRUE;
}

//
//  函数:  WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目的:    处理主窗口的消息。
//
//  WM_COMMAND	- 处理应用程序菜单
//  WM_PAINT	- 绘制主窗口
//  WM_DESTROY	- 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
#define MAXLINE 1000
#define MAXNUMCHAR 15
	static int numchar=15;
	static TCHAR cCharInfo[MAXLINE][MAXNUMCHAR];
	static int nNumLine = 0;
	static int nArrayPos[MAXLINE];
	static int nX = 0, nY = 0;
	static int nLnHeight = 0;
	static int nCharWidth;
	static int nXCaret = 0, nYCaret = 0;
	int x;

	PAINTSTRUCT PtStr;
	TEXTMETRIC tm;
	HDC hdc;

	switch (message)
	{
	case WM_CREATE:
	{
		hdc = GetDC(hWnd);
		GetTextMetrics(hdc, &tm);
		nLnHeight = tm.tmHeight + tm.tmExternalLeading;
		nCharWidth = tm.tmAveCharWidth;
		CreateCaret(hWnd, NULL, tm.tmAveCharWidth / 10, tm.tmHeight + tm.tmExternalLeading);
		ShowCaret(hWnd);
		ReleaseDC(hWnd, hdc);
	}
		break;
    case WM_CHAR:
	{
		if (wParam == VK_BACK)
		{
			if (nX == 0)
			{
				if (nNumLine > 0)
				{
					nY--;
					nX = nArrayPos[nY];
					nNumLine--;
				}
				else
				{
					nX = nY = nNumLine = nArrayPos[0] = 0;
					MessageBox(hWnd, L"已经到头了", NULL, MB_OK);
					break;
				}
			}
			else
			{
				nArrayPos[nY] = nArrayPos[nY] - 1;
				for (int i = nX; i < nArrayPos[nY]; i++)
				{
					cCharInfo[nY][i] = cCharInfo[nY][i - 1];
				}
				nX--;
			}
			InvalidateRect(hWnd, NULL, TRUE);
			break;
		}
		else if(wParam == VK_RETURN)
		{
			nNumLine++;
			if (nNumLine >= MAXLINE)
			{
				nY = nNumLine;
				MessageBox(hWnd, L"已超过最大行数", NULL, MB_OK);
				break;
			}
			nArrayPos[nY] = nX;
			for (int i = nNumLine; i > nY + 1; i--)
			{
				_tcscpy_s(cCharInfo[i], cCharInfo[i - 1]);
				nArrayPos[i] = nArrayPos[i - 1];
			}
			_tcscpy_s(cCharInfo[nY + 1], &cCharInfo[nY][nX]);
			cCharInfo[nY][nX] = '\0';
			nY++;
			nX = 0;
			nArrayPos[nY] = _tcsclen(cCharInfo[nY]);
		}
		else
		{
			if (nArrayPos[nY] < MAXNUMCHAR-1)
			{
				cCharInfo[nY][nArrayPos[nY] + 1] = '\0';
				for (x = nArrayPos[nY]; x > nX; x--)
				{
					cCharInfo[nY][x] = cCharInfo[nY][x - 1];
				}
				cCharInfo[nY][nX] = (TCHAR)wParam;
				nArrayPos[nY] = nArrayPos[nY] + 1;
				nX++;
			}
			else
			{
				nNumLine++;
				if (nNumLine > MAXLINE)
				{
					nY = nNumLine;
					MessageBox(hWnd, L"已超过最大行数", NULL,MB_OK);
					break;
				}
				for (int i = nNumLine; i > nY + 1; i--)
				{
					_tcscpy_s(cCharInfo[i], cCharInfo[i - 1]);
					nArrayPos[i] = nArrayPos[i - 1];
				}
				_tcscpy_s(cCharInfo[nY + 1], &cCharInfo[nY][nX]);
				cCharInfo[nY][nX] = '\0';
				nArrayPos[nY] = nX;
				nY++;
				nX = 0;
				nArrayPos[nY] = _tcsclen(cCharInfo[nY]);
			}
		}



		InvalidateRect(hWnd, NULL, TRUE);
	}
		break;

	case WM_KEYDOWN:
	{
		switch (wParam)
		{
		case VK_END:
		{
			nX = nArrayPos[nY];
		}
			break;
		case VK_HOME:
		{
			nX = 0;
		}
			break;
		case VK_DELETE:
		{
			if (nArrayPos[nY] == nX)
			{
				MessageBox(hWnd, L"本行当前位置以后已空,没有字符可供删除", NULL, MB_OK);
			}
			else
			{
				for (x = nX; x < nArrayPos[nY]; x = x + 1)
				{
					cCharInfo[nY][x] = cCharInfo[nY][x + 1];
				}
				nArrayPos[nY] = nArrayPos[nY] - 1;
			}
		}
			break;
		case VK_LEFT:
		{
			if (nX > 0)
			{
				nX = nX - 1;
			}
			else
			{
				MessageBox(hWnd, L"已经移动到起始位置,不能再左移。", NULL, MB_OK);
			}
		}
			break;
		case VK_RIGHT:
		{
			if (nArrayPos[nY]>nX)
			{
				nX++;
			}
			else
			{
				MessageBox(hWnd, L"已经移动到行末,不能再右移。", NULL, MB_OK);
			}
		}
			break;
		case VK_UP:
		{
			if (nY>0)
			{
				nY--;
				if (nX>nArrayPos[nY])
				{
					nX = nArrayPos[nY];
				}
			}
			else
			{
				MessageBox(hWnd, L"已经移动到第一行了,不能再上移。", NULL, MB_OK);
			}
			break;
		}
		case VK_DOWN:
			if (nY<MAXLINE)
			{
				nY++;
				if (nX > nArrayPos[nY])
				{
					nX = nArrayPos[nY];
				}
			}
			else
			{
				MessageBox(hWnd, L"已经到最后一行,不能再下移。", NULL, MB_OK);
			}
			break;
		}
			InvalidateRect(hWnd, NULL, TRUE);
		}
	
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &PtStr);
		// TODO:  在此添加任意绘图代码...
		for (int i = 0; i <= nNumLine; i++)
		{
			TextOut(hdc, 0, nLnHeight*i, cCharInfo[i], nArrayPos[i]);
		}
		SIZE size;
		GetTextExtentPoint(hdc, cCharInfo[nY], nX, &size);
		nXCaret = size.cx;
		nYCaret = nY*nLnHeight;
		SetCaretPos(nXCaret, nYCaret);
		EndPaint(hWnd, &PtStr);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值