How do I change the MessageBox location?

前端 未结 5 1010
醉梦人生
醉梦人生 2020-12-11 01:22

I need to change the message box location. I don\'t want it to be in the center of the page.

MessageBox.Show(\"Hello\");
相关标签:
5条回答
  • 2020-12-11 01:55

    There is a way to change the location, but its way too complicated for such a small task.
    If you really need to change its location, you could display it, then use GetForegroundWindow to get a window handle, then MoveWindow to your desired location.
    But, as I already mensioned, this is way too complicated.
    Just create your own form with a label on it an a "OK" button. Set the button as the default window button, and then, in Form1 do MyWndName.ShowDialog();

    0 讨论(0)
  • 2020-12-11 01:59

    You will need to create a new form that inherits from the MessageBox form. That is the only way to access the position properties.

    0 讨论(0)
  • 2020-12-11 02:06

    What you can do is to create a new window, set the property AllowsTransparency to true and set the Background to Transparent. In that window you can put a TextBlock, or a label and also add Yes/No Buttons. Set the location of this window using Canvs.SetTop(Window,TopPosition) and Canvas.SetLeft(Window,LeftPosition). next, call the window with the method Show() or ShowDialog().

    0 讨论(0)
  • 2020-12-11 02:09

    Normally, you can't change startup location of standard message box.

    Solutions for your question:

    • Create your own custom message box. There is example of creation on CodeProject.
    • Complicated way using Windows Hook Procedure (WinAPI).
    0 讨论(0)
  • 2020-12-11 02:15

    Since I already use AutoIt for several other tasks in my project so I just create another thread to move the Message box

    using System.Threading;
    using AutoIt;
    //Namespace, class, function stuffs
    //New thread BEFORE create message box - safety measure
    Thread autoItThread = new Thread(delegate ()
                    {
                        AutoItX.WinWait("New Message box");
                        AutoItX.WinMove("New Message box", "This box will be moved", 400, 300);
                    });
                    autoItThread.Start();
    MessageBox.Show("This box will be moved", "New Message box");
    

    Please note

    • The coordinate 400,300 is absolute. 0,0 will be top left corner.
    • This is screen-dependent. If you want to be exact, other code to determine location is needed
    • This task is to change the absolute position of the Message box rather than move it.
    • How to get/install AutoIt is not addressed here. Please look for instruction on that if you need to.
    0 讨论(0)
提交回复
热议问题