Monday, January 28, 2019

C#: Hide WinForms main window

Can be used for any Windows application.

namespace Vurdalakov
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public partial class MainForm : Form
    {
        public MainForm()
        {
            this.InitializeComponent();
        }

        private void MainForm_Load(Object sender, EventArgs e)
        {
            this.HideMainForm();
        }

        private void HideMainForm()
        {
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            this.StartPosition = FormStartPosition.Manual;

            var x = 0;
            var y = 0;

            foreach (var screen in Screen.AllScreens)
            {
                var bounds = screen.Bounds;
                x = Math.Min(x, bounds.Left);
                y = Math.Min(y, bounds.Bottom);
            }

            this.Location = new Point(x - this.Width - 100, y - this.Height - 100);
        }
    }
}

No comments:

Post a Comment