-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionFile.cs
More file actions
275 lines (246 loc) · 8.93 KB
/
FunctionFile.cs
File metadata and controls
275 lines (246 loc) · 8.93 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace CopyLine
{
// INIファイルを読み書きするためのクラス
class IniFile
{
private string filePath;
[DllImport("kernel32", CharSet = CharSet.Auto)]
private static extern int GetPrivateProfileString(
string section,
string key,
string defaultValue,
StringBuilder retVal,
int size,
string filePath
);
[DllImport("kernel32", CharSet = CharSet.Auto)]
private static extern bool WritePrivateProfileString(
string section,
string key,
string value,
string filePath
);
public IniFile(string path)
{
filePath = path;
// ファイルが存在しない場合は作成
if (!File.Exists(filePath))
{
File.WriteAllText(filePath, ""); // 空のファイルを作成
}
}
public string Read(string section, string key, string defaultValue = "")
{
var retVal = new StringBuilder(256);
GetPrivateProfileString(
section, key, defaultValue, retVal, retVal.Capacity, filePath
);
return retVal.ToString();
}
public bool Write(string section, string key, string value)
{
return WritePrivateProfileString(section, key, value, filePath);
}
}
partial class CopyLine : Form
{
////////////////////////////////////////
// 設定ファイルの書き込み、読み込み
////////////////////////////////////////
// 設定ファイルに書き込み
private void SaveValuesINI()
{
IniFile ini = new IniFile(iniFilePath);
ini.Write("Settings", "WaitTimeAfterPaste",
WaitTimeAfterPaste.ToString());
ini.Write("Settings", "QueueBackColor1", QueueBackColor[0]);
ini.Write("Settings", "QueueBackColor2", QueueBackColor[1]);
ini.Write("Settings", "ClipboardBackColorON", ClipboardBackColorON);
ini.Write("Settings", "ClipboardBackColorOFF", ClipboardBackColorOFF);
ini.Write("Settings", "TextFont", TextFont);
}
private void GetValuesINI()
{
if (!File.Exists(iniFilePath))
{
// INIファイルがない場合には作成して初期設定を書き込む
SaveValuesINI();
}
else
{
// INIファイルから読み込む
IniFile ini = new IniFile(iniFilePath);
WaitTimeAfterPaste =
Convert.ToInt32(ini.Read("Settings", "WaitTimeAfterPaste"));
QueueBackColor[0] = ini.Read("Settings", "QueueBackColor1");
QueueBackColor[1] = ini.Read("Settings", "QueueBackColor2");
ClipboardBackColorON = ini.Read("Settings", "ClipboardBackColorON");
ClipboardBackColorOFF = ini.Read("Settings", "ClipboardBackColorOFF");
TextFont = ini.Read("Settings", "TextFont");
}
}
public string ConvertColorNameToHex(string colorName)
{
Color color = Color.FromName(colorName);
return ColorTranslator.ToHtml(color);
}
////////////////////////////////////////
// ダイアログからテキストファイルを選択する
////////////////////////////////////////
private void HandlerOpenFile(Object sender, EventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
op.Title = "開くファイルを選択してください";
op.Filter = "txtファイル|*.txt|Markdownファイル|*.md|すべてのファイル|*.*";
if (op.ShowDialog() == DialogResult.OK)
{
OpenFile(op.FileName);
}
}
private void OpenFile(string path)
{
try
{
string content = FileConvertToUtf8(path);
string newText = ConvertToCRLF(content);
textBoxInput.Text = newText;
}
catch (Exception ex)
{
MessageBox.Show($"ファイルを開くことができません\n{ex.Message}");
}
}
// ファイルがドラッグされたときの処理
private void TextBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
}
// ファイルがドロップされたときの処理
private void TextBox_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files.Length == 0)
{
return;
}
try
{
string content = FileConvertToUtf8(files[0]);
string newText = ConvertToCRLF(content);
textBoxInput.Text = newText;
}
catch (Exception ex)
{
MessageBox.Show($"ファイルを開くことができません\n{ex.Message}");
}
}
// ファイルを開き、Shift-JIS であれば utf-8 に変換する
private string FileConvertToUtf8(string filePath)
{
Encoding encoding = DetectEncoding(filePath);
if (encoding == Encoding.GetEncoding("Shift_JIS"))
{
string context = File.ReadAllText(filePath, Encoding.Default);
byte[] shiftJisBytes = Encoding.GetEncoding("Shift_JIS").GetBytes(context);
// CP932(Shift_JIS)から utf-8 へ変換
byte[] utf8Bytes =
Encoding.Convert(Encoding.GetEncoding("Shift_JIS"),
Encoding.UTF8, shiftJisBytes);
// バイト配列を utf-8 の文字列に変換
return Encoding.UTF8.GetString(utf8Bytes);
}
else
{
string context = File.ReadAllText(filePath);
return context;
}
}
// 改行コードを CRLF に変換する
private string ConvertToCRLF(string content)
{
string newText = content
.Replace("\r\n", "\n")
.Replace("\r", "\n")
.Replace("\n", "\r\n");
return newText;
}
static Encoding DetectEncoding(string filePath)
{
byte[] bytes = File.ReadAllBytes(filePath);
// utf-8 の場合、BOM付きかどうかを確認
if (bytes.Length >= 3 &&
bytes[0] == 0xEF &&
bytes[1] == 0xBB &&
bytes[2] == 0xBF)
{
return Encoding.UTF8; // BOM付き utf-8
}
// utf-8 のバイトパターンをチェック
bool isUtf8 = true;
int i = 0;
while (i < bytes.Length)
{
byte b = bytes[i];
if ((b & 0x80) == 0x00)
{
// ASCII文字(1バイト)
i++;
}
else if ((b & 0xE0) == 0xC0)
{
// 2バイト utf-8
if (i + 1 >= bytes.Length || (bytes[i + 1] & 0xC0) != 0x80)
{
isUtf8 = false;
break;
}
i += 2;
}
else if ((b & 0xF0) == 0xE0)
{
// 3バイト utf-8
if (i + 2 >= bytes.Length ||
(bytes[i + 1] & 0xC0) != 0x80 ||
(bytes[i + 2] & 0xC0) != 0x80)
{
isUtf8 = false;
break;
}
i += 3;
}
else if ((b & 0xF8) == 0xF0)
{
// 4バイト utf-8
if (i + 3 >= bytes.Length ||
(bytes[i + 1] & 0xC0) != 0x80 ||
(bytes[i + 2] & 0xC0) != 0x80 ||
(bytes[i + 3] & 0xC0) != 0x80)
{
isUtf8 = false;
break;
}
i += 4;
}
else
{
isUtf8 = false;
break;
}
}
if (isUtf8)
{
return Encoding.UTF8;
}
return Encoding.GetEncoding("Shift_JIS");
}
}
}