Browse Source

修复闪屏问题

liuyuqi-dellpc 1 year ago
parent
commit
9074f5c06e

+ 5 - 5
DayOf1440/DayOf1440.csproj

@@ -57,13 +57,13 @@
     </Compile>
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
-    <Compile Include="UI\BaseForm.cs">
+    <Compile Include="Pages\BaseForm.cs">
       <SubType>Form</SubType>
     </Compile>
-    <Compile Include="UI\LifeForm.cs">
+    <Compile Include="Pages\LifeForm.cs">
       <SubType>Form</SubType>
     </Compile>
-    <Compile Include="UI\LifeForm.Designer.cs">
+    <Compile Include="Pages\LifeForm.Designer.cs">
       <DependentUpon>LifeForm.cs</DependentUpon>
     </Compile>
     <Compile Include="Utils\SystemSleep.cs" />
@@ -80,10 +80,10 @@
       <DependentUpon>Resources.resx</DependentUpon>
       <DesignTime>True</DesignTime>
     </Compile>
-    <EmbeddedResource Include="UI\BaseForm.resx">
+    <EmbeddedResource Include="Pages\BaseForm.resx">
       <DependentUpon>BaseForm.cs</DependentUpon>
     </EmbeddedResource>
-    <EmbeddedResource Include="UI\LifeForm.resx">
+    <EmbeddedResource Include="Pages\LifeForm.resx">
       <DependentUpon>LifeForm.cs</DependentUpon>
     </EmbeddedResource>
     <None Include="Properties\Settings.settings">

+ 5 - 4
DayOf1440/MainForm.cs

@@ -50,21 +50,21 @@ namespace DayOf1440
             int bai = result / 100 % 10;
             int qian = result / 1000 % 10;
             // 居中显示时间,加特效
-            if (result > 1000)
+            if (result >= 1000)
             {
                 pictureBox1.BackgroundImage = charNum[qian];
                 pictureBox2.BackgroundImage = charNum[bai];
                 pictureBox3.BackgroundImage = charNum[shi];
                 pictureBox4.BackgroundImage = charNum[ge];
             }
-            else if (result > 100) 
+            else if (result >= 100) 
             {
                 pictureBox1.BackgroundImage = null;
                 pictureBox2.BackgroundImage = charNum[bai];
                 pictureBox3.BackgroundImage = charNum[shi];
                 pictureBox4.BackgroundImage = charNum[ge];
             }
-            else if (result > 10)
+            else if (result >= 10)
             {
                 pictureBox1.BackgroundImage = null;
                 pictureBox2.BackgroundImage = charNum[shi];
@@ -108,7 +108,8 @@ namespace DayOf1440
 
         private void cms_life_Click(object sender, EventArgs e)
         {
-            Program.getInstance(typeof(LifeForm));
+            //跳转到 人生进度条 界面
+            Go(typeof(LifeForm));
         }
     }
 }

+ 95 - 0
DayOf1440/Pages/BaseForm.cs

@@ -0,0 +1,95 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Forms;
+
+namespace DayOf1440.UI
+{
+    /// <summary>
+    /// Form 基类
+    /// </summary>
+    public partial class BaseForm : Form
+    {
+        public BaseForm()
+        {
+            this.Load += new EventHandler(BaseForm_Load);
+            this.FormClosed += new FormClosedEventHandler(BaseForm_FormClosed);
+            InitializeComponent();
+        }
+        public void BaseForm_Load(object sender, EventArgs e)
+        {
+            Program.formList.Add(this);
+        }
+        public void BaseForm_FormClosed(object sender, FormClosedEventArgs e)
+        {
+            Program.formList.Remove(this);
+        }
+
+        private void InitializeComponent()
+        {
+            this.SuspendLayout();
+            // 
+            // BaseForm
+            // 
+            this.ClientSize = new System.Drawing.Size(1350, 729);
+            this.Name = "BaseForm";
+            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
+            this.DoubleClick += new System.EventHandler(this.BaseForm_DoubleClick);
+            this.ResumeLayout(false);
+
+        }
+        /// <summary>
+        /// 双击关闭窗体
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void BaseForm_DoubleClick(object sender, EventArgs e)
+        {
+            Application.Exit();
+        }
+        /// <summary>
+        /// 解决winform刚启动的时候,由于picture 太多闪屏问题
+        /// </summary>
+        protected override CreateParams CreateParams
+        {
+            get
+            {
+                CreateParams cp = base.CreateParams;
+                cp.ExStyle |= 0x02000000;
+                return cp;
+            }
+        }
+
+        /// <summary>
+        /// Form之间跳转
+        /// </summary>
+        /// <param name="type"></param>
+        /// <returns></returns>
+        internal static Form Go(Type type)
+        {
+            Form currentForm = null;
+            foreach (Form formItem in Program.formList)
+            {
+                if (formItem.GetType() == type)
+                {
+                    currentForm = formItem;
+                    currentForm.Activate();
+                    break;
+                }
+
+            }
+            if (currentForm == null)
+            {
+                object obj = Activator.CreateInstance(type);
+                if (obj is Form)
+                {
+                    currentForm = obj as Form;
+                    currentForm.Show();
+                }
+            }
+            return currentForm;
+        }
+    }
+}

+ 0 - 0
DayOf1440/UI/BaseForm.resx → DayOf1440/Pages/BaseForm.resx


+ 43 - 12
DayOf1440/UI/LifeForm.Designer.cs → DayOf1440/Pages/LifeForm.Designer.cs

@@ -38,8 +38,8 @@ namespace DayOf1440.UI
             this.progressBar1 = new System.Windows.Forms.ProgressBar();
             this.progressBar2 = new System.Windows.Forms.ProgressBar();
             this.progressBar3 = new System.Windows.Forms.ProgressBar();
+            this.progressBar4 = new System.Windows.Forms.ProgressBar();
             this.progressBar5 = new System.Windows.Forms.ProgressBar();
-            this.progressBar6 = new System.Windows.Forms.ProgressBar();
             this.timer1 = new System.Windows.Forms.Timer(this.components);
             this.panel1 = new System.Windows.Forms.Panel();
             this.label7 = new System.Windows.Forms.Label();
@@ -50,8 +50,11 @@ namespace DayOf1440.UI
             this.panel2 = new System.Windows.Forms.Panel();
             this.label4 = new System.Windows.Forms.Label();
             this.panel3 = new System.Windows.Forms.Panel();
+            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
+            this.tsm_home = new System.Windows.Forms.ToolStripMenuItem();
             this.panel1.SuspendLayout();
             this.panel2.SuspendLayout();
+            this.contextMenuStrip1.SuspendLayout();
             this.SuspendLayout();
             // 
             // label1
@@ -128,32 +131,39 @@ namespace DayOf1440.UI
             this.progressBar3.TabIndex = 8;
             this.progressBar3.Value = 51;
             // 
+            // progressBar4
+            // 
+            this.progressBar4.Location = new System.Drawing.Point(203, 375);
+            this.progressBar4.Name = "progressBar4";
+            this.progressBar4.Size = new System.Drawing.Size(514, 35);
+            this.progressBar4.TabIndex = 10;
+            this.progressBar4.Value = 51;
+            // 
             // progressBar5
             // 
-            this.progressBar5.Location = new System.Drawing.Point(203, 375);
+            this.progressBar5.Location = new System.Drawing.Point(203, 480);
             this.progressBar5.Name = "progressBar5";
             this.progressBar5.Size = new System.Drawing.Size(514, 35);
-            this.progressBar5.TabIndex = 10;
+            this.progressBar5.TabIndex = 11;
             this.progressBar5.Value = 51;
             // 
-            // progressBar6
+            // timer1
             // 
-            this.progressBar6.Location = new System.Drawing.Point(203, 480);
-            this.progressBar6.Name = "progressBar6";
-            this.progressBar6.Size = new System.Drawing.Size(514, 35);
-            this.progressBar6.TabIndex = 11;
-            this.progressBar6.Value = 51;
+            this.timer1.Enabled = true;
+            this.timer1.Interval = 5000;
+            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
             // 
             // panel1
             // 
+            this.panel1.ContextMenuStrip = this.contextMenuStrip1;
             this.panel1.Controls.Add(this.label7);
             this.panel1.Controls.Add(this.label8);
             this.panel1.Controls.Add(this.label9);
             this.panel1.Controls.Add(this.label10);
             this.panel1.Controls.Add(this.label11);
-            this.panel1.Controls.Add(this.progressBar6);
-            this.panel1.Controls.Add(this.progressBar1);
             this.panel1.Controls.Add(this.progressBar5);
+            this.panel1.Controls.Add(this.progressBar1);
+            this.panel1.Controls.Add(this.progressBar4);
             this.panel1.Controls.Add(this.label1);
             this.panel1.Controls.Add(this.progressBar3);
             this.panel1.Controls.Add(this.label2);
@@ -167,6 +177,7 @@ namespace DayOf1440.UI
             this.panel1.Name = "panel1";
             this.panel1.Size = new System.Drawing.Size(1166, 668);
             this.panel1.TabIndex = 12;
+            this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
             this.panel1.DoubleClick += new System.EventHandler(this.panel1_DoubleClick);
             // 
             // label7
@@ -221,6 +232,7 @@ namespace DayOf1440.UI
             // 
             // panel2
             // 
+            this.panel2.ContextMenuStrip = this.contextMenuStrip1;
             this.panel2.Controls.Add(this.label4);
             this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
             this.panel2.Location = new System.Drawing.Point(0, 0);
@@ -241,6 +253,7 @@ namespace DayOf1440.UI
             // 
             // panel3
             // 
+            this.panel3.ContextMenuStrip = this.contextMenuStrip1;
             this.panel3.Dock = System.Windows.Forms.DockStyle.Left;
             this.panel3.Location = new System.Drawing.Point(0, 100);
             this.panel3.Name = "panel3";
@@ -248,6 +261,20 @@ namespace DayOf1440.UI
             this.panel3.TabIndex = 14;
             this.panel3.DoubleClick += new System.EventHandler(this.panel3_DoubleClick);
             // 
+            // contextMenuStrip1
+            // 
+            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
+            this.tsm_home});
+            this.contextMenuStrip1.Name = "contextMenuStrip1";
+            this.contextMenuStrip1.Size = new System.Drawing.Size(101, 26);
+            // 
+            // tsm_home
+            // 
+            this.tsm_home.Name = "tsm_home";
+            this.tsm_home.Size = new System.Drawing.Size(100, 22);
+            this.tsm_home.Text = "首页";
+            this.tsm_home.Click += new System.EventHandler(this.tsm_home_Click);
+            // 
             // LifeForm
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
@@ -259,10 +286,12 @@ namespace DayOf1440.UI
             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
             this.Name = "LifeForm";
             this.Text = "LifeForm";
+            this.Load += new System.EventHandler(this.LifeForm_Load);
             this.panel1.ResumeLayout(false);
             this.panel1.PerformLayout();
             this.panel2.ResumeLayout(false);
             this.panel2.PerformLayout();
+            this.contextMenuStrip1.ResumeLayout(false);
             this.ResumeLayout(false);
 
         }
@@ -277,8 +306,8 @@ namespace DayOf1440.UI
         private System.Windows.Forms.ProgressBar progressBar1;
         private System.Windows.Forms.ProgressBar progressBar2;
         private System.Windows.Forms.ProgressBar progressBar3;
+        private System.Windows.Forms.ProgressBar progressBar4;
         private System.Windows.Forms.ProgressBar progressBar5;
-        private System.Windows.Forms.ProgressBar progressBar6;
         private System.Windows.Forms.Timer timer1;
         private System.Windows.Forms.Panel panel1;
         private System.Windows.Forms.Panel panel2;
@@ -289,5 +318,7 @@ namespace DayOf1440.UI
         private System.Windows.Forms.Label label9;
         private System.Windows.Forms.Label label10;
         private System.Windows.Forms.Label label11;
+        private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
+        private System.Windows.Forms.ToolStripMenuItem tsm_home;
     }
 }

+ 74 - 0
DayOf1440/Pages/LifeForm.cs

@@ -0,0 +1,74 @@
+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 DayOf1440.UI
+{
+    public partial class LifeForm : BaseForm
+    {
+        public LifeForm()
+        {
+            InitializeComponent();
+        }
+
+        private void panel1_DoubleClick(object sender, EventArgs e)
+        {
+            Application.Exit();
+
+        }
+
+        private void panel3_DoubleClick(object sender, EventArgs e)
+        {
+            Application.Exit();
+
+        }
+
+        private void panel2_DoubleClick(object sender, EventArgs e)
+        {
+            Application.Exit();
+
+        }
+
+        private void tsm_home_Click(object sender, EventArgs e)
+        {
+            Go(typeof(MainForm));
+        }
+
+        private void panel1_Paint(object sender, PaintEventArgs e)
+        {
+
+        }
+
+        private void LifeForm_Load(object sender, EventArgs e)
+        {
+
+        }
+
+        private void timer1_Tick(object sender, EventArgs e)
+        {
+            int tian = (int)((DateTime.Now.Subtract(DateTime.Today).TotalMinutes / 14.4));
+            progressBar1.Value = tian;//天
+            label7.Text = tian.ToString() +"%";
+
+            //int zhou = (int)(DateTime.Today.);
+            progressBar2.Value = 50;//周
+            label8.Text = tian.ToString() + "%";
+
+            progressBar3.Value = 50;//月
+            label9.Text = tian.ToString() + "%";
+
+            progressBar4.Value = 50;//年
+            label10.Text = tian.ToString() + "%";
+
+            progressBar4.Value = 50;//生
+            label11.Text = tian.ToString() + "%";
+
+        }
+    }
+}

+ 3 - 0
DayOf1440/UI/LifeForm.resx → DayOf1440/Pages/LifeForm.resx

@@ -120,4 +120,7 @@
   <metadata name="timer1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     <value>17, 17</value>
   </metadata>
+  <metadata name="contextMenuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+    <value>104, 17</value>
+  </metadata>
 </root>

+ 42 - 27
DayOf1440/Program.cs

@@ -1,6 +1,9 @@
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.Linq;
+using System.Reflection;
+using System.Runtime.InteropServices;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 
@@ -8,6 +11,9 @@ namespace DayOf1440
 {
     static class Program
     {
+        /// <summary>
+        ///  全局窗体List,便于相互跳转
+        /// </summary>
         public static List<Form> formList = new List<Form>();
 
         /// <summary>
@@ -16,40 +22,49 @@ namespace DayOf1440
         [STAThread]
         static void Main()
         {
-            Application.EnableVisualStyles();
-            Application.SetCompatibleTextRenderingDefault(false);
-            Application.Run(new MainForm());
-        }
-
-        /// <summary>
-        /// new a Form object
-        /// </summary>
-        /// <param name="type"></param>
-        /// <returns></returns>
-        internal static Form getInstance(Type type)
-        {
-            Form currentForm = null;
-            foreach (Form formItem in formList)
+            Process instance = RunningInstance();
+            if (instance == null)
             {
-                if (formItem.GetType() == type)
-                {
-                    currentForm = formItem;
-                    currentForm.Activate();
-                    break;
-                }
 
+                Application.EnableVisualStyles();
+                Application.SetCompatibleTextRenderingDefault(false);
+                Application.Run(new MainForm());
             }
-            if (currentForm == null)
+            else
             {
-                object obj = Activator.CreateInstance(type);
-                if (obj is Form)
+                // MessageBox.Show("程序已经在运行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
+            }
+        }
+
+        [DllImport("User32.dll", EntryPoint = "FindWindow")]
+        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
+
+        [DllImport("User32.dll")]
+        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
+
+        [DllImport("User32.dll")]
+        private static extern bool SetForegroundWindow(IntPtr hWnd);
+
+        public static Process RunningInstance()
+        {
+            // 获取当前活动的进程
+            Process current = Process.GetCurrentProcess();
+            // 获取当前本地计算机上指定的进程名称的所有进程
+            Process[] processes = Process.GetProcessesByName(current.ProcessName);
+            foreach (Process process in processes)
+            {
+                // 忽略当前进程
+                if (process.Id != current.Id)
                 {
-                    currentForm = obj as Form;
-                    currentForm.Show();
+                    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
+                    {
+                        return process;
+                    }
                 }
             }
-            return currentForm;
-        }
 
+            // 如果没有其他同名进程存在,则返回 null
+            return null;
+        }
     }
 }

+ 4 - 4
DayOf1440/Properties/AssemblyInfo.cs

@@ -6,11 +6,11 @@ using System.Runtime.InteropServices;
 // set of attributes. Change these attribute values to modify the information
 // associated with an assembly.
 [assembly: AssemblyTitle("DayOf1440")]
-[assembly: AssemblyDescription("")]
+[assembly: AssemblyDescription("tools")]
 [assembly: AssemblyConfiguration("")]
-[assembly: AssemblyCompany("")]
+[assembly: AssemblyCompany("ZhiZhou Information Technology lnc")]
 [assembly: AssemblyProduct("DayOf1440")]
-[assembly: AssemblyCopyright("Copyright ©  2022")]
+[assembly: AssemblyCopyright("Copyright Zhizhou©  2022")]
 [assembly: AssemblyTrademark("")]
 [assembly: AssemblyCulture("")]
 
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
 // You can specify all the values or you can default the Build and Revision Numbers
 // by using the '*' as shown below:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyVersion("1.1.0.0")]
 [assembly: AssemblyFileVersion("1.0.0.0")]

+ 0 - 49
DayOf1440/UI/BaseForm.cs

@@ -1,49 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using System.Windows.Forms;
-
-namespace DayOf1440.UI
-{
-    /// <summary>
-    /// Form 基类
-    /// </summary>
-    public partial class BaseForm : Form
-    {
-        public BaseForm()
-        {
-            this.Load += new EventHandler(BaseForm_Load);
-            this.FormClosed += new FormClosedEventHandler(BaseForm_FormClosed);
-            InitializeComponent();
-        }
-        public void BaseForm_Load(object sender, EventArgs e)
-        {
-            Program.formList.Add(this);
-        }
-        public void BaseForm_FormClosed(object sender, FormClosedEventArgs e)
-        {
-            Program.formList.Remove(this);
-        }
-
-        private void InitializeComponent()
-        {
-            this.SuspendLayout();
-            // 
-            // BaseForm
-            // 
-            this.ClientSize = new System.Drawing.Size(1350, 729);
-            this.Name = "BaseForm";
-            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
-            this.DoubleClick += new System.EventHandler(this.BaseForm_DoubleClick);
-            this.ResumeLayout(false);
-
-        }
-
-        private void BaseForm_DoubleClick(object sender, EventArgs e)
-        {
-            Application.Exit();
-        }
-    }
-}

+ 0 - 38
DayOf1440/UI/LifeForm.cs

@@ -1,38 +0,0 @@
-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 DayOf1440.UI
-{
-    public partial class LifeForm : BaseForm
-    {
-        public LifeForm()
-        {
-            InitializeComponent();
-        }
-
-        private void panel1_DoubleClick(object sender, EventArgs e)
-        {
-            Application.Exit();
-
-        }
-
-        private void panel3_DoubleClick(object sender, EventArgs e)
-        {
-            Application.Exit();
-
-        }
-
-        private void panel2_DoubleClick(object sender, EventArgs e)
-        {
-            Application.Exit();
-
-        }
-    }
-}