kstools-license-manager / revit-plugin /LicenseActivationDialog.cs
KyrosDev's picture
Initial commit: Complete KSTools License Management System
ad67bb9
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" +
"如有問題請聯絡技術支援。";
// 可以在這裡顯示提示,或者加入幫助按鈕
}
}
}