简体   繁体   English

如何获取资源文件中对话框的按钮控件的大小和位置?

[英]How to get the size and position of a button control of a dialog in a resource file?

I am trying to programmatically design a dialog menu that resizes based on the resolution of the screen, and I am able to get the size and positions of the dialog using the dialog's nameID following this question:我正在尝试以编程方式设计一个根据屏幕分辨率调整大小的对话框菜单,并且我能够在这个问题之后使用对话框的nameID对话框的大小和位置:

Get Dialog Size as defined in resource file 获取资源文件中定义的对话框大小

However, I'm having trouble trying to obtain the size and positions of the button controls within the dialog.但是,我在尝试获取对话框中按钮控件的大小和位置时遇到了问题。 Here's an example of what the dialog might look like in the resource file:以下是对话框在资源文件中的示例:

IDD_DLG DIALOG  0, 0, 300, 200
STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP
BEGIN
    DEFPUSHBUTTON   "OK",IDOK,30,46,42,18
    PUSHBUTTON      "Cancel",IDCANCEL,145,46,42,18
END

I can obtain the dialog's size and positions using its nameID , ie IDD_DLG , but is it possible to obtain the same thing for the DEFPUSHBUTTON or PUSHBUTTON programmatically?我可以使用它的nameID获取对话框的大小和位置,即IDD_DLG ,但是是否可以通过编程方式为DEFPUSHBUTTONPUSHBUTTON获取相同的东西?

If so, how?如果是这样,如何? Thanks!谢谢!

MFC allows automatic repositioning/resizing of child buttons. MFC 允许自动重新定位/调整子按钮的大小。 In resource property page, click on dialog button, go to dynamic control section, enable dynamic resizing/move for each button.在资源属性页中,单击对话框按钮,转到动态控制部分,为每个按钮启用动态调整大小/移动。

To find the coordinates of the button, relative to top-left of dialog client window:要查找按钮相对于对话框客户端窗口左上角的坐标:

Use GetWindowRect to find the buttons rectangle in screen coordinates.使用GetWindowRect在屏幕坐标中查找按钮矩形。 Then convert the screen coordinates to client coordinates:然后将屏幕坐标转换为客户端坐标:

CMyDialog::OnInitDialog()
{
    CDialog::OnInitDialog();
    CRect rc;
    CWnd *wnd = GetDlgItem(IDOK);
    wnd->GetWindowRect(&rc);
    ScreenToClient(rc);
    ...
    //move/resize rc
    wnd->SetWindowPos(NULL, rc.left, rc.top, rc.Width(), rc.Height(), SWP_SHOWWINDOW);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM