File size: 11,641 Bytes
ad67bb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using System;
using System.Drawing;
using System.Windows.Forms;

namespace KSTools.Licensing
{
    /// <summary>
    /// 授權啟用對話框
    /// 提供用戶友好的授權碼輸入界面
    /// </summary>
    public partial class LicenseActivationDialog : Form
    {
        /// <summary>
        /// 用戶輸入的授權碼
        /// </summary>
        public string LicenseCode { get; private set; }
        
        private TextBox txtLicenseCode;
        private TextBox txtHardwareId;
        private Button btnActivate;
        private Button btnCancel;
        private Button btnCopyHardwareId;
        private Label lblInstructions;
        private Label lblHardwareId;
        private Label lblLicenseCode;
        private PictureBox picIcon;
        
        /// <summary>
        /// 初始化授權啟用對話框
        /// </summary>
        /// <param name="hardwareId">硬體指紋ID</param>
        public LicenseActivationDialog(string hardwareId)
        {
            InitializeComponent();
            txtHardwareId.Text = hardwareId;
            
            // 設定對話框屬性
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ShowInTaskbar = false;
        }
        
        /// <summary>
        /// 初始化組件
        /// </summary>
        private void InitializeComponent()
        {
            this.txtLicenseCode = new TextBox();
            this.txtHardwareId = new TextBox();
            this.btnActivate = new Button();
            this.btnCancel = new Button();
            this.btnCopyHardwareId = new Button();
            this.lblInstructions = new Label();
            this.lblHardwareId = new Label();
            this.lblLicenseCode = new Label();
            this.picIcon = new PictureBox();
            
            ((System.ComponentModel.ISupportInitialize)(this.picIcon)).BeginInit();
            this.SuspendLayout();
            
            // 
            // picIcon
            // 
            this.picIcon.Location = new Point(12, 12);
            this.picIcon.Size = new Size(48, 48);
            this.picIcon.SizeMode = PictureBoxSizeMode.StretchImage;
            // 這裡可以設定圖示,例如:this.picIcon.Image = Properties.Resources.KSToolsIcon;
            
            // 
            // lblInstructions
            // 
            this.lblInstructions.AutoSize = false;
            this.lblInstructions.Location = new Point(76, 12);
            this.lblInstructions.Size = new Size(380, 60);
            this.lblInstructions.Text = "歡迎使用 KSTools!\n\n" +
                                      "請將下方的硬體 ID 提供給管理員以取得授權碼,\n" +
                                      "然後在下方輸入授權碼進行啟用。";
            this.lblInstructions.Font = new Font("Microsoft JhengHei", 9F, FontStyle.Regular);
            
            // 
            // lblHardwareId
            // 
            this.lblHardwareId.AutoSize = true;
            this.lblHardwareId.Location = new Point(12, 85);
            this.lblHardwareId.Size = new Size(68, 15);
            this.lblHardwareId.Text = "硬體 ID:";
            this.lblHardwareId.Font = new Font("Microsoft JhengHei", 9F, FontStyle.Bold);
            
            // 
            // txtHardwareId
            // 
            this.txtHardwareId.Location = new Point(12, 105);
            this.txtHardwareId.Size = new Size(350, 25);
            this.txtHardwareId.ReadOnly = true;
            this.txtHardwareId.BackColor = Color.LightGray;
            this.txtHardwareId.Font = new Font("Consolas", 9F, FontStyle.Regular);
            
            // 
            // btnCopyHardwareId
            // 
            this.btnCopyHardwareId.Location = new Point(370, 105);
            this.btnCopyHardwareId.Size = new Size(75, 25);
            this.btnCopyHardwareId.Text = "複製";
            this.btnCopyHardwareId.UseVisualStyleBackColor = true;
            this.btnCopyHardwareId.Click += new EventHandler(this.btnCopyHardwareId_Click);
            
            // 
            // lblLicenseCode
            // 
            this.lblLicenseCode.AutoSize = true;
            this.lblLicenseCode.Location = new Point(12, 145);
            this.lblLicenseCode.Size = new Size(68, 15);
            this.lblLicenseCode.Text = "授權碼:";
            this.lblLicenseCode.Font = new Font("Microsoft JhengHei", 9F, FontStyle.Bold);
            
            // 
            // txtLicenseCode
            // 
            this.txtLicenseCode.Location = new Point(12, 165);
            this.txtLicenseCode.Size = new Size(433, 25);
            this.txtLicenseCode.Font = new Font("Consolas", 10F, FontStyle.Regular);
            this.txtLicenseCode.CharacterCasing = CharacterCasing.Upper;
            this.txtLicenseCode.MaxLength = 50;
            this.txtLicenseCode.TextChanged += new EventHandler(this.txtLicenseCode_TextChanged);
            this.txtLicenseCode.KeyPress += new KeyPressEventHandler(this.txtLicenseCode_KeyPress);
            
            // 
            // btnActivate
            // 
            this.btnActivate.Location = new Point(280, 210);
            this.btnActivate.Size = new Size(80, 30);
            this.btnActivate.Text = "啟用";
            this.btnActivate.UseVisualStyleBackColor = true;
            this.btnActivate.Enabled = false;
            this.btnActivate.Font = new Font("Microsoft JhengHei", 9F, FontStyle.Bold);
            this.btnActivate.Click += new EventHandler(this.btnActivate_Click);
            
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new Point(370, 210);
            this.btnCancel.Size = new Size(80, 30);
            this.btnCancel.Text = "取消";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.DialogResult = DialogResult.Cancel;
            
            // 
            // LicenseActivationDialog
            // 
            this.AcceptButton = this.btnActivate;
            this.CancelButton = this.btnCancel;
            this.ClientSize = new Size(467, 260);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnActivate);
            this.Controls.Add(this.txtLicenseCode);
            this.Controls.Add(this.lblLicenseCode);
            this.Controls.Add(this.btnCopyHardwareId);
            this.Controls.Add(this.txtHardwareId);
            this.Controls.Add(this.lblHardwareId);
            this.Controls.Add(this.lblInstructions);
            this.Controls.Add(this.picIcon);
            this.Font = new Font("Microsoft JhengHei", 9F, FontStyle.Regular);
            this.Text = "KSTools 授權啟用";
            
            ((System.ComponentModel.ISupportInitialize)(this.picIcon)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        
        /// <summary>
        /// 複製硬體ID到剪貼簿
        /// </summary>
        private void btnCopyHardwareId_Click(object sender, EventArgs e)
        {
            try
            {
                Clipboard.SetText(txtHardwareId.Text);
                MessageBox.Show("硬體 ID 已複製到剪貼簿!", "複製成功", 
                              MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"複製失敗:{ex.Message}", "錯誤", 
                              MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        /// <summary>
        /// 授權碼文字變更事件
        /// </summary>
        private void txtLicenseCode_TextChanged(object sender, EventArgs e)
        {
            // 檢查授權碼格式並啟用/停用啟用按鈕
            string licenseCode = txtLicenseCode.Text.Trim();
            
            // 基本格式檢查:以KS開頭且長度合適
            bool isValidFormat = licenseCode.StartsWith("KS") && licenseCode.Length >= 8 && licenseCode.Length <= 20;
            
            btnActivate.Enabled = isValidFormat;
            
            // 變更文字顏色提示
            if (string.IsNullOrEmpty(licenseCode))
            {
                txtLicenseCode.ForeColor = Color.Black;
            }
            else if (isValidFormat)
            {
                txtLicenseCode.ForeColor = Color.Green;
            }
            else
            {
                txtLicenseCode.ForeColor = Color.Red;
            }
        }
        
        /// <summary>
        /// 授權碼按鍵事件
        /// </summary>
        private void txtLicenseCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            // 只允許英數字元
            if (!char.IsLetterOrDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        
        /// <summary>
        /// 啟用按鈕點擊事件
        /// </summary>
        private void btnActivate_Click(object sender, EventArgs e)
        {
            string licenseCode = txtLicenseCode.Text.Trim().ToUpper();
            
            if (string.IsNullOrEmpty(licenseCode))
            {
                MessageBox.Show("請輸入授權碼!", "輸入錯誤", 
                              MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtLicenseCode.Focus();
                return;
            }
            
            if (!licenseCode.StartsWith("KS"))
            {
                MessageBox.Show("授權碼格式錯誤!授權碼必須以 'KS' 開頭。", "格式錯誤", 
                              MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtLicenseCode.Focus();
                return;
            }
            
            // 確認對話框
            var confirmResult = MessageBox.Show(
                $"確定要使用授權碼 '{licenseCode}' 進行啟用嗎?\n\n" +
                $"授權碼將與此電腦的硬體 ID 綁定,無法轉移到其他電腦使用。",
                "確認啟用",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question);
            
            if (confirmResult == DialogResult.Yes)
            {
                this.LicenseCode = licenseCode;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
        
        /// <summary>
        /// 表單載入事件
        /// </summary>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            
            // 聚焦到授權碼輸入框
            txtLicenseCode.Focus();
            
            // 設定提示訊息
            this.ShowTips();
        }
        
        /// <summary>
        /// 顯示使用提示
        /// </summary>
        private void ShowTips()
        {
            var tips = "使用提示:\n\n" +
                      "1. 請將上方的硬體 ID 提供給系統管理員\n" +
                      "2. 管理員會為您生成專屬的授權碼\n" +
                      "3. 在授權碼輸入框中輸入完整的授權碼\n" +
                      "4. 授權碼格式:KS + 數字字母組合\n" +
                      "5. 每個授權碼只能在一台電腦上使用\n\n" +
                      "如有問題請聯絡技術支援。";
            
            // 可以在這裡顯示提示,或者加入幫助按鈕
        }
    }
}