Help Please; Form positioning
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Help Please; Form positioning

  1. #1
    Join Date
    Feb 2011
    Posts
    13

    Help Please; Form positioning

    Looking for some help on Form positioning;

    How would I code the following?

    On Form Exit;
    -Save new form position.

    On Form Load;
    -Check to see if the form was saved on a multiple monitor setup
    - is the monitor present, -if true
    -is the form visible
    -if true, do nothing.
    -if false, reset to center of that screen
    - is the monitor present, -if false
    -reset to center screen of primary monitor
    -is the form visible
    -if true, do nothing.
    -if false, reset to center of that screen

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,485

    Re: Help Please; Form positioning

    To save and reload settings, use the Settings class.

    To find out multi-mon details, use the Screen class. Find out details and code snippets in msdn (http://msdn.microsoft.com).

  3. #3
    Join Date
    Feb 2011
    Posts
    13

    Re: Help Please; Form positioning

    Thanks Arjay

    Almost as helpful as Google.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,485

    Re: Help Please; Form positioning

    Quote Originally Posted by SteveyD View Post
    Thanks Arjay

    Almost as helpful as Google.
    You're welcome, except with google you don't have any idea of what to search for. I've given you the two key classes for solving the problem: saving/loading the data and how to determine whether you're running on multi-mon.

    I suggest starting with learning how to use the settings class and write a simple windows form app that positions the form on startup based on the last position it was saved before closing. You'll want to override the OnLoad and OnClose events.

    Once you tackle that part of the problem, then use the Screen class to detect the multi-mon part of the problem.

    Feel free to work with us on the site and post additional questions. As always any positive feedback that you may have to the volunteers on this site are always appreciated.

  5. #5
    Join Date
    Feb 2011
    Posts
    13

    Re: Help Please; Form positioning

    I found and implemented the following code to deal with form location, now I need to solve the issue of multiple monitor and whether the form was closed on a screen that is not currently present.

    using Settings.settings
    WindowGeometry
    WindowGeometryOpen
    WindowGeometrySearch

    as type String
    as scope User


    Code:
    public TextWindow()
    {
        // TextWindow is this form's name.
        // Your form will have a different name most likely.
        InitializeComponent();
        GeometryFromString(Properties.Settings.Default.WindowGeometry, this);
    }
    
    void TextWindow_FormClosing(object sender, FormClosingEventArgs e)
    {
        // persist our geometry string.
        Properties.Settings.Default.WindowGeometry = GeometryToString(this);
        Properties.Settings.Default.Save();
    }
    
    public static void GeometryFromString(string thisWindowGeometry, Form formIn)
    {
        if (string.IsNullOrEmpty(thisWindowGeometry) == true)
        {
            return;
        }
        string[] numbers = thisWindowGeometry.Split('|');
        string windowString = numbers[4];
        if (windowString == "Normal")
        {
            Point windowPoint = new Point(int.Parse(numbers[0]),
                int.Parse(numbers[1]));
            Size windowSize = new Size(int.Parse(numbers[2]),
                int.Parse(numbers[3]));
    
            bool locOkay = GeometryIsBizarreLocation(windowPoint, windowSize);
            bool sizeOkay = GeometryIsBizarreSize(windowSize);
    
            if (locOkay == true && sizeOkay == true)
            {
                formIn.Location = windowPoint;
                formIn.Size = windowSize;
                formIn.StartPosition = FormStartPosition.Manual;
                formIn.WindowState = FormWindowState.Normal;
            }
            else if (sizeOkay == true)
            {
                formIn.Size = windowSize;
            }
        }
        else if (windowString == "Maximized")
        {
            formIn.Location = new Point(100, 100);
            formIn.StartPosition = FormStartPosition.Manual;
            formIn.WindowState = FormWindowState.Maximized;
        }
    }
    
    private static bool GeometryIsBizarreLocation(Point loc, Size size)
    {
        bool locOkay;
        if (loc.X < 0 || loc.Y < 0)
        {
            locOkay = false;
        }
        else if (loc.X + size.Width > Screen.PrimaryScreen.WorkingArea.Width)
        {
            locOkay = false;
        }
        else if (loc.Y + size.Height > Screen.PrimaryScreen.WorkingArea.Height)
        {
            locOkay = false;
        }
        else
        {
            locOkay = true;
        }
        return locOkay;
    }
    
    private static bool GeometryIsBizarreSize(Size size)
    {
        return (size.Height <= Screen.PrimaryScreen.WorkingArea.Height &&
            size.Width <= Screen.PrimaryScreen.WorkingArea.Width);
    }
    
    public static string GeometryToString(Form mainForm)
    {
        return mainForm.Location.X.ToString() + "|" +
            mainForm.Location.Y.ToString() + "|" +
            mainForm.Size.Width.ToString() + "|" +
            mainForm.Size.Height.ToString() + "|" +
            mainForm.WindowState.ToString();
    }

  6. #6
    Join Date
    Feb 2011
    Posts
    13

    Re: Help Please; Form positioning

    to save the info for the screen the form was on when it last closed;

    Screen.AllScreens.Count(); //gets the total multiple screen count

    string CurrentScreenName = Screen.FromControl(this).DeviceName; //gets the name of the screen the form is on, or was last on?

    Screen CurrentScreen = Screen.FromControl(this); // gets the current screen object


    How could I use this info to;

    1. save screen name on exit/form unload.
    2. when the form loads, check to see if the screen name exists/is visible..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center