博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单的编辑器
阅读量:6900 次
发布时间:2019-06-27

本文共 3532 字,大约阅读时间需要 11 分钟。

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

  

posted on
2015-01-19 20:23 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/devinblog/p/4234700.html

你可能感兴趣的文章
模拟web高并发
查看>>
解决数据重复导致查询出错问题
查看>>
springmvc(4)注解简单了解
查看>>
HTMLCSS学习笔记(四)----浮动原理及清浮动
查看>>
探究如何求两数的最大公约数(两种方法)
查看>>
Better Django models
查看>>
第 8 章 容器网络 - 054 - 准备 macvlan 环境
查看>>
第 10 章 容器监控 - 081 - Weave Scope 多主机监控
查看>>
FLASK爬坑笔记
查看>>
[haoi2008]圆上的整点
查看>>
node.js常用方法
查看>>
iscroll手册
查看>>
5.Struts2框架中的ServletAPI如何获取
查看>>
java学习路线
查看>>
layui-学习01-基本api
查看>>
Angular 表单验证 基础篇
查看>>
大话设计模式第十三章---建造者模式
查看>>
Google Chrome浏览器的使用方法
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource
查看>>
『左偏树 Leftist Tree』
查看>>