#region Apache License 2.0
/*
Nuclex .NET Framework
Copyright (C) 2002-2024 Markus Ewald / Nuclex Development Labs
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#endregion // Apache License 2.0
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace Nuclex.Windows.Forms {
///
/// Proxy stand-in to delay checking for the main window until it has been created
///
///
///
/// The issue: when the view model for the main window is created, the main window
/// may only exist as a .NET object, without the underlying operating system window
/// (done in checkable via
/// ). Not only will things
/// like fail, we can't
/// even locate the main window at that stage.
///
///
/// Thus, if the main window cannot be found at the time a view model is created,
/// this late-checking synchronizer will jump into its place and re-check for
/// the main window only when something needs to be executed in the UI thread.
///
///
class LateCheckedSynchronizer : ISynchronizeInvoke {
/// Initializes a new late-checked main window synchronizer
///
public LateCheckedSynchronizer(Action uiContextFoundCallback) {
this.uiContextFoundCallback = uiContextFoundCallback;
}
/// Finds the application's main window
/// Main window of the application or null if none has been created
///
/// The application's main window, if it has been created yet
///
public static Form GetMainWindow() {
IntPtr mainWindowHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
// We can get two things: a list of all open windows and the handle of
// the window that the process has registered as main window. Use the latter
// to pick the correct window from the former.
FormCollection openForms = Application.OpenForms;
int openFormCount = openForms.Count;
for(int index = 0; index < openFormCount; ++index) {
Form form = openForms[index];
IntPtr handle;
if(form.InvokeRequired) {
handle = (IntPtr)form.Invoke(new Func