开发者

How do I pass a reference type to Control.Invoke / display a form centered on the main form

开发者 https://www.devze.com 2023-01-01 20:48 出处:网络
I\'m on a thread other than the UI thread and need to display a modal form that\'s centered on the application\'s main form. What I usually do is use the width and height of the main form and the moda

I'm on a thread other than the UI thread and need to display a modal form that's centered on the application's main form. What I usually do is use the width and height of the main form and the modal form to calculate the location, then use the PointToScreen method of the main form to get the location of the modal form. Since I'm on another thread I need to use Control.Invoke to call this method. I just can't figure out how to pass a parameter of type Point to Control.Invoke (params object[]). Value types and String works fine.

Or, if someone can find a better way to display a form centered on the main form regardless of thread, that would be great. MessageBox seems to be able 开发者_如何学编程to do this (although not modally).

EDIT

I have no problem calling something like this.

mainform.Invoke(..., object[] { someString });

The problem is this:

public Point Control.PointToScreen(Point p)

This method takes a Point as a parameter and returns a Point. How can I call this via Control.Invoke?


myWhatever.Invoke(myDelegate, new object[] { myParam1, myParam2, myParam3 });


Take a look at this, you can adapt the function UpdateStatusBarMessage_OnNewStatusMessage, to display your form instead of writing a message.


WRT "a better way to display a form centered on the main form regardless of thread, that would be great" - you indeed don't need to do this centering manually. :)

  1. Before displaying (calling ShowDialog, presumably), set the StartPosition property on the child dialog to CenterParent.
  2. In the case the active window (has the button that creates the dialog) isn't the intended parent (sounds unlikely given your scenario), you can call the ShowDialog overload that lets you specify the parent. I don't think you'll need this, but this lets you parent the dialog somewhat it arbitrarily. :)
0

精彩评论

暂无评论...
验证码 换一张
取 消