Click here to Skip to main content
13,552,835 members
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 8 Oct 2012
Licenced CPOL

Position a Windows Forms MessageBox in C#

, 6 Apr 2013
Rate this:
Please Sign up or sign in to vote.
A tip about how to set the position of a Windows Forms MessageBox in C#


There's no method in C# to position a MessageBox. But in C++, you can position a message box by finding the message box and moving it. In this tip, I'll tell you how to position a MessageBox in C#.

Using the Code

At the top of your code file, add these using namespace statements:

using System.Runtime.InteropServices;
using System.Threading;

In your Form class, add these DllImport attributes:

static extern IntPtr FindWindow(IntPtr classname, string title); // extern method: FindWindow

static extern void MoveWindow(IntPtr hwnd, int X, int Y, 
	int nWidth, int nHeight, bool rePaint); // extern method: MoveWindow

static extern bool GetWindowRect
	(IntPtr hwnd, out Rectangle rect); // extern method: GetWindowRect

The DllImportAttribute class is available in each .NET version.
Then, you need to find and move the MessageBox. To do that, use this code:  

void FindAndMoveMsgBox(int x, int y, bool repaint, string title)
    Thread thr = new Thread(() => // create a new thread
        IntPtr msgBox = IntPtr.Zero;
        // while there's no MessageBox, FindWindow returns IntPtr.Zero
        while ((msgBox = FindWindow(IntPtr.Zero, title)) == IntPtr.Zero) ;
        // after the while loop, msgBox is the handle of your MessageBox
        Rectangle r = new Rectangle();
        GetWindowRect(msgBox, out r); // Gets the rectangle of the message box
        MoveWindow(msgBox /* handle of the message box */, x , y, 
           r.Width - r.X /* width of originally message box */, 
           r.Height - r.Y /* height of originally message box */, 
           repaint /* if true, the message box repaints */);
    thr.Start(); // starts the thread

Call this method before you call MessageBox.Show. When you call MessageBox.Show, be sure the caption parameter isn't empty, because the title parameter must be equal to the caption parameter.

For example:

FindAndMoveMsgBox(0, 0, true,"Title");


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

I'm a hobbyist programmer. I work mostly in C#, JavaScript, and Python. I also like chess; you can find me on Lichess.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
TechnoGeek0017-Apr-13 21:19
memberTechnoGeek0017-Apr-13 21:19 
GeneralMy vote of 5 Pin
Gun Gun Febrianza24-Oct-12 5:37
member Gun Gun Febrianza24-Oct-12 5:37 
GeneralMy vote of 5 Pin
DrABELL9-Oct-12 7:30
memberDrABELL9-Oct-12 7:30 
GeneralMy vote of 5 Pin
hoernchenmeister9-Oct-12 5:16
memberhoernchenmeister9-Oct-12 5:16 
GeneralMy vote of 5 Pin
Carsten V2.08-Oct-12 18:36
memberCarsten V2.08-Oct-12 18:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180515.1 | Last Updated 7 Apr 2013
Article Copyright 2012 by Thomas D [ProgramFOX]
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid