liuyuqi-dellpc 3 years ago
parent
commit
d8df82f65e

+ 31 - 0
ShareWifi/Model/QRModel.cs

@@ -0,0 +1,31 @@
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ShareWifi.Model
+{
+    class QRModel
+    {
+        private string path; //图片所在文件夹
+        private string name; //图片名
+        private Bitmap qrData; //二维码
+
+        public QRModel()
+        {
+        }
+
+        public QRModel(string mpath, string picname, Bitmap map)
+        {
+            Path = mpath;
+            name = picname;
+            QrData = map;
+        }
+
+        public string Path { get => path; set => path = value; }
+        public Bitmap QrData { get => qrData; set => qrData = value; }
+        public string Name { get => name; set => name = value; }
+    }
+}

+ 20 - 0
ShareWifi/Model/WifiModel.cs

@@ -0,0 +1,20 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ShareWifi.Model
+{
+    class WifiModel
+    {
+        private string ssid;
+        string pwd;
+        string mac;
+
+        public string getWifi()
+        {
+            return "WIFI:T:WPA;p:\"" + pwd + "\";S:" + ssid + ";";
+        }
+    }
+}

+ 20 - 3
ShareWifi/ShareWifi.csproj

@@ -33,6 +33,11 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="PresentationCore" />
+    <Reference Include="PresentationFramework" />
+    <Reference Include="QRCoder, Version=1.3.9.0, Culture=neutral, processorArchitecture=MSIL">
+      <HintPath>..\packages\QRCoder.1.3.9\lib\net40\QRCoder.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="System.Xml.Linq" />
@@ -44,6 +49,7 @@
     <Reference Include="System.Net.Http" />
     <Reference Include="System.Windows.Forms" />
     <Reference Include="System.Xml" />
+    <Reference Include="WindowsBase" />
   </ItemGroup>
   <ItemGroup>
     <Compile Include="MainForm.cs">
@@ -52,9 +58,12 @@
     <Compile Include="MainForm.Designer.cs">
       <DependentUpon>MainForm.cs</DependentUpon>
     </Compile>
+    <Compile Include="Model\QRModel.cs" />
+    <Compile Include="Model\WifiModel.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Utils\Net\WifiUtils.cs" />
+    <Compile Include="Utils\QRUtils.cs" />
     <Compile Include="Utils\Shell.cs" />
     <Compile Include="Views\AboutMe.cs">
       <SubType>UserControl</SubType>
@@ -62,6 +71,12 @@
     <Compile Include="Views\AboutMe.Designer.cs">
       <DependentUpon>AboutMe.cs</DependentUpon>
     </Compile>
+    <Compile Include="Views\Components\SimpleAlert.cs">
+      <SubType>Form</SubType>
+    </Compile>
+    <Compile Include="Views\Components\SimpleAlert.Designer.cs">
+      <DependentUpon>SimpleAlert.cs</DependentUpon>
+    </Compile>
     <Compile Include="Views\Home.cs">
       <SubType>UserControl</SubType>
     </Compile>
@@ -84,9 +99,13 @@
     <EmbeddedResource Include="Views\AboutMe.resx">
       <DependentUpon>AboutMe.cs</DependentUpon>
     </EmbeddedResource>
+    <EmbeddedResource Include="Views\Components\SimpleAlert.resx">
+      <DependentUpon>SimpleAlert.cs</DependentUpon>
+    </EmbeddedResource>
     <EmbeddedResource Include="Views\Home.resx">
       <DependentUpon>Home.cs</DependentUpon>
     </EmbeddedResource>
+    <None Include="packages.config" />
     <None Include="Properties\Settings.settings">
       <Generator>SettingsSingleFileGenerator</Generator>
       <LastGenOutput>Settings.Designer.cs</LastGenOutput>
@@ -112,8 +131,6 @@
   <ItemGroup>
     <None Include="Resources\Minus-icon.png" />
   </ItemGroup>
-  <ItemGroup>
-    <Folder Include="Model\" />
-  </ItemGroup>
+  <ItemGroup />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 9 - 6
ShareWifi/Utils/Net/WifiUtils.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 
 namespace ShareWifi.Utils.Net
@@ -9,23 +10,25 @@ namespace ShareWifi.Utils.Net
     class WifiUtils
     {
         /// <summary>
-        /// 获取WIFI SSID
+        /// 获取WIFI SSID ,如果为空则没有连接wifi
         /// </summary>
         /// <returns></returns>
         public static string getSSID()
         {
-            string v = Shell.RunCmd("netsh wlan show interfaces | findstr SSID");
-            return v;
+            string cmdRes = Shell.RunCmd("netsh wlan show interfaces | findstr SSID");
+            Regex regex = new Regex("[^B]SSID\\s+:\\s(.*)");
+            return regex.Match(cmdRes).Groups[1].Value.Trim();
         }
         /// <summary>
-        /// 根据SSID获取密码
+        /// 根据 SSID 获取密码
         /// </summary>
         /// <param name="ssid">SSID</param>
         /// <returns></returns>
         public static string getSSDIPassword(string ssid)
         {
-            string v = Shell.RunCmd("netsh wlan show profile name=" + ssid + " key=clear | findstr Key");
-            return v;
+            string  cmdRes = Shell.RunCmd("netsh wlan show profile name=" + ssid + " key=clear | findstr Key");
+            Regex regex = new Regex("Key Content\\s+:\\s(.*)");
+            return regex.Match(cmdRes).Groups[1].Value.Trim();
         }
     }
 }

+ 34 - 0
ShareWifi/Utils/QRUtils.cs

@@ -0,0 +1,34 @@
+using QRCoder;
+using ShareWifi.Model;
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Drawing.Imaging;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ShareWifi.Utils
+{
+    class QRUtils
+    {
+        /// <summary>
+        /// 根据 data 生成二维码
+        /// </summary>
+        /// <param name="data"></param>
+        public static QRModel genQR(string data)
+        {
+            QRCodeGenerator qrGenerator = new QRCodeGenerator();
+            QRCodeData qrCodeData = qrGenerator.CreateQrCode(data, QRCodeGenerator.ECCLevel.Q);
+            QRCode qrCode = new QRCode(qrCodeData);
+            Bitmap bitmap = qrCode.GetGraphic(5);
+
+            //保存图片
+
+            string fileName = System.DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss") + ".jpg";
+            string filePath = System.Environment.CurrentDirectory;
+            bitmap.Save(filePath + "/" + fileName, ImageFormat.Jpeg);
+            return new QRModel(filePath, fileName, bitmap);
+        }
+    }
+}

+ 85 - 0
ShareWifi/Views/Components/SimpleAlert.Designer.cs

@@ -0,0 +1,85 @@
+namespace ShareWifi.Views.Components
+{
+    partial class SimpleAlert
+    {
+        /// <summary>
+        /// Required designer variable.
+        /// </summary>
+        private System.ComponentModel.IContainer components = null;
+
+        /// <summary>
+        /// Clean up any resources being used.
+        /// </summary>
+        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
+        protected override void Dispose(bool disposing)
+        {
+            if (disposing && (components != null))
+            {
+                components.Dispose();
+            }
+            base.Dispose(disposing);
+        }
+
+        #region Windows Form Designer generated code
+
+        /// <summary>
+        /// Required method for Designer support - do not modify
+        /// the contents of this method with the code editor.
+        /// </summary>
+        private void InitializeComponent()
+        {
+            this.components = new System.ComponentModel.Container();
+            this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
+            this.lbMessage = new System.Windows.Forms.Label();
+            this.timer1 = new System.Windows.Forms.Timer(this.components);
+            this.SuspendLayout();
+            // 
+            // dateTimePicker1
+            // 
+            this.dateTimePicker1.Location = new System.Drawing.Point(489, 192);
+            this.dateTimePicker1.Name = "dateTimePicker1";
+            this.dateTimePicker1.Size = new System.Drawing.Size(200, 21);
+            this.dateTimePicker1.TabIndex = 0;
+            // 
+            // lbMessage
+            // 
+            this.lbMessage.AutoSize = true;
+            this.lbMessage.Font = new System.Drawing.Font("SimSun", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.lbMessage.ForeColor = System.Drawing.Color.White;
+            this.lbMessage.Location = new System.Drawing.Point(72, 9);
+            this.lbMessage.Name = "lbMessage";
+            this.lbMessage.Size = new System.Drawing.Size(34, 15);
+            this.lbMessage.TabIndex = 1;
+            this.lbMessage.Text = "85%";
+            // 
+            // timer1
+            // 
+            this.timer1.Interval = 5000;
+            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
+            // 
+            // SimpleAlert
+            // 
+            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
+            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+            this.BackColor = System.Drawing.Color.Black;
+            this.ClientSize = new System.Drawing.Size(169, 37);
+            this.Controls.Add(this.lbMessage);
+            this.Controls.Add(this.dateTimePicker1);
+            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
+            this.Name = "SimpleAlert";
+            this.Opacity = 0.5D;
+            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
+            this.Text = "SimpleAlert";
+            this.Load += new System.EventHandler(this.SimpleAlert_Load);
+            this.ResumeLayout(false);
+            this.PerformLayout();
+
+        }
+
+        #endregion
+
+        private System.Windows.Forms.DateTimePicker dateTimePicker1;
+        private System.Windows.Forms.Label lbMessage;
+        private System.Windows.Forms.Timer timer1;
+    }
+}

+ 49 - 0
ShareWifi/Views/Components/SimpleAlert.cs

@@ -0,0 +1,49 @@
+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 ShareWifi.Views.Components
+{
+    /// <summary>
+    /// 进度条弹框消息
+    /// </summary>
+    public partial class SimpleAlert : Form
+    {
+        public SimpleAlert()
+        {
+            InitializeComponent();
+        }
+
+        public static SimpleAlert getInstance()
+        {
+            return new SimpleAlert();
+        }
+
+        public void Show(string message)
+        {
+            lbMessage.Text = message;
+            Show();
+        }
+
+        private void SimpleAlert_Load(object sender, EventArgs e)
+        {
+
+        }
+
+        /// <summary>
+        /// 500 ms 之后窗体自动关闭
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void timer1_Tick(object sender, EventArgs e)
+        {
+            Close();
+        }
+    }
+}

+ 123 - 0
ShareWifi/Views/Components/SimpleAlert.resx

@@ -0,0 +1,123 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+  <!-- 
+    Microsoft ResX Schema 
+    
+    Version 2.0
+    
+    The primary goals of this format is to allow a simple XML format 
+    that is mostly human readable. The generation and parsing of the 
+    various data types are done through the TypeConverter classes 
+    associated with the data types.
+    
+    Example:
+    
+    ... ado.net/XML headers & schema ...
+    <resheader name="resmimetype">text/microsoft-resx</resheader>
+    <resheader name="version">2.0</resheader>
+    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
+    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
+    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
+    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
+    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
+        <value>[base64 mime encoded serialized .NET Framework object]</value>
+    </data>
+    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
+        <comment>This is a comment</comment>
+    </data>
+                
+    There are any number of "resheader" rows that contain simple 
+    name/value pairs.
+    
+    Each data row contains a name, and value. The row also contains a 
+    type or mimetype. Type corresponds to a .NET class that support 
+    text/value conversion through the TypeConverter architecture. 
+    Classes that don't support this are serialized and stored with the 
+    mimetype set.
+    
+    The mimetype is used for serialized objects, and tells the 
+    ResXResourceReader how to depersist the object. This is currently not 
+    extensible. For a given mimetype the value must be set accordingly:
+    
+    Note - application/x-microsoft.net.object.binary.base64 is the format 
+    that the ResXResourceWriter will generate, however the reader can 
+    read any of the formats listed below.
+    
+    mimetype: application/x-microsoft.net.object.binary.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
+            : and then encoded with base64 encoding.
+    
+    mimetype: application/x-microsoft.net.object.soap.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
+            : and then encoded with base64 encoding.
+
+    mimetype: application/x-microsoft.net.object.bytearray.base64
+    value   : The object must be serialized into a byte array 
+            : using a System.ComponentModel.TypeConverter
+            : and then encoded with base64 encoding.
+    -->
+  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
+    <xsd:element name="root" msdata:IsDataSet="true">
+      <xsd:complexType>
+        <xsd:choice maxOccurs="unbounded">
+          <xsd:element name="metadata">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" />
+              </xsd:sequence>
+              <xsd:attribute name="name" use="required" type="xsd:string" />
+              <xsd:attribute name="type" type="xsd:string" />
+              <xsd:attribute name="mimetype" type="xsd:string" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="assembly">
+            <xsd:complexType>
+              <xsd:attribute name="alias" type="xsd:string" />
+              <xsd:attribute name="name" type="xsd:string" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="data">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
+              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="resheader">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" />
+            </xsd:complexType>
+          </xsd:element>
+        </xsd:choice>
+      </xsd:complexType>
+    </xsd:element>
+  </xsd:schema>
+  <resheader name="resmimetype">
+    <value>text/microsoft-resx</value>
+  </resheader>
+  <resheader name="version">
+    <value>2.0</value>
+  </resheader>
+  <resheader name="reader">
+    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <resheader name="writer">
+    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <metadata name="timer1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+    <value>17, 17</value>
+  </metadata>
+</root>

+ 16 - 1
ShareWifi/Views/Home.cs

@@ -8,11 +8,14 @@ using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using ShareWifi.Utils.Net;
+using ShareWifi.Views.Components;
+using ShareWifi.Model;
 
 namespace ShareWifi.Views
 {
     public partial class Home : UserControl
     {
+        WifiModel wifiModel;
         public Home()
         {
             InitializeComponent();
@@ -24,6 +27,12 @@ namespace ShareWifi.Views
 
         }
 
+        public override bool Equals(object obj)
+        {
+            return obj is Home home &&
+                   EqualityComparer<WifiModel>.Default.Equals(wifiModel, home.wifiModel);
+        }
+
         private void btnGenerate_Click(object sender, EventArgs e)
         {
             string ssid = WifiUtils.getSSID();
@@ -32,7 +41,13 @@ namespace ShareWifi.Views
                 //查找密码
                 string v = WifiUtils.getSSDIPassword(ssid);
                 //生成二维码
-                
+                btnGenerate.Text = v;
+            }
+            else
+            {
+                SimpleAlert simpleAlert = SimpleAlert.getInstance();
+                simpleAlert.Show("请链接 WIFI 后才可以分享!");
+
             }
         }
     }

+ 4 - 0
ShareWifi/packages.config

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="QRCoder" version="1.3.9" targetFramework="net461" />
+</packages>