Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#11971 Enhance WinForms Application static class to get current application context #11972

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/System.Windows.Forms/src/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2265,6 +2265,7 @@ static System.Windows.Forms.Application.ProductVersion.get -> string!
static System.Windows.Forms.Application.RaiseIdle(System.EventArgs! e) -> void
static System.Windows.Forms.Application.RegisterMessageLoop(System.Windows.Forms.Application.MessageLoopCallback? callback) -> void
static System.Windows.Forms.Application.RemoveMessageFilter(System.Windows.Forms.IMessageFilter! value) -> void
static System.Windows.Forms.Application.CurrentMainForm.get -> System.Windows.Forms.Form?
static System.Windows.Forms.Application.RenderWithVisualStyles.get -> bool
static System.Windows.Forms.Application.Restart() -> void
static System.Windows.Forms.Application.Run() -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ public static InputLanguage CurrentInputLanguage
set => InputLanguage.CurrentInputLanguage = value;
}

/// <summary>
/// Get current application context's main form
/// </summary>
/// <returns>Current application context's main form</returns>
public static Form? CurrentMainForm { get => ThreadContext.FromCurrent().ApplicationContext?.MainForm; }

internal static bool CustomThreadExceptionHandlerAttached
=> ThreadContext.FromCurrent().CustomThreadExceptionHandlerAttached;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,50 @@ public void Application_CurrentCulture_Set_GetReturnsExpected()
}).Dispose();
}

[STAThread]
[WinFormsFact]
public void Application_CurrentMainForm_Get()
{
Application.EnableVisualStyles();
var form1 = new Form();
var form2 = new Form();
var event1 = new ManualResetEventSlim();
var event2 = new ManualResetEventSlim();
Form mainForm1 = default, mainForm2 = default, mainForm3 = default, mainForm4 = default;
form1.Load += (s, e) =>
{
mainForm1 = Application.CurrentMainForm;
form1.Close();
event1.Set();
};
form2.Load += (s, e) =>
{
mainForm2 = Application.CurrentMainForm;
form2.Close();
event2.Set();
};
var thread1 = new Thread(() => Application.Run(form1));
var thread2 = new Thread(() => Application.Run(form2));
var thread3 = new Thread(() => mainForm3 = Application.CurrentMainForm);
var thread4 = new Thread(() => mainForm4 = Application.CurrentMainForm);
thread1.SetApartmentState(ApartmentState.STA);
thread2.SetApartmentState(ApartmentState.STA);
thread3.SetApartmentState(ApartmentState.STA);
thread1.Start();
thread2.Start();
thread3.Start();
thread4.Start();
event1.Wait();
event2.Wait();
Assert.NotNull(mainForm1);
Assert.NotNull(mainForm2);
Assert.Null(mainForm3);
Assert.Null(mainForm4);
Assert.NotSame(mainForm1, mainForm2);
Assert.Same(form1, mainForm1);
Assert.Same(form2, mainForm2);
}

[WinFormsFact]
public void Application_CurrentCulture_SetNull_ThrowsArgumentNullException()
{
Expand Down Expand Up @@ -327,9 +371,9 @@ public void Application_SetDefaultFont_NonSystemFont()

// Retrieve the text scale factor, which is set via Settings > Display > Make Text Bigger.
using RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Accessibility");
int textScale = (int)(key?.GetValue("TextScaleFactor", 100) ?? 100);
int textScale = (int)(key?.GetValue("TextScaleFactor", 100) ?? 100);
if (textScale == 100) // Application.DefaultFont must be the same
{
{
font.Should().BeNull("Because TextScaleFactor == 100.");
Application.DefaultFont.Should().BeSameAs(customFont, "Because TextScaleFactor == 100.");
}
Expand Down