C#: Get complete desktop size?

前端 未结 9 2135
梦谈多话
梦谈多话 2020-11-27 16:40

How do I find out the size of the entire desktop? Not the \"working area\" and not the \"screen resolution\", both of which refer to only o

9条回答
  •  感动是毒
    2020-11-27 16:52

    This doesn't answer the question, but merely adds additional insight on a window's Point (location) within all the screens).

    Use the code below to find out if a Point (e.g. last known Location of window) is within the bounds of the overall Desktop. If not, reset the window's Location to the default pBaseLoc;

    Code does not account for the TaskBar or other Toolbars, yer on yer own there.

    Example Use: Save the Window location to a database from station A. User logs into station B with 2 monitors and moves the window to the 2nd monitor, logs out saving new location. Back to station A and the window wouldn't be shown unless the above code is used.

    My further resolve implemented saving the userID and station's IP (& winLoc) to database or local user prefs file for a given app, then load in user pref for that station & app.

    Point pBaseLoc = new Point(40, 40)
    int x = -500, y = 140;
    Point pLoc = new Point(x, y);
    bool bIsInsideBounds = false;
    
    foreach (Screen s in Screen.AllScreens)
    {
        bIsInsideBounds = s.Bounds.Contains(pLoc);
        if (bIsInsideBounds) { break; }
    }//foreach (Screen s in Screen.AllScreens)
    
    if (!bIsInsideBounds) { pLoc = pBaseLoc;  }
    
    this.Location = pLoc;
    

提交回复
热议问题