-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEGE-TextEditor.cpp.c
More file actions
139 lines (136 loc) · 3.53 KB
/
Copy pathEGE-TextEditor.cpp.c
File metadata and controls
139 lines (136 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <stdio.h>
#include <graphics.h>
#include <ege/sys_edit.h>
#include <commdlg.h>
#define MaxFileSize 1048576
int height = 32;
void DrawButton(int x, int y, int w, int h, int highlight, const char* text)
{
ege_point polyPoints1[3] =
{
{(float)x, (float)y}, {(float)x+w, (float)y}, {(float)x, (float)y+h}
};
ege_point polyPoints2[3] =
{
{(float)x+w, (float)y}, {(float)x, (float)y+h}, {(float)x+w, (float)y+h}
};
setfillcolor(WHITE);
ege_fillpoly(3, polyPoints1);
setfillcolor(GRAY);
ege_fillpoly(3, polyPoints2);
setfillcolor(highlight ? LIGHTBLUE : LIGHTGRAY);
ege_fillrect(x+w*2.0/32, y+h*2.0/32, w*28/32, h*28/32);
setcolor(BLACK);
setfont(height/2, 0, "黑体");
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextrect(x+w*2.0/32, y+h*2.0/32, w*28/32, h*28/32, text);
settextjustify(LEFT_TEXT, TOP_TEXT);
}
int main()
{
int choice, suspend = -1;
mouse_msg mouseMsg;
setcaption("untitled - Text Editor");
SetProcessDPIAware();//避免Windows缩放造成模糊
initgraph(1280, 720, INIT_RENDERMANUAL);
setbkcolor(WHITE);
setbkmode(TRANSPARENT);//默认设置为无背景字体
sys_edit editBox;
editBox.create(1);//多行文本框
editBox.move(0, height);//位置
editBox.size(1280, 720-height);//大小
editBox.setfont(height*3/4, 0, "Consolas");
editBox.setmaxlen(MaxFileSize);//最大输入长度
editBox.visible(1);//默认不可见,设为可见
while(1)
{
DrawButton(height*0, 0, height*2, height, suspend == 0, "新建");
DrawButton(height*2, 0, height*2, height, suspend == 1, "打开");
DrawButton(height*4, 0, height*2, height, suspend == 2, "保存");
DrawButton(height*6, 0, height*2, height, suspend == 3, "关闭");
choice = -1;
while(mousemsg())
{
mouseMsg = getmouse();
if(mouseMsg.y < height && mouseMsg.x < height*8)
{
suspend = mouseMsg.x / (height*2);
if(mouseMsg.is_up()) choice = suspend;
}
else
{
suspend = -1;
}
}
if(choice == 0)//新建
{
editBox.settext("");
setcaption("untitled - Text Editor");
}
else if(choice == 1 || choice == 2)//打开或保存
{
OPENFILENAME ofn = {0};
char fileName[260] = {0};
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL; //父窗口句柄
ofn.lpstrFile = fileName;
ofn.nMaxFile = sizeof(fileName);
ofn.lpstrFilter = "All Files (*.*)\0*.*\0";//设置文件过滤器
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; //标志位
if(choice == 1)//打开
{
if(GetOpenFileName(&ofn))//文件打开对话框
{
//开始读取file路径文件
FILE* file = fopen(fileName, "r");
char content[MaxFileSize] = {0};
char ch;
int i = 0;
while(1)
{
ch = fgetc(file);
if(ch == EOF) break;
if(ch == '\n')//将\n转为\r\n适配sys_edit
{
content[i] = '\r';
i++;
}
content[i] = ch;
i++;
if(i == MaxFileSize)
{
content[i-1] = 0;
break;
}
}
fclose(file);
editBox.settext(content);
//设置程序标题
char title[300] = {0};
sprintf(title, "%s - Text Editor", fileName);
setcaption(title);
}
//editBox.settext("content\r\n123456\n7\r89");
}
else if(choice == 2)//保存
{
if(GetSaveFileName(&ofn))//文件另存为对话框
{
//开始写入file路径文件
FILE* file = fopen(fileName, "w");
char content[MaxFileSize] = {0};
editBox.gettext(MaxFileSize, content);
fputs(content, file);
fclose(file);
}
}
}
else if(choice == 3)//关闭
{
break;
}
delay_ms(100);
}
closegraph();
return 0;
}