From 0f1aff24a5e2d2f5f36579d35649af229faad218 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Tue, 13 Jan 2015 01:32:08 +0800 Subject: [PATCH] try 5 different focus --- shadowsocks-csharp/Data/cn.txt | 2 + shadowsocks-csharp/View/MenuViewController.cs | 75 +++++++++++++------ .../View/QRCodeSplashForm.Designer.cs | 17 ++++- 3 files changed, 71 insertions(+), 23 deletions(-) diff --git a/shadowsocks-csharp/Data/cn.txt b/shadowsocks-csharp/Data/cn.txt index e091b27..82ea174 100644 --- a/shadowsocks-csharp/Data/cn.txt +++ b/shadowsocks-csharp/Data/cn.txt @@ -43,3 +43,5 @@ Disabled=已禁用代理 Update PAC from GFWList=从 GFWList 更新 PAC Failed to update PAC file =更新 PAC 文件失败 PAC updated=更新 PAC 成功 +"Failed to find QRCode=无法找到二维码 +Failed to decode QRCode=无法解析二维码 diff --git a/shadowsocks-csharp/View/MenuViewController.cs b/shadowsocks-csharp/View/MenuViewController.cs index d0eb8d9..fb09202 100755 --- a/shadowsocks-csharp/View/MenuViewController.cs +++ b/shadowsocks-csharp/View/MenuViewController.cs @@ -33,6 +33,7 @@ namespace Shadowsocks.View private MenuItem globalModeItem; private MenuItem PACModeItem; private ConfigForm configForm; + private string _urlToOpen; public MenuViewController(ShadowsocksController controller) { @@ -362,31 +363,57 @@ namespace Shadowsocks.View private void ScanQRCodeItem_Click(object sender, EventArgs e) { - using (Bitmap image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, + using (Bitmap fullImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)) { - using (Graphics g = Graphics.FromImage(image)) + using (Graphics g = Graphics.FromImage(fullImage)) { g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, - image.Size, + fullImage.Size, CopyPixelOperation.SourceCopy); } - var reader = new BarcodeReader + for (int i = 0; i < 5; i++) { - TryHarder = true, - PossibleFormats = new List + int marginLeft = fullImage.Width * i / 3 / 5; + int marginTop = fullImage.Height * i / 3 / 5; + Rectangle cropRect = new Rectangle(marginLeft, marginTop, fullImage.Width - marginLeft * 2, fullImage.Height - marginTop * 2); + Bitmap target = new Bitmap(cropRect.Width, cropRect.Height); + + using (Graphics g = Graphics.FromImage(target)) { - BarcodeFormat.QR_CODE + g.DrawImage(fullImage, new Rectangle(0, 0, target.Width, target.Height), + cropRect, + GraphicsUnit.Pixel); } - }; - var result = reader.Decode(image); - if (result != null) - { - var success = controller.AddServerBySSURL(result.Text); - if (success) + var reader = new BarcodeReader { + TryHarder = true, + PossibleFormats = new List + { + BarcodeFormat.QR_CODE + } + }; + var result = reader.Decode(target); + if (result != null) + { + var success = controller.AddServerBySSURL(result.Text); + QRCodeSplashForm splash = new QRCodeSplashForm(); + if (success) + { + splash.FormClosed += splash_FormClosed; + } + else if (result.Text.StartsWith("http://") || result.Text.StartsWith("https://")) + { + _urlToOpen = result.Text; + splash.FormClosed += openURLFromQRCode; + } + else + { + MessageBox.Show(I18N.GetString("Failed to decode QRCode")); + return; + } float minX = Int32.MaxValue, minY = Int32.MaxValue, maxX = 0, maxY = 0; foreach (ResultPoint point in result.ResultPoints) { @@ -397,21 +424,20 @@ namespace Shadowsocks.View } // make it 20% larger float margin = (maxX - minX) * 0.20f; - minX -= margin; - maxX += margin; - minY -= margin; - maxY += margin; - - QRCodeSplashForm splash = new QRCodeSplashForm(); - splash.FormClosed += splash_FormClosed; + minX += -margin + marginLeft; + maxX += margin + marginLeft; + minY += -margin + marginTop; + maxY += margin + marginTop; splash.Location = new Point((int)minX, (int)minY); - splash.Size = new Size((int)maxX - (int)minX, (int)maxY - (int)minY); + // we need a panel because a window has a minimal size + splash.Panel.Size = new Size((int)maxX - (int)minX, (int)maxY - (int)minY); + splash.Size = splash.Panel.Size; splash.Show(); return; } } - MessageBox.Show(I18N.GetString("Failed to scan QRCode")); } + MessageBox.Show(I18N.GetString("Failed to find QRCode")); } void splash_FormClosed(object sender, FormClosedEventArgs e) @@ -419,6 +445,11 @@ namespace Shadowsocks.View ShowConfigForm(); } + void openURLFromQRCode(object sender, FormClosedEventArgs e) + { + Process.Start(_urlToOpen); + } + private void AutoStartupItem_Click(object sender, EventArgs e) { AutoStartupItem.Checked = !AutoStartupItem.Checked; if (!AutoStartup.Set(AutoStartupItem.Checked)) { diff --git a/shadowsocks-csharp/View/QRCodeSplashForm.Designer.cs b/shadowsocks-csharp/View/QRCodeSplashForm.Designer.cs index 87f9dd6..0c7e160 100755 --- a/shadowsocks-csharp/View/QRCodeSplashForm.Designer.cs +++ b/shadowsocks-csharp/View/QRCodeSplashForm.Designer.cs @@ -28,14 +28,25 @@ /// private void InitializeComponent() { + this.Panel = new System.Windows.Forms.Panel(); this.SuspendLayout(); // + // Panel + // + this.Panel.BackColor = System.Drawing.Color.Crimson; + this.Panel.Location = new System.Drawing.Point(0, 0); + this.Panel.Margin = new System.Windows.Forms.Padding(0); + this.Panel.Name = "Panel"; + this.Panel.Size = new System.Drawing.Size(168, 158); + this.Panel.TabIndex = 0; + // // QRCodeSplashForm // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; - this.BackColor = System.Drawing.Color.Crimson; + this.BackColor = System.Drawing.Color.White; this.ClientSize = new System.Drawing.Size(284, 262); this.ControlBox = false; + this.Controls.Add(this.Panel); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.MaximizeBox = false; this.MinimizeBox = false; @@ -47,11 +58,15 @@ this.StartPosition = System.Windows.Forms.FormStartPosition.Manual; this.Text = "QRCodeSplashForm"; this.TopMost = true; + this.TransparencyKey = System.Drawing.Color.White; this.Load += new System.EventHandler(this.QRCodeSplashForm_Load); this.ResumeLayout(false); } #endregion + + public System.Windows.Forms.Panel Panel; + } } \ No newline at end of file