Main Content

setpixelposition

设置组件位置(以像素为单位)

    说明

    示例

    注意

    App 设计工具或使用 uifigure 函数创建的 App 不支持 setpixelposition 函数。在这种情况下,要以像素为单位指定组件的位置,请将其 Units 属性设置为 'pixels',然后使用 Position 属性设置其位置。

    pos = setpixelposition(c,position) 设置 c 指定的组件的位置(以像素为单位)。将位置指定为四元素向量,以 [left bottom width height] 形式给出组件相对于其父容器的位置和大小。

    示例

    pos = setpixelposition(c,position,isrecursive)(其中 isrecursivetrue)设置组件相对于父图窗的位置。isrecursive 的默认值为 false,它设置组件相对于其直接父容器的位置。

    示例

    全部折叠

    在面板容器中创建一个普通按钮。

    f = figure('Position',[300 300 300 200]);
    p = uipanel('Position',[.2 .2 .6 .6]);
    btn = uicontrol(p,'Style','PushButton', ...
         'Units','Normalized', ...
         'String','Push Button', ...
         'Position',[.1 .1 .5 .2]);

    Push button in a panel in a figure window

    获取普通按钮相对于面板的位置(以像素为单位)。通过向右和向上移动 10 个像素,并将宽度和高度增加 25 个像素来更新位置向量。使用更新后的位置向量来设置普通按钮在面板内的位置。

    pos = getpixelposition(btn);
    newpos = pos + [10 10 25 25];
    setpixelposition(btn,newpos);

    Push button with updated size in a panel in a figure window. The button is taller and wider than when it was originally created, and is further away from the bottom-left corner of the panel that it is in.

    更新普通按钮相对于图窗窗口的位置,以在移动其父容器后保持该按钮的位置。

    在面板容器中创建一个普通按钮。

    f = figure('Position',[300 300 300 200]);
    p = uipanel('Position',[.2 .2 .6 .6]);
    btn = uicontrol(p,'Style','PushButton', ...
         'Units','Normalized', ...
         'String','Push Button', ...
         'Position',[.1 .1 .5 .2]);

    Push button in a panel in a figure window

    获取普通按钮相对于图窗窗口的位置(以像素为单位),并将其存储在 pos 中。

    pos = getpixelposition(btn,true)
    pos =
    
       80.6000   54.6000   88.0000   23.2000

    更新面板的位置。由于普通按钮的位置是相对于其父级(即面板)而言的,因此此操作也会更改普通按钮的位置。

    p.Position = [.1 .1 .8 .8];

    Push button in a panel with updated size. Both the panel and the button are taller and wider than when they were originally created.

    设置普通按钮相对于图窗窗口的位置,以将普通按钮重置到其原始位置,同时保持更新后的面板位置。

    setpixelposition(btn,pos,true);

    Push button in a panel. The button is the same size as when it was originally created.

    输入参数

    全部折叠

    UI 组件。

    组件的新位置(以像素为单位),指定为 [left bottom width height] 形式的向量。向量的 leftbottom 元素与直接父容器或父图窗相关,具体取决于 isrecursive 的值。此表介绍该向量中的每个元素。

    元素描述
    left父级的内部左边缘与 UI 组件的外部左边缘之间的距离
    bottom父级的内部下边缘与 UI 组件的外部下边缘之间的距离
    widthUI 组件的左右外部边缘之间的距离。
    heightUI 组件的上下外部边缘之间的距离。

    是否设置相对于父图窗的位置,指定为数值或逻辑值 0 (false) 或 1 (true)。

    • 0 (false) - 函数设置组件相对于其直接父级的位置。

    • 1 (true) - 函数设置组件相对于父图窗窗口的位置。

    详细信息

    全部折叠

    像素

    以像素为单位的距离不依赖 Windows®Macintosh 系统上的系统分辨率:

    • 在 Windows 系统上,MATLAB® 将一个像素定义为 1/96 英寸。

    • Macintosh 系统上,MATLAB 将一个像素定义为 1/72 英寸。

    在 Linux® 系统上,您的系统分辨率决定一个 MATLAB 像素的大小。有关详细信息,请参阅DPI-Aware Behavior in MATLAB

    版本历史记录

    在 R2007a 中推出