locked
Open in the middle of screen RRS feed

  • Question

  • I made a window and i changed the flag to my window screen but when i open it in another computer with a different monitor size it was like way off. How would i do this. For a win32 application

    • Edited by NO.obie Saturday, July 18, 2009 9:50 PM Forgot to add win32 application
    Saturday, July 18, 2009 9:29 PM

Answers

  • Well, you make it easy to save face.  How do you expect 700,400 to be the middle of the screen except yours?  Use GetSystemMetrics() to find out the size of your customer's screen.  Dealing with the taskbar and multiple monitors is best done in managed code.


    Hans Passant.
    • Marked as answer by Wesley Yao Friday, July 24, 2009 6:20 AM
    Saturday, July 18, 2009 10:34 PM
  • Alright i changed the flags to


    flags, GetSystemMetrics(SM_CXSCREEN) / 2.6, GetSystemMetrics(SM_CYSCREEN) / 3.5


    but when i tryed it on another monitor it failed to. its like off.



    Thanks

    Well that's not an accurate way to center your window. Look at NoBugz's code above and convert it to API equivalent.
    http://blog.voidnish.com
    • Marked as answer by Wesley Yao Friday, July 24, 2009 6:21 AM
    Saturday, July 18, 2009 11:06 PM

All replies

  • Is this MFC? CWnd has a CenterWindow method.
    http://blog.voidnish.com
    Saturday, July 18, 2009 9:46 PM
  • Setting the StartupPosition property to CenterScreen won't work properly if the form gets rescaled when the target machine has a different system font size or a different video adapter DPI setting.  Windows Forms provides a way to work around that problem, as usual, position the form yourself in the Load event:

        protected override void OnLoad(EventArgs e) {
          Screen scr = Screen.FromPoint(new Point(this.Left, this.Top));
          this.Left = scr.WorkingArea.Left + (scr.WorkingArea.Width - this.Width) / 2;
          this.Top = scr.WorkingArea.Top + (scr.WorkingArea.Height - this.Height) / 2;
        }

    Use scr.Bounds instead of scr.WorkingArea if you really want it in the center, regardless of the location of the taskbar.
    Hans Passant.
    • Marked as answer by Wesley Yao Friday, July 24, 2009 6:20 AM
    • Unmarked as answer by nobugz Friday, July 24, 2009 10:23 AM
    Saturday, July 18, 2009 9:53 PM
  • Im making this in C++ win32 application. Unless when you mean by windows forms then alright i will try that out.


    Thanks for helping
    Saturday, July 18, 2009 10:00 PM
  • Im making this in C++ win32 application. Unless when you mean by windows forms then alright i will try that out.


    Thanks for helping

    If you are writing a native application, you cannot use NoBugz's suggestion without enabling /clr for your application and rewriting your UI in WinForms.

    It seems you are not using MFC either, so you'd have to roll your own CenterWindow function as the API does not have one.
    http://blog.voidnish.com
    Saturday, July 18, 2009 10:04 PM
  • Oh alright.
    Saturday, July 18, 2009 10:05 PM
  • Crud, I got the language wrong again.  Not that I could tell from the OP, what the heck does "changed the flag to my window screen" mean?  I'll try to save face and recommend C# here.  Good for newbies and MVPs alike.

    Hans Passant.
    Saturday, July 18, 2009 10:10 PM
  • Crud, I got the language wrong again.  Not that I could tell from the OP, what the heck does "changed the flag to my window screen" mean?  I'll try to save face and recommend C# here.  Good for newbies and MVPs alike.

    Hans Passant.

    Yeah, initially I assumed he was talking about an MFC app, then when I saw your reply and his response to it I thought maybe he's using WinForms, but his last reply indicates he's writing a straight API application.
    http://blog.voidnish.com
    Saturday, July 18, 2009 10:14 PM
  • Yeah im using Win32 api application. To create this. And when i mean "Changed the flag" is this right here


    HWND hWndToolWindow = CreateWindowEx(WS_EX_TOOLWINDOW, TEXT("ToolWindow"), TEXT("ToolWindow"), flags, 700, 400, 269, 302, NULL, NULL, hInstance, NULL);

    I changed this to be in the middle of my screen


    flags, 700, 400,

    But when i open it on another computer with a different monitor size. It was like off the screen lol.

    Thats what i mean about changing the flag
    Saturday, July 18, 2009 10:24 PM
  • Well, you make it easy to save face.  How do you expect 700,400 to be the middle of the screen except yours?  Use GetSystemMetrics() to find out the size of your customer's screen.  Dealing with the taskbar and multiple monitors is best done in managed code.


    Hans Passant.
    • Marked as answer by Wesley Yao Friday, July 24, 2009 6:20 AM
    Saturday, July 18, 2009 10:34 PM
  • Alright i changed the flags to


    flags, GetSystemMetrics(SM_CXSCREEN) / 2.6, GetSystemMetrics(SM_CYSCREEN) / 3.5


    but when i tryed it on another monitor it failed to. its like off.



    Thanks
    Saturday, July 18, 2009 11:04 PM
  • Alright i changed the flags to


    flags, GetSystemMetrics(SM_CXSCREEN) / 2.6, GetSystemMetrics(SM_CYSCREEN) / 3.5


    but when i tryed it on another monitor it failed to. its like off.



    Thanks

    Well that's not an accurate way to center your window. Look at NoBugz's code above and convert it to API equivalent.
    http://blog.voidnish.com
    • Marked as answer by Wesley Yao Friday, July 24, 2009 6:21 AM
    Saturday, July 18, 2009 11:06 PM
  • Try these code:

    RECT rect;
    GetWindowRect(hWnd, &rect);
    int x = (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) / 2;
    int y = (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) / 2;
    SetWindowPos(hWnd, NULL, x, y, 0, 0, SWP_NOSIZE);


    Sincerely,
    Wesley


    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, July 24, 2009 6:19 AM
  • Alright i changed the flags to


    flags, GetSystemMetrics(SM_CXSCREEN) / 2.6, GetSystemMetrics(SM_CYSCREEN) / 3.5


    but when i tryed it on another monitor it failed to. its like off.



    Thanks

    Well that's not an accurate way to center your window. Look at NoBugz's code above and convert it to API equivalent.
    http://blog.voidnish.com
    What do you mean O.o
    Friday, July 24, 2009 5:55 PM