Skip to content

Commit

Permalink
Prevent app window showing on first run
Browse files Browse the repository at this point in the history
fahminlb33 committed Jun 11, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 47fecd9 commit c63edc4
Showing 4 changed files with 132 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/KFmaintenance/KFmaintenance.csproj
Original file line number Diff line number Diff line change
@@ -90,6 +90,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Services\AppModulesInstaller.cs" />
<Compile Include="Services\CustomApplicationContext.cs" />
<Compile Include="Services\FormService.cs" />
<Compile Include="Services\WindowsHelpers.cs" />
<Compile Include="Views\AboutForm.cs">
4 changes: 2 additions & 2 deletions src/KFmaintenance/Program.cs
Original file line number Diff line number Diff line change
@@ -38,13 +38,13 @@ static void Main()
// install modules
Container.Install(new AppModulesInstaller());

// enable TLS
// enable TLS;
Helpers.EnableTls();

// bootstrap
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new StartupForm());
Application.Run(new CustomApplicationContext(new StartupForm()));
}
else
{
80 changes: 80 additions & 0 deletions src/KFmaintenance/Services/CustomApplicationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KFmaintenance.Services
{
public class CustomApplicationContext : ApplicationContext
{
/// <summary>
/// The main application form.
/// </summary>
private Form _mainForm;

/// <summary>
/// Initializes a new instance of the <see cref="CustomApplicationContext"/> class.
/// </summary>
/// <param name="mainForm">The main form of the application.</param>
public CustomApplicationContext(Form mainForm)
{
_mainForm = mainForm;

if (_mainForm != null)
{
// Wire up the destroy events similar to how the base ApplicationContext
// does things when a form is provided.
_mainForm.HandleDestroyed += OnFormDestroy;

// We still want to call Show() here, but we can at least hide it from the user
// by setting Opacity to 0 while the form is being shown for the first time.
_mainForm.Opacity = 0;
_mainForm.Show();
_mainForm.Hide();
_mainForm.Opacity = 1;
}
}

/// <summary>
/// Handles the <see cref="Control.HandleDestroyed"/> event.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">An <see cref="EventArgs"/> that contains the event data.</param>
private void OnFormDestroy(object sender, EventArgs e)
{
if (sender is Form form && !form.RecreatingHandle)
{
form.HandleDestroyed -= OnFormDestroy;
OnMainFormClosed(sender, e);
}
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing,
/// or resetting unmanaged resources.
/// </summary>
/// <param name="disposing">
/// true if invoked from the <see cref="IDisposable.Dispose"/> method;
/// false if invoked from the finalizer.
/// </param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (_mainForm != null)
{
if (!_mainForm.IsDisposed)
{
_mainForm.Dispose();
}

_mainForm = null;
}
}

base.Dispose(disposing);
}
}
}
50 changes: 49 additions & 1 deletion src/KFmaintenance/Services/WindowsHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,60 @@
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace KFmaintenance.Services
{
internal static class WindowsHelpers
public static class WindowsHelpers
{
public static void Shutdown()
{
Process.Start("shutdown -s -t 0");
}

public static void ApplyStyle(this DataGridView dgv)
{
var font = new Font("Segoe UI", 9.87F, FontStyle.Regular, GraphicsUnit.Point, 0);

// globals
dgv.Font = font;
dgv.BackColor = Color.FromArgb(45, 47, 49);
dgv.ForeColor = Color.White;
dgv.BorderStyle = BorderStyle.None;
dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

// column header
dgv.EnableHeadersVisualStyles = false;
dgv.ColumnHeadersHeight = 50;
dgv.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;

// rows
dgv.RowHeadersVisible = false;
dgv.RowTemplate.Height = 25;
dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
dgv.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;

// cell style
dgv.ColumnHeadersDefaultCellStyle = new DataGridViewCellStyle
{
Font = font,
ForeColor = Color.White,
BackColor = Color.FromArgb(45, 47, 49),
Alignment = DataGridViewContentAlignment.MiddleCenter
};

dgv.DefaultCellStyle = new DataGridViewCellStyle
{
BackColor = Color.FromArgb(60, 70, 73),
SelectionBackColor = Color.FromArgb(35, 168, 109),
SelectionForeColor = Color.White
};

dgv.AlternatingRowsDefaultCellStyle = new DataGridViewCellStyle
{
BackColor = Color.SlateGray
};
}
}
}

0 comments on commit c63edc4

Please sign in to comment.