简单的编辑器

#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);



	}

}

  

你可能感兴趣的:(编辑器)