如何将主应用程序窗口居中放置在计算机屏幕中间的MOTIF中?

时间:2019-02-12 17:46:25

标签: motif

如何将MOTIF Widget的主应用程序窗口居中在计算机屏幕中央?例如此处的表单窗口小部件。

代码已准备就绪并且可以正常工作,但是该窗口出现在屏幕的左上方。

#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>


void main ( int argc, char ** argv )

{
    Widget              shell, form, label, button;
    XtAppContext app;
    int  i;


    shell = XtAppInitialize ( &app, "Formtest", NULL, 0,
                              &argc, argv, NULL, NULL, 0 );


    form = XtCreateManagedWidget ( "form", xmFormWidgetClass,
                                    shell, NULL, 0 );

    XtVaSetValues ( form,
                  XmNwidth, 500, 
                  XmNheight, 300, 
                  NULL );               


    label = XtVaCreateManagedWidget ( "label", xmLabelWidgetClass,
                                    form, NULL, 0 );

    button = XtVaCreateManagedWidget ( "button", xmPushButtonWidgetClass,
                                    form, 
                                    XmNbottomAttachment,       XmATTACH_FORM,
                                     0 );
    XtVaSetValues ( button,
                  XmNwidth, 100, 
                  XmNheight, 50, 
                  NULL );


    XtRealizeWidget ( shell );
    XtAppMainLoop ( app );

}`````

This MOTIF Windows is working correct already.
The only thing i want to do is to position it in the middle of the computerscreen.
It has something to do with the command  Xtscreen.


1 个答案:

答案 0 :(得分:0)

获取屏幕的高度和宽度并进行调整。 请尝试以下操作:

#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>


int main ( int argc, char ** argv )
{
    Widget              shell, form, label, button;
    XtAppContext app;
    int  i;


    shell = XtAppInitialize ( &app, "Formtest", NULL, 0,
                          &argc, argv, NULL, NULL, 0 );

    Screen * s = XtScreen(shell);

    int dw = WidthOfScreen(s);
    int dh = HeightOfScreen( s );

    Dimension px = (dw-500)/2;
    Dimension py = (dh-300)/2;

    XtVaSetValues ( shell,
              XmNwidth, 500,
              XmNheight, 300,
              XmNx, px,
              XmNy, py,
              NULL );


    form = XtCreateManagedWidget ( "form", xmFormWidgetClass,
                                shell, NULL, 0 );

    label = XtVaCreateManagedWidget ( "label", xmLabelWidgetClass,
                                form, NULL, 0 );

    button = XtVaCreateManagedWidget ( "button", xmPushButtonWidgetClass,
                                form,
                                XmNbottomAttachment,       XmATTACH_FORM,
                                 0 );
    XtVaSetValues ( button,
              XmNwidth, 100,
              XmNheight, 50,
              NULL );


    XtRealizeWidget ( shell );
    XtAppMainLoop ( app );

}
相关问题