Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Centering MessageBox, Common DialogBox or Form on applications

, 30 Mar 2005
Simple component to center any MessageBox, Form or CommonDialog on applications.
centerdialog_demo.zip
DialogBox.dll
TestApplication.exe
centerdialog_src.zip
CodeProject
DialogBox
DialogBox.csproj.user
TestApplication
App.ico
bin
Debug
DialogBox.dll
TestApplication.csproj.user
//=============================================================================
// COPYRIGHT: Prosoft-Lanz
//=============================================================================
//
// $Workfile: Win32API.cs $
//
// PROJECT : CodeProject Components
// VERSION : 1.00
// CREATION : 19.02.2003
// AUTHOR : JCL
//
// DETAILS : This class implement Win32 API calls
//           and the contants used for these calls.
//
//-----------------------------------------------------------------------------
using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CodeProject.Win32API
{
	///////////////////////////////////////////////////////////////////////
	#region Generic declarations

	/// <summary>
	/// Rectangle parameters exposed as a structure.
	/// </summary>
	public struct RECT
	{
		/// <summary>
		/// Rectangle members.
		/// </summary>
		public int left, top, right, bottom;
	}

	#endregion

	///////////////////////////////////////////////////////////////////////
	#region Util class

	/// <summary>
	/// Utility functions.
	/// </summary>
	public sealed class API
	{
		private API() {}	// To remove the constructor from the documentation!

		/// <summary>
		/// Get true multiscreen size.
		/// </summary>
		public static Rectangle TrueScreenRect
		{
			get
			{
				// get the biggest screen area
				Rectangle rectScreen = Screen.PrimaryScreen.WorkingArea;
				int left = rectScreen.Left;
				int top = rectScreen.Top;
				int right = rectScreen.Right;
				int bottom = rectScreen.Bottom;
				foreach (Screen screen in Screen.AllScreens)
				{
					left = Math.Min(left, screen.WorkingArea.Left);
					right = Math.Max(right, screen.WorkingArea.Right);
					top = Math.Min(top, screen.WorkingArea.Top);
					bottom = Math.Max(bottom, screen.WorkingArea.Bottom);
				}
				return new Rectangle(left, top, right-left, bottom-top);
			}
		}
	}

	#endregion

	///////////////////////////////////////////////////////////////////////
	#region USER32 class

	/// <summary>
	/// Class to expose USER32 API functions.
	/// </summary>
	public sealed class USER32
	{
		private USER32() {}	// To remove the constructor from the documentation!

		[DllImport("user32", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
		internal static extern int GetWindowRect(IntPtr hWnd, ref RECT rect);

		[DllImport("user32", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
		internal static extern int MoveWindow(IntPtr hWnd, int x, int y, int w, int h, int repaint);

		[DllImport("user32", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
		internal static extern IntPtr GetActiveWindow();

		[DllImport("user32", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
		internal static extern int GetClassName(IntPtr hwnd, StringBuilder lpClassName, int nMaxCount);
	}
	#endregion
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Jean-Claude Lanz
Web Developer
Switzerland Switzerland
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 30 Mar 2005
Article Copyright 2005 by Jean-Claude Lanz
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid