Move Form onto specified Screen

ぐ巨炮叔叔 提交于 2019-12-06 22:50:08

问题


I am trying to figure out how to move specified System.Windows.Forms.Form onto another than primary screen. I have ComboBox with list of available screens where user selects whichever screen he likes and my application is supposed to move one of its windows onto that screen.

I have only one screen on my laptop and no external monitor, so ComboBox on my computer offers only one option. I think minimalising desired window, moving it's left corner in the center of selected screen's Bounds and maximilising would do the job, right? I just can't test it. Is this a good way to go?

Thanks in advance!


回答1:


Here's what I did, as a simple test...

I added a simple wrapper class so that I could change what happens on the ToString call (I only wanted to see the name listed in the combo box)

private class ScreenObj
{
    public Screen screen = null;

    public ScreenObj(Screen scr)
    {
        screen = scr;
    }

    public override string ToString()
    {
        return screen.DeviceName;
    }
}

In the form load event I added this:

foreach(Screen screen in Screen.AllScreens)
{
     cboScreens.Items.Add(new ScreenObj(screen));
}

And for the selected index change event of the combo box I had this:

private void cboScreens_SelectedIndexChanged(object sender, EventArgs e)
{
    object o = cboScreens.SelectedItem;
    if(null == o)
        return;

    ScreenObj scrObj = o as ScreenObj;
    if(null == scrObj)
        return;

    Point p = new Point();

    p.X = scrObj.screen.WorkingArea.Left;
    p.Y = scrObj.screen.WorkingArea.Top;

    this.Location = p;
}

It moved the form to the upper left hand corner of each of my screens.



来源:https://stackoverflow.com/questions/8420203/move-form-onto-specified-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!