Reputation:
I want to change the location of the messagebox in my c# application for visual studio 2013. I found this article:
http://www.codeproject.com/Tips/472294/Position-a-Windows-Forms-MessageBox-in-Csharp
It says "In your Form class, add these DllImport attributes."
What does this actually need me to do? I went to my System.Windows.Forms reference. If that's where I need to be do add this code, I have no idea where it needs to be added within there as there is a lot of stuff going on that I have no idea about.
Upvotes: 0
Views: 552
Reputation: 1411
Import the below name spaces
using System.Runtime.InteropServices;
using System.Threading;
Write the below code in class level (If you want information regrading these methods please refer pinvoke
[DllImport("user32.dll")]
static extern IntPtr FindWindow(IntPtr classname, string title);
[DllImport("user32.dll")]
static extern void MoveWindow(IntPtr hwnd, int X, int Y,int nWidth, int nHeight, bool rePaint);
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hwnd, out Rectangle rect);
Write the FindAndMoveMsgBox
method and call wherever you want
Here I called the method in Form1 constructor and below is the final code
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern IntPtr FindWindow(IntPtr classname, string title);
[DllImport("user32.dll")]
static extern void MoveWindow(IntPtr hwnd, int X, int Y,int nWidth, int nHeight, bool rePaint);
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hwnd, out Rectangle rect);
public Form1()
{
InitializeComponent();
FindAndMoveMsgBox(0, 0, true, "Title");
MessageBox.Show("Message", "Title");
}
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
}
}
}
Upvotes: 4
Reputation: 514
(See answer above - posted while I was typing the code up :) ) What 'DLLImport' does is allows you to call functions in unmanaged code from managed code. This is called Platform Invocation Services (or PInvoke)
Before making use of PInvoke services, I would urge you to familiarise yourself with PInvoke and how it works. PInvoke is great and I make use of it a lot for OS manipulation such as the link you posted.
Dated, but still a good PInvoke tutoriual: http://msdn.microsoft.com/en-us/library/aa288468(v=vs.71).aspx
To answer the question: Add those lines of code at the top of the Form.cs file (In the class reference)
public partial class MyForm : Form
{
[DllImport("user32.dll")]
static extern IntPtr FindWindow(IntPtr classname, string title); // extern method: FindWindow
[DllImport("user32.dll")]
static extern void MoveWindow(IntPtr hwnd, int X, int Y,
int nWidth, int nHeight, bool rePaint); // extern method: MoveWindow
[DllImport("user32.dll")]
static extern bool GetWindowRect
(IntPtr hwnd, out Rectangle rect); // extern method: GetWindowRect
//ETC
Upvotes: 0