码迷,mamicode.com
首页 > 其他好文 > 详细

简单的编辑器

时间:2015-01-19 22:13:54      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

#include<Windows.h>

	LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR lpCmdLine,int nShowCmd)
{
	static TCHAR szAPPName[] = TEXT("MyWindows");
	HWND hwnd;
	MSG msg;
	WNDCLASS wndclass;

	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	wndclass.hInstance = hInstance;
	wndclass.lpfnWndProc = WndProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszClassName = szAPPName;
	wndclass.lpszMenuName = NULL;

	if(!RegisterClass(&wndclass))
	{
		MessageBox(NULL,TEXT("这个程序需要在 windows NT 下菜能执行!"),TEXT("错误"),MB_OK | MB_ICONERROR);
		return 0;
	}

	hwnd = CreateWindow(szAPPName,TEXT("简单的文本编辑器"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

	ShowWindow(hwnd,nShowCmd);

	UpdateWindow(hwnd);

	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	static RECT rect;
	static int wordWidth,wordHeight;
	TEXTMETRIC tm;
	static TCHAR *p = NULL;
	static int curx = 0,cury = 0;
	switch(message)
	{
	case WM_CREATE:
		hdc = GetDC(hwnd);
		SelectObject(hdc,CreateFont(0,0,0,0,0,0,0,0,0,0,0,0,FIXED_PITCH,NULL));
		GetClientRect(hwnd,&rect);
		GetTextMetrics(hdc,&tm);
		wordWidth = tm.tmAveCharWidth;
		wordHeight = tm.tmHeight;
		rect.left = 10;
		rect.top = 30;
		rect.right = rect.left + 50 * wordWidth;
		rect.bottom = rect.top + 10 * wordHeight;

		if(p)
			free(p);
		else
		{
			p = (TCHAR *)malloc(50 * 10 * sizeof(CHAR));
		}
		ReleaseDC(hwnd,hdc);
		return 0;

	case WM_PAINT:
		hdc = BeginPaint(hwnd,&ps);
		SelectObject(hdc,CreateFont(0,0,0,0,0,0,0,0,0,0,0,0,FIXED_PITCH,NULL));
		TextOut(hdc,10,10,TEXT("简单的文本编辑器"),lstrlen(TEXT("简单的文本编辑器")));
		SelectObject(hdc,CreatePen(PS_SOLID,2,RGB(255,0,0)));
		Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);
		EndPaint(hwnd,&ps);
		return 0;
	case WM_KEYDOWN:
		if(wparam == VK_BACK)
		{
			if(curx == 0 && cury == 0)
				return 0;
			if(curx <= 0)
			{
				curx = 50 - 1;
				if(cury > 0)
					cury--;
			}
			else
				curx--;
		}
		HideCaret(hwnd);
		hdc = GetDC(hwnd);
		p[cury * 50 + curx] = (TCHAR)‘ ‘;
		SetCaretPos(rect.left + curx * wordWidth,rect.top + cury * wordHeight);	
		SelectObject(hdc,CreateFont(0,0,0,0,0,0,0,0,0,0,0,0,FIXED_PITCH,NULL));
		TextOut(hdc,rect.left + curx * wordWidth,rect.top + cury * wordHeight,&p[cury * 50 + curx],1);
		ShowCaret(hwnd);
		ReleaseDC(hwnd,hdc);
		return 0;
	case WM_SETFOCUS:
		CreateCaret(hwnd,NULL,wordWidth / 8,wordHeight);
		SetCaretPos(rect.left,rect.top);
		ShowCaret(hwnd);

		return 0;
	case WM_KILLFOCUS:
		HideCaret(hwnd);
		DestroyCaret();
		return 0;
	case WM_CHAR:
		if(wparam == 8)
			return 0;
		if(cury >= 10)
			return 0;
		HideCaret(hwnd);
		p[cury * 50 + curx] = (CHAR)wparam;
		hdc = GetDC(hwnd);
		SelectObject(hdc,CreateFont(0,0,0,0,0,0,0,0,0,0,0,0,FIXED_PITCH,NULL));
		TextOut(hdc,rect.left + curx * wordWidth,rect.top + cury * wordHeight,&p[cury*50+curx],1);
		curx++;
		if(curx >= 50)
		{
			curx = 0;
			cury++;
		}
		SetCaretPos(rect.left + curx * wordWidth,rect.top + cury * wordHeight);
		ShowCaret(hwnd);

		ReleaseDC(hwnd,hdc);
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;


	default:
		return DefWindowProc(hwnd,message,wparam,lparam);

	}
}

  

简单的编辑器

标签:

原文地址:http://www.cnblogs.com/devinblog/p/4234700.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!