Hello everybody,
My MessageBox displayed the message, but behind other windows, and with the mouse cursor in the middle of the message window instead of the middle of the button.
Furthermore, after I positioned the mouse cursor, the previous position was still visible, so I saw two mouse cursors on the screen.
So, I corrected that, it works good, but I wonder whether a much more simple solution does not exist.
And also, to evaluate the correct position of the cursor, on WinForms you have Control.PointToScreen, but I am not sure that can be used on a MessageBox. I put values hardcoded, that is not sure to adapt if I change the sizes of the fonts in Windows.
The timer is also used at another place, where I do not use messageForm to manage the TopMost attribute. It some of works, but the MessageBox appears behind the calling form. I found a solution by hiding the calling form until the user has validated the message.
I repeat the title of the MessageBox at the top of the message, as during several months my Windows did not show the titles of the messages, either in C# or in Javascript. Having it repeated looks strange but works, if it is not displayed at all you miss some information.
My MessageBox displayed the message, but behind other windows, and with the mouse cursor in the middle of the message window instead of the middle of the button.
Furthermore, after I positioned the mouse cursor, the previous position was still visible, so I saw two mouse cursors on the screen.
So, I corrected that, it works good, but I wonder whether a much more simple solution does not exist.
And also, to evaluate the correct position of the cursor, on WinForms you have Control.PointToScreen, but I am not sure that can be used on a MessageBox. I put values hardcoded, that is not sure to adapt if I change the sizes of the fonts in Windows.
Manage a MessageBox:
private void Button_Click(Object sender, EventArgs e)
{
if (fic.FullName.Equals(strPath))
{
bFicPresent = true;
Form messageForm = new Form();
messageForm.TopMost = true;
strWindowToPosition = "File already present";
timer2.Enabled = true;
MessageBox.Show(messageForm, String.Format("{1} :\n{0}", fic.FullName, strWindowToPosition), strWindowToPosition);
messageForm.Close();
break;
}
}
string strWindowToPosition;
private void timer2_Tick(object sender, EventArgs e)
{
timer2.Enabled = false;
//System.Threading.Thread.Sleep(1000);
RECT rect = new RECT();
IntPtr hWnd = FindWindow(null, strWindowToPosition);
SetForegroundWindow(hWnd);
SetActiveWindow(hWnd);
GetWindowRect(hWnd, out rect);
int x = rect.Right - (btnShortcut.Width + 60);
int y = rect.Bottom - 70;
//lblInfo.Text = x.ToString();
SetCursorPos(x, y);
#region mask old position of cursor
for (int i = 0; i < 10; i++)
{
System.Threading.Thread.Sleep(100);
SetCursorPos(x--, y--);
}
#endregion
}
The timer is also used at another place, where I do not use messageForm to manage the TopMost attribute. It some of works, but the MessageBox appears behind the calling form. I found a solution by hiding the calling form until the user has validated the message.
I repeat the title of the MessageBox at the top of the message, as during several months my Windows did not show the titles of the messages, either in C# or in Javascript. Having it repeated looks strange but works, if it is not displayed at all you miss some information.