diff --git a/OTP/Form1.Designer.cs b/OTP/Form1.Designer.cs index 9a88935..39e0eab 100644 --- a/OTP/Form1.Designer.cs +++ b/OTP/Form1.Designer.cs @@ -124,6 +124,7 @@ this.circularProgressBar1.TabIndex = 6; this.circularProgressBar1.Text = "time"; this.circularProgressBar1.TextMargin = new System.Windows.Forms.Padding(21, 2, 0, 0); + this.circularProgressBar1.Value = 30; // // button3 // @@ -169,6 +170,7 @@ this.Controls.Add(this.circularProgressBar1); this.Name = "Form1"; this.Text = "Main Window"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); this.ResumeLayout(false); this.PerformLayout(); diff --git a/OTP/Form1.cs b/OTP/Form1.cs index 32f4c2f..eacded1 100644 --- a/OTP/Form1.cs +++ b/OTP/Form1.cs @@ -82,5 +82,18 @@ namespace OTP // 隐藏当前的 Form1 实例 this.Hide(); } + + private void Form1_FormClosing(object sender, FormClosingEventArgs e) + { + DialogResult result = MessageBox.Show("Do you want to exit the application?", "Confirm Exit", + MessageBoxButtons.YesNo, MessageBoxIcon.Question); + + if (result == DialogResult.No) + { + e.Cancel = true; // 取消关闭事件,阻止关闭 + } + // 如果用户选择 Yes,什么都不需要做,窗体会继续关闭 + } + } } diff --git a/OTP/Form2.Designer.cs b/OTP/Form2.Designer.cs index 95e921f..0d664ac 100644 --- a/OTP/Form2.Designer.cs +++ b/OTP/Form2.Designer.cs @@ -112,6 +112,7 @@ this.Controls.Add(this.panel1); this.Name = "Form2"; this.Text = "Data Manage"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form2_FormClosing); this.panel1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.ResumeLayout(false); diff --git a/OTP/Form2.cs b/OTP/Form2.cs index d541d75..906afc9 100644 --- a/OTP/Form2.cs +++ b/OTP/Form2.cs @@ -60,5 +60,16 @@ namespace OTP } } } + + private void Form2_FormClosing(object sender, FormClosingEventArgs e) + { + Form1 form1 = Application.OpenForms["Form1"] as Form1; + if (form1 != null) + { + form1.Show(); + } + } + + } } diff --git a/OTP/Form3.Designer.cs b/OTP/Form3.Designer.cs index 8baa49d..5f07074 100644 --- a/OTP/Form3.Designer.cs +++ b/OTP/Form3.Designer.cs @@ -37,12 +37,18 @@ this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.button5 = new System.Windows.Forms.Button(); + this.button6 = new System.Windows.Forms.Button(); + this.label3 = new System.Windows.Forms.Label(); + this.label4 = new System.Windows.Forms.Label(); + this.label5 = new System.Windows.Forms.Label(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(26, 9); + this.label1.Location = new System.Drawing.Point(26, 13); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(27, 13); this.label1.TabIndex = 0; @@ -50,7 +56,7 @@ // // textBox1 // - this.textBox1.Location = new System.Drawing.Point(72, 6); + this.textBox1.Location = new System.Drawing.Point(72, 10); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(202, 20); this.textBox1.TabIndex = 1; @@ -58,7 +64,7 @@ // label2 // this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(12, 37); + this.label2.Location = new System.Drawing.Point(12, 41); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(54, 13); this.label2.TabIndex = 2; @@ -66,25 +72,27 @@ // // textBox2 // - this.textBox2.Location = new System.Drawing.Point(72, 34); + this.textBox2.Location = new System.Drawing.Point(72, 38); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(202, 20); this.textBox2.TabIndex = 3; // // button1 // - this.button1.Location = new System.Drawing.Point(92, 62); + this.button1.Location = new System.Drawing.Point(72, 68); this.button1.Name = "button1"; - this.button1.Size = new System.Drawing.Size(75, 23); + this.button1.Size = new System.Drawing.Size(100, 45); this.button1.TabIndex = 4; this.button1.Text = "Add"; this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // - this.button2.Location = new System.Drawing.Point(183, 62); + this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel; + this.button2.Location = new System.Drawing.Point(174, 68); this.button2.Name = "button2"; - this.button2.Size = new System.Drawing.Size(75, 23); + this.button2.Size = new System.Drawing.Size(100, 45); this.button2.TabIndex = 5; this.button2.Text = "Cancel"; this.button2.UseVisualStyleBackColor = true; @@ -93,35 +101,86 @@ // this.button3.Location = new System.Drawing.Point(292, 4); this.button3.Name = "button3"; - this.button3.Size = new System.Drawing.Size(117, 23); + this.button3.Size = new System.Drawing.Size(176, 23); this.button3.TabIndex = 6; - this.button3.Text = "Screenshot Scan"; + this.button3.Text = "Scan QR Code from Screenshot"; this.button3.UseVisualStyleBackColor = true; + this.button3.Click += new System.EventHandler(this.button3_Click); // // button4 // this.button4.Location = new System.Drawing.Point(292, 33); this.button4.Name = "button4"; - this.button4.Size = new System.Drawing.Size(117, 23); + this.button4.Size = new System.Drawing.Size(176, 23); this.button4.TabIndex = 7; - this.button4.Text = "Camera Scan"; + this.button4.Text = "Scan QR Code from Image File"; this.button4.UseVisualStyleBackColor = true; // // button5 // this.button5.Location = new System.Drawing.Point(292, 62); this.button5.Name = "button5"; - this.button5.Size = new System.Drawing.Size(117, 23); + this.button5.Size = new System.Drawing.Size(176, 23); this.button5.TabIndex = 8; - this.button5.Text = "Import from Clipboard"; + this.button5.Text = "Scan QR Code with Camera"; this.button5.UseVisualStyleBackColor = true; // + // button6 + // + this.button6.Location = new System.Drawing.Point(292, 91); + this.button6.Name = "button6"; + this.button6.Size = new System.Drawing.Size(176, 23); + this.button6.TabIndex = 9; + this.button6.Text = "Import from Clipboard"; + this.button6.UseVisualStyleBackColor = true; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(24, 33); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(33, 13); + this.label3.TabIndex = 11; + this.label3.Text = "None"; + // + // label4 + // + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(24, 59); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(37, 13); + this.label4.TabIndex = 12; + this.label4.Text = "Status"; + // + // label5 + // + this.label5.AutoSize = true; + this.label5.Location = new System.Drawing.Point(24, 86); + this.label5.Name = "label5"; + this.label5.Size = new System.Drawing.Size(25, 13); + this.label5.TabIndex = 13; + this.label5.Text = "Info"; + // + // groupBox1 + // + this.groupBox1.Controls.Add(this.label4); + this.groupBox1.Controls.Add(this.label5); + this.groupBox1.Controls.Add(this.label3); + this.groupBox1.Location = new System.Drawing.Point(15, 148); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(453, 243); + this.groupBox1.TabIndex = 14; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Debug Info"; + // // Form3 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.CancelButton = this.button2; - this.ClientSize = new System.Drawing.Size(418, 96); + this.ClientSize = new System.Drawing.Size(488, 403); + this.Controls.Add(this.groupBox1); + this.Controls.Add(this.button6); this.Controls.Add(this.button5); this.Controls.Add(this.button4); this.Controls.Add(this.button3); @@ -138,6 +197,9 @@ this.ShowIcon = false; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "New Record"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form3_FormClosing); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); @@ -154,5 +216,10 @@ private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; private System.Windows.Forms.Button button5; + private System.Windows.Forms.Button button6; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.Label label4; + private System.Windows.Forms.Label label5; + private System.Windows.Forms.GroupBox groupBox1; } } \ No newline at end of file diff --git a/OTP/Form3.cs b/OTP/Form3.cs index 8654182..641d020 100644 --- a/OTP/Form3.cs +++ b/OTP/Form3.cs @@ -1,22 +1,141 @@ -using System; +using ScreenCapturerNS; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; +using System.Data.SQLite; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using ZXing; namespace OTP { public partial class Form3 : Form { + private string connectionString = "Data Source=key.db;Version=3;"; + private bool isCapturing = false; // 添加标识截图状态的成员变量 + public Form3() { InitializeComponent(); } + private void Form3_FormClosing(object sender, FormClosingEventArgs e) + { + StopCapturing(); // 关闭窗口时停止截图 + } + private void button1_Click(object sender, EventArgs e) + { + try + { + string title = textBox1.Text; + string secretKey = textBox2.Text; + + if (!string.IsNullOrWhiteSpace(title) && !string.IsNullOrWhiteSpace(secretKey)) + { + using (SQLiteConnection connection = new SQLiteConnection(connectionString)) + { + connection.Open(); + + using (SQLiteCommand command = new SQLiteCommand("INSERT INTO TotpData (Title, SecretKey) VALUES (@Title, @SecretKey)", connection)) + { + command.Parameters.AddWithValue("@Title", title); + command.Parameters.AddWithValue("@SecretKey", secretKey); + command.ExecuteNonQuery(); + } + } + this.DialogResult = DialogResult.OK; + MessageBox.Show("Record added successfully."); + + // 关闭当前的 Form3 实例 + this.Close(); + } + else + { + MessageBox.Show("Please enter both title and secret key."); + } + } + catch (Exception ex) + { + MessageBox.Show($"An error occurred: {ex.Message}"); + } + } + + private void button3_Click(object sender, EventArgs e) + { + if (isCapturing) + { + label5.Text = "Stopped"; + StopCapturing(); // 停止截图 + } + else + { + label3.Text = "None"; + label5.Text = "Running"; + label4.Text = "No QR code found."; + StartCapturing(); // 开始截图 + } + } + + private void StartCapturing() + { + isCapturing = true; // 设置截图状态为正在截图 + ScreenCapturer.StartCapture((Bitmap bitmap) => { + // 进行二维码扫描 + ScanQRCode(bitmap); + }); + } + + private void StopCapturing() + { + isCapturing = false; // 设置截图状态为未截图 + ScreenCapturer.StopCapture(); // 停止截图 + } + + + private void ScanQRCode(Bitmap bitmap) + { + // 创建二维码读取器实例 + BarcodeReader barcodeReader = new BarcodeReader(); + + // 尝试解码二维码 + Result result = barcodeReader.Decode(bitmap); + if (result != null) + { + // 扫描成功,显示扫描结果 + string decodedText = result.Text; + this.Invoke((MethodInvoker)(() => label3.Text = decodedText)); + + // 判断是否符合期望的格式 + if (decodedText.StartsWith("otpauth://totp/") && decodedText.Contains("?secret=")) + { + StopCapturing(); + this.Invoke((MethodInvoker)(() => label5.Text = "Stopped")); + string[] parts = decodedText.Split(new char[] { '/', '=', '&','?' }); + if (parts.Length >= 6) + { + string title = parts[3]; // 获取 title 部分 + string secretKey = parts[5]; // 获取 secretKey 部分 + + // 在窗体上显示 title 和 secretKey + this.Invoke((MethodInvoker)(() => textBox1.Text = title)); + this.Invoke((MethodInvoker)(() => textBox2.Text = secretKey)); + this.Invoke((MethodInvoker)(() => label4.Text = "Success")); + } + else + { + this.Invoke((MethodInvoker)(() => label4.Text = "Invalid QR code format.")); + } + } + else + { + this.Invoke((MethodInvoker)(() => label4.Text = "QR code format not recognized.")); + } + } + } } } diff --git a/OTP/Form4.Designer.cs b/OTP/Form4.Designer.cs new file mode 100644 index 0000000..4006ef5 --- /dev/null +++ b/OTP/Form4.Designer.cs @@ -0,0 +1,73 @@ +namespace OTP +{ + partial class Form4 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.button1 = new System.Windows.Forms.Button(); + this.pictureBox1 = new System.Windows.Forms.PictureBox(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); + this.SuspendLayout(); + // + // button1 + // + this.button1.Location = new System.Drawing.Point(713, 160); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(75, 23); + this.button1.TabIndex = 0; + this.button1.Text = "button1"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); + // + // pictureBox1 + // + this.pictureBox1.Location = new System.Drawing.Point(12, 12); + this.pictureBox1.Name = "pictureBox1"; + this.pictureBox1.Size = new System.Drawing.Size(695, 426); + this.pictureBox1.TabIndex = 1; + this.pictureBox1.TabStop = false; + // + // Form4 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.pictureBox1); + this.Controls.Add(this.button1); + this.Name = "Form4"; + this.Text = "Form4"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.Button button1; + private System.Windows.Forms.PictureBox pictureBox1; + } +} \ No newline at end of file diff --git a/OTP/Form4.cs b/OTP/Form4.cs new file mode 100644 index 0000000..100b76c --- /dev/null +++ b/OTP/Form4.cs @@ -0,0 +1,31 @@ +using ScreenCapturerNS; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace OTP +{ + public partial class Form4 : Form + { + public Form4() + { + InitializeComponent(); + } + + private void button1_Click(object sender, EventArgs e) + { + ScreenCapturer.StartCapture((Bitmap bitmap) => { + // 处理截图 (bitmap) 这里 + // 例如,你可以将截图显示在某个 PictureBox 控件中 + pictureBox1.Image = bitmap; + ScreenCapturer.StopCapture(); + }); + } + } +} diff --git a/OTP/Form4.resx b/OTP/Form4.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/OTP/Form4.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/OTP/OTP.csproj b/OTP/OTP.csproj index 509888e..2aa5424 100644 --- a/OTP/OTP.csproj +++ b/OTP/OTP.csproj @@ -48,6 +48,18 @@ ..\packages\Otp.NET.1.3.0\lib\net45\Otp.NET.dll + + ..\packages\ScreenCapturer.3.0.1\lib\netstandard2.0\ScreenCapturer.dll + + + ..\packages\SharpDX.4.2.0\lib\net45\SharpDX.dll + + + ..\packages\SharpDX.Direct3D11.4.2.0\lib\net45\SharpDX.Direct3D11.dll + + + ..\packages\SharpDX.DXGI.4.2.0\lib\net45\SharpDX.DXGI.dll + @@ -60,6 +72,9 @@ ..\packages\System.Data.SQLite.Linq.1.0.118.0\lib\net46\System.Data.SQLite.Linq.dll + + ..\packages\System.Drawing.Common.7.0.0\lib\net462\System.Drawing.Common.dll + ..\packages\System.IO.4.3.0\lib\net462\System.IO.dll True @@ -77,6 +92,12 @@ ..\packages\WinFormAnimation.1.6.0.4\lib\net40\WinFormAnimation.dll + + ..\packages\ZXing.Net.0.16.9\lib\net48\zxing.dll + + + ..\packages\ZXing.Net.0.16.9\lib\net48\zxing.presentation.dll + @@ -97,6 +118,12 @@ Form3.cs + + Form + + + Form4.cs + @@ -108,6 +135,9 @@ Form3.cs + + Form4.cs + ResXFileCodeGenerator Resources.Designer.cs diff --git a/OTP/packages.config b/OTP/packages.config index 2cdfc1c..2c93e7f 100644 --- a/OTP/packages.config +++ b/OTP/packages.config @@ -3,11 +3,17 @@ + + + + + + \ No newline at end of file