Center a Photon Window

I have a PtWindow that I want to center on the screen. Is there a way to do this? I know how to get the current screen width and height with PgGetVideoMode and PgGetVideoModeInfo, but it’s not clear to me how to get the outside dimensions of the window (which I don’t think can be determined until after the window has be realized), and then how to move the window to the proper location.


You could set the windows size yourself. You can definitely set it’s location. Since you know the other relevent information, centering should be straight forward.

Also, there are both pre and post realize callbacks for startup, so if you want the window to set its own size, you can check afterwards and just reset its location. This can cause a visual jump, which you can mitigate by setting the location off screen in the pre-realize callback.

Thanks for the idea. I’ve run into a small problem though. I’m drawing the window initially offscreen and registering the Pt_CB_REALIZED callback on the window. In the callback, I’m setting the position with PtSetResource(window, Pt_ARG_POS, *pos, 0), but nothing happens. I’ve confirmed that the callback is being made and that PtSetResource is not returning -1. If I move the window with a PtTimer,with the same callback code, that works but that seems hackish to use a timer to move a window one time especially when there is a callback that should work.

In addition, when I make the call to PtWindowGetFrameSize, my upper left point is the same as my lower right point. This occurs even in the timer callback.

Here is my code for creating the window:

PtSetArg(&args[0], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_FALSE, Ph_WM_RENDER_CLOSE | Ph_WM_RENDER_TITLE | Ph_WM_RENDER_RESIZE); PtSetArg(&args[1], Pt_CB_REALIZED, windowCBs, sizeof(windowCBs)/sizeof(windowCBs[0])); window = PtCreateWidget(PtWindow, Pt_NO_PARENT, 2, args);

and here is my code for centering the window:

  PgDisplaySettings_t displaySettings;
  PgVideoModeInfo_t modeInfo;
  PhRect_t windowRect;
  PhPoint_t newPos;
  PgGetVideoModeInfo(displaySettings.mode, &modeInfo);
  PtWindowGetFrameSize(window, &windowRect);
  newPos.x = (modeInfo.width - ( - windowRect.ul.x)) / 2;
  newPos.y = (modeInfo.height - ( - windowRect.ul.y)) / 2;
  PtSetResource(window, Pt_ARG_POS, &newPos, 0);

Assuming you are doing things right, it seems that somehow setting the new position in the Realized callback doesn’t work.
Maybe you need to “damage” the widget?