Problems and solutions encountered in the preparation of small features winform

Small summary of problems and solutions encountered when writing winform.

1, label tag size can not be set

  A: The autoSize attribute label label was changed to false.

  

  Reference: https: //zhidao.baidu.com/question/335396798.html

 

2, how to prevent the program several times to open (ie: to limit the program can open only one)

  A: Add the following code to Program.cs at the entrance to the program. 

static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool ret;
            System.Threading.Mutex mutex = new System.Threading.Mutex(true,Application.ProductName,out ret);
            if (ret)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run ( new new Form1 ()); 
                mutex.ReleaseMutex ();    // release once 
            }
             the else { 
                MessageBox.Show ( " program is already running! " , " Prompt " ); 
                Application.Exit (); 
            } 

        } 
    }

 

3, set up after clicking the close button does not close, but minimized to the taskbar.

  ① The first step: Rewrite the closing event of the window, click the Close button after setting, but the window is minimized, thus achieving a close button instead of closing the window function.

        //窗口关闭事件
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.WindowState = FormWindowState.Minimized;
            this.Visible = false;
        }

 

4, then minimize the window how to maximize the open window?

  ① The first step: add a notifyIcon control

  

  ② Step two: add icons to notifyIcon control properties, set an icon.

  

 

  ③ The third step: to notifyIcon control to add a double-click event, you can open the window normal after such a double-click.

 //图标双击事件
        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.ShowInTaskbar = false;
                this.Visible = true;
            }
        }

 

5, how close the program do?

  A: The only be closed by the Task Manager. Press ctrl + shift + esc to open the Task Manager, find the appropriate program, and then the end of the process can be.

 

6, after the shutdown process, the tray icon will display the program until after the move the mouse to the icon will disappear. So how is the process of program icons disappear immediately after closing it?

  A: uh ~ like no way ....

Guess you like

Origin www.cnblogs.com/masha2017/p/11123195.html