/
RichTextEditorForm.cs
170 lines (152 loc) · 6.04 KB
/
RichTextEditorForm.cs
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraRichEdit;
using zhuhai.service;
using zhuhai.model;
using System.IO;
using zhuhai.util;
namespace zhuhai
{
public partial class RichTextEditorForm : DevExpress.XtraEditors.XtraForm
{
private int id = 0;
public int Id
{
get { return id; }
set { id = value; }
}
private Boolean isReadOnly = false;
private IOperateService<CommonText> operateService;
public RichTextEditorForm(IOperateService<CommonText> operateService)
{
InitializeComponent();
this.operateService = operateService;
}
/// <summary>
/// 修改或者查看
/// </summary>
/// <param name="id"></param>
/// <param name="title"></param>
/// <param name="operateService"></param>
/// <param name="isReadOnly">true表示查看,默认是修改</param>
public RichTextEditorForm(int id,string title, IOperateService<CommonText> operateService, Boolean isReadOnly = false)
{
InitializeComponent();
this.operateService = operateService;
Id = id;
//根据id获取内容
CommonText ct = operateService.getRow(id, title);
//如果是查看
if (isReadOnly == true)
{
richEditControl_context.ReadOnly = true;
textEdit_title.Properties.ReadOnly = true;
this.Text = "查看";
this.simpleButton_save.Hide();
this.isReadOnly = isReadOnly;
this.simpleButton_cancel.Text = "关闭";
}
textEdit_title.Text = ct.Title;
//加载内容
richEditControl_context.Document.LoadDocument(StreamByteTransfer.BytesToStream(operateService.getRow(id, title).Bytes), DocumentFormat.Rtf);
}
private void simpleButton_cancel_Click(object sender, EventArgs e)
{
//编辑或者新增的时候
if (isReadOnly == false)
{
if (MessageBox.Show("确定要退出吗?你还没有保存标题和内容呢?", "退出系统", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
this.Close();
}
}
//查看
else
{
this.Close();
}
}
private void simpleButton_save_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(textEdit_title.Text))
{
MessageBox.Show("标题不能为空!", "提示");
return;
}
if (operateService.findRowByIdAndTitle(id, textEdit_title.Text))
{
MessageBox.Show("标题不能重复!", "提示");
return;
}
CommonText ct = new CommonText();
ct.Title = textEdit_title.Text;
//保存到一个临时文件中,AppData\Local\Temp
string filePath = System.IO.Path.GetTempPath() + "/zxt.rtf";
richEditControl_context.Document.SaveDocument(filePath, DocumentFormat.Rtf);
ct.Bytes = StreamByteTransfer.FileToBytes(filePath);
//超过规定的大小后提示用户无法保存
if (ct.Bytes.LongLength > 16000 * 1000)
{
MessageBox.Show("图片太多, 请删除一些图片然后在保存(默认大小是16M)!", "错误");
return;
}
ct.Id = Id;
//保存
if (id == 0 && true == operateService.addRow(ct))
{
MessageBox.Show("保存成功!", "提示");
if (operateService is DisposePlanService)
{
LogService.getInstance().log("增加,标题为" + ct.Title, ModuleConstant.DisposePlan_MODULE);
}
else if (operateService is EpidemicInfoService) {
LogService.getInstance().log("增加,标题为" + ct.Title, ModuleConstant.EpidemicInfo_MODULE);
}
else if (operateService is JobGuideBookService) {
LogService.getInstance().log("增加,标题为" + ct.Title, ModuleConstant.JobGuideBook_MODULE);
}
else if (operateService is WorkRuleService)
{
LogService.getInstance().log("增加,标题为" + ct.Title, ModuleConstant.WorkRule_MODULE);
}
this.Close();
return ;
}
else if (id != 0 && true == operateService.modifyRow(ct))
{
MessageBox.Show("保存成功!", "提示");
if (operateService is DisposePlanService)
{
LogService.getInstance().log("修改,标题为" + ct.Title, ModuleConstant.DisposePlan_MODULE);
}
else if (operateService is EpidemicInfoService)
{
LogService.getInstance().log("修改,标题为" + ct.Title, ModuleConstant.EpidemicInfo_MODULE);
}
else if (operateService is JobGuideBookService)
{
LogService.getInstance().log("修改,标题为" + ct.Title, ModuleConstant.JobGuideBook_MODULE);
}
else if (operateService is WorkRuleService)
{
LogService.getInstance().log("修改,标题为" + ct.Title, ModuleConstant.WorkRule_MODULE);
}
this.Close();
}
else
{
MessageBox.Show("保存失败!", "提示");
}
}
private void richEditControl_context_Click(object sender, EventArgs e)
{
}
}
}