如何在 MFC 中调整对话框大小时移动控件?

2024-05-12

我已经在 MFC 中创建了对话框视图。

从下图中可以清楚地看到。(如滑块控件和编辑框等)

当我调整对话框大小时,这些控件不会移动。

在此输入图像描述 https://i.stack.imgur.com/7OxAK.jpg

我想移动控件以适应对话框。但不需要调整控件的大小。

我应该做什么来解决这个问题?

update 1

特别是,我想设置滑块位置。

因此,首先,我将滑块“CSliderCtrl sss”的成员变量添加到对话框类中,然后在 OnSize() 中进行此操作;

 OnSize()
{
....
sss.MoveWindow(10, 10,cx-20,cy-60);
....

}

顺便说一句,问题是调整对话框大小后滑块的第一个位置存在差异。

所以我想知道如何在初始对话框中设置滑块的第一个位置。

update 2

我已经追踪到你的消息来源了。但你我之间的工作是有区别的。

When I run first I got this enter image description here

When I resize the dialog a very little I got this enter image description here

如您所见,滑块消失了。

我在这里更新了 MyDlg.cpp

// mfc_test5Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "mfc_test5.h"
#include "mfc_test5Dlg.h"
#include "afxdialogex.h"
#include "Testview.h"
#include <Magick++.h>
extern CTestview *global_TestView;
char * fileposition;


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialogEx
{
public:
    CAboutDlg();

    // Dialog Data
    enum { IDD = IDD_ABOUTBOX };

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

    // Implementation
protected:
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// Cmfc_test5Dlg dialog




Cmfc_test5Dlg::Cmfc_test5Dlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(Cmfc_test5Dlg::IDD, pParent)
    , m_CString(_T(""))
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    fileposition = (char*)this;
    m_pTestView = NULL;
}

void Cmfc_test5Dlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_SLIDER1, m_CSliderCtrl);
    DDX_Text(pDX, IDC_EDIT1, m_CString);
}

BEGIN_MESSAGE_MAP(Cmfc_test5Dlg, CDialogEx)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER1, &Cmfc_test5Dlg::OnNMReleasedcaptureSlider1)
    ON_WM_DROPFILES()
    ON_WM_CREATE()
    ON_WM_SIZE()
END_MESSAGE_MAP()



BOOL Cmfc_test5Dlg::OnInitDialog()
{

    CDialogEx::OnInitDialog();
     GetClientRect(&m_SaveRect);

    // Add "About..." menu item to system menu.


    CRuntimeClass *pObject;
    pObject = RUNTIME_CLASS(CTestview);
    m_pTestView = (CTestview*)pObject->CreateObject();
    CRect crt;
    GetClientRect(&crt);
    int x,y;
     x = crt.Width();
     y  = crt.Height();
     //http://stackoverflow.com/questions/33653087/how-to-move-a-control-while-resizing-dialog-size-in-mfc
//   m_CSliderCtrl.SetWindowPos(0,x-293,y-125,x-293,y-125,SWP_NOSIZE /*SWP_NOZORDER*/);

    if (!m_pTestView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, CRect(0, 0,x-200 ,y), this, AFX_IDW_PANE_FIRST, NULL))
    {
        TRACE0("Failed to create view window\n");
        return -1;
    }


    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        BOOL bNameValid;
        CString strAboutMenu;
        bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
        ASSERT(bNameValid);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);         // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon

    // TODO: Add extra initialization here
    m_CSliderCtrl.SetRange(0,255); //Reference Here (http://system.tistory.com/13)
    m_CSliderCtrl.SetRangeMin(0);
    m_CSliderCtrl.SetRangeMax(255);
    m_CSliderCtrl.SetPos(128);
    m_CSliderCtrl.SetLineSize(1);
    m_CString.Format(_T("%d"), 0); //Reference here (http://depts.washington.edu/cmmr/biga/chapter_tutorials/1.C++_MFC_D3DOGL/1.StepByStepGuide/tutorial_3.html)


    return TRUE;  // return TRUE  unless you set the focus to a control
}

void Cmfc_test5Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }
    else
    {
        CDialogEx::OnSysCommand(nID, lParam);
    }
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void Cmfc_test5Dlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialogEx::OnPaint();
    }
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR Cmfc_test5Dlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}



void Cmfc_test5Dlg::OnNMReleasedcaptureSlider1(NMHDR *pNMHDR, LRESULT *pResult)
{
    // TODO: Add your control notification handler code here
    int value = m_CSliderCtrl.GetPos(); //Reference Here (http://depts.washington.edu/cmmr/biga/chapter_tutorials/1.C++_MFC_D3DOGL/1.StepByStepGuide/tutorial_3.html)
    m_CString.Format(_T("%d"), value);
    UpdateData(FALSE);

    *pResult = 0;
}


void Cmfc_test5Dlg::OnDropFiles(HDROP hDropInfo)
{

    int nFiles;
    static char szPathName[MAX_PATH]; // 파일 경로면이 들어간다.
    CString strFileName;

    ::DragQueryFile(hDropInfo, 0, szPathName, MAX_PATH);

    ::DragFinish(hDropInfo);
    CDialog::OnDropFiles(hDropInfo);
    CDialogEx::OnDropFiles(hDropInfo);

    fileposition = szPathName;
    if (m_pTestView != NULL)
        m_pTestView->DoDisplayImage();


}


int Cmfc_test5Dlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDialogEx::OnCreate(lpCreateStruct) == -1)
        return -1;

    // TODO:  Add your specialized creation code here


    return 0;
}

void OffsetChildWindow(CWnd* child, int dx, int dy, int dw, int dh)
{
    if (!child)
    {
        return; //Add error handling, control does not exits
    }

    if (!child->GetParent()) 
    {
        return;
    }

    //find child window's coordinates relative to top-left of parent
    CRect rc;
    child->GetClientRect(&rc);
    CPoint p(0, 0);
    ScreenToClient(child->GetParent()->GetSafeHwnd(), &p);
    rc.OffsetRect(p);

    //prevent negative size
    if ((rc.Width() + dw) < 0) dw = -rc.Width();
    if ((rc.Height() + dh) < 0) dh = -rc.Height();

   child->SetWindowPos(0, rc.left + dx, rc.top + dy, rc.Width() + dw, rc.Height() + dh, SWP_NOZORDER);


    child->Invalidate(FALSE);
}

void Cmfc_test5Dlg::OnSize(UINT nType, int cx, int cy)
{
    CDialogEx::OnSize(nType, cx, cy);

    // TODO: Add your message handler code here

    if (!cx || !cy)
        return;


    if (m_pTestView)
    {
        m_pTestView->MoveWindow(0, 0, cx-200, cy);
    }


    int dx = cx - m_SaveRect.right;
    int dy = cy - m_SaveRect.bottom;

    OffsetChildWindow(GetDlgItem(IDC_SLIDER1), 0,dy,dx,0);
    GetClientRect(&m_SaveRect);

}

这是头文件。

// mfc_test5Dlg.h : header file
//

#pragma once
#include "afxcmn.h"
#include "Testview.h"
#include "atltypes.h"



// Cmfc_test5Dlg dialog
class Cmfc_test5Dlg : public CDialogEx
{
// Construction
public:
    Cmfc_test5Dlg(CWnd* pParent = NULL);    // standard constructor

// Dialog Data
    enum { IDD = IDD_MFC_TEST5_DIALOG };

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support


// Implementation
protected:
    HICON m_hIcon;

    // Generated message map functions
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    DECLARE_MESSAGE_MAP()
public:
    CSliderCtrl m_CSliderCtrl;
private:
    CString m_CString;
public:
    afx_msg void OnNMReleasedcaptureSlider1(NMHDR *pNMHDR, LRESULT *pResult);
    afx_msg void OnDropFiles(HDROP hDropInfo);
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    CTestview *m_pTestView;
    afx_msg void OnSize(UINT nType, int cx, int cy);
    CRect m_SaveRect;
};

update 3

I have updated. but it does not stll work well. When I resize the dialog to left a little. enter image description here

when I resize the dialog to left so many. enter image description here

When I resize the dialog to right. enter image description here


另一种方法,使用这个函数:

void OffsetChildWindow(CWnd* child, int dx, int dy, int dw, int dh)
{
    if (!child) return;
    if (!child->GetParent()) return;
    if (!IsWindow(child->m_hWnd)) return;

    //find child window's coordinates relative to top-left of parent
    CRect rc;
    child->GetWindowRect(&rc);
    CPoint p(0, 0);
    ScreenToClient(child->GetParent()->GetSafeHwnd(), &p);
    rc.OffsetRect(p);

    //prevent negative size
    if ((rc.Width() + dw) < 0) dw = -rc.Width();
    if ((rc.Height() + dh) < 0) dh = -rc.Height();

    child->SetWindowPos(0, rc.left + dx, rc.top + dy, rc.Width() + dw, rc.Height() + dh, SWP_NOZORDER);
    child->Invalidate(FALSE);
}

声明一个成员变量如下:CRect m_SaveRect;

//Initialize
BOOL CMyDialog::OnInitDialog()
{
    CDialogEx::OnInitDialog();
    GetClientRect(&m_SaveRect);
    ...
}

void CMyDialog::OnSize(UINT f, int cx, int cy)
{
    CDialogEx::OnSize(f, cx, cy);

    if (!cx || !cy)
        return;

    //(this error check is needed on at least one child control)
    if (!GetDlgItem(IDOK)) return; //not ready yet

    int dx = cx - m_SaveRect.right;
    int dy = cy - m_SaveRect.bottom;

    OffsetChildWindow(GetDlgItem(IDCANCEL), dx, dy, 0, 0);
    OffsetChildWindow(GetDlgItem(IDOK), dx, dy, 0, 0);
    OffsetChildWindow(GetDlgItem(IDC_SLIDER1), 0, dy, dx, 0);
    OffsetChildWindow(GetDlgItem(IDC_EDIT1), 0, 0, dx, dy);

    GetClientRect(&m_SaveRect);
}

dx or dy如果您只需要水平/垂直偏移之一,则可以为零。

当我们这样做时,您可能还需要添加ON_WM_GETMINMAXINFO消息映射以防止对话框变得太小:

void CMyDialog::OnGetMinMaxInfo(MINMAXINFO *mx)
{
    mx->ptMinTrackSize.x = 400; //minimum 400x300 size
    mx->ptMinTrackSize.y = 300;
    CDialogEx::OnGetMinMaxInfo(mx);
}

-------------------------------------------------- -----

Edit 2

Edit 3

复制/粘贴这 2 个函数OffsetChildWindow and Cmfc_test5Dlg::OnSize并删除旧功能。我已经做了更新。

void OffsetChildWindow(CWnd* child, int dx, int dy, int dw, int dh)
{
    if (!child) return;
    if (!child->GetParent()) return;
    if (!IsWindow(child->m_hWnd)) return;

    //find child window's coordinates relative to top-left of parent
    CRect rc;
    child->GetWindowRect(&rc);
    CPoint p(0, 0);
    ScreenToClient(child->GetParent()->GetSafeHwnd(), &p);
    rc.OffsetRect(p);

    //prevent negative size
    if ((rc.Width() + dw) < 0) dw = -rc.Width();
    if ((rc.Height() + dh) < 0) dh = -rc.Height();

    child->SetWindowPos(0, rc.left + dx, rc.top + dy, rc.Width() + dw, rc.Height() + dh, SWP_NOZORDER);
    child->Invalidate(FALSE);
}

void Cmfc_test5Dlg::OnSize(UINT nType, int cx, int cy)
{
    CDialogEx::OnSize(nType, cx, cy);
    if (!cx || !cy) return;

    int dx = cx - m_SaveRect.right;
    int dy = cy - m_SaveRect.bottom;

    OffsetChildWindow(m_pTestView, 0,0,dx,dy);
    OffsetChildWindow(GetDlgItem(IDC_SLIDER1), dx,dy,0,0);
    OffsetChildWindow(GetDlgItem(IDC_EDIT1), dx,dy,0,0);
    GetClientRect(&m_SaveRect);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 MFC 中调整对话框大小时移动控件? 的相关文章

随机推荐

  • 如何使用编译时间作为自动版本控制信息?

    当程序被编译以获得一种自动版本信息时 是否有任何常量或 pragma来获取时间和日期 DATE and TIME 是预定义的宏 将分别扩展到当前日期和时间
  • 如何格式化折线图谷歌图表材料上的轴?

    我在格式化材料图表的轴时遇到问题 Using classic line chart if I would like to format my vertical axis with a dollar sign I would do vAxes
  • OpenCV Visual Studio ntdll.dll

    我尝试在 Visual Studio 2013 上使用 OpenCV 2 4 10 创建一个项目 但由于以下异常 到目前为止我运气不佳 请建议帮助 TIA letstryitonemoretime exe Win32 Loaded C Us
  • CSS 网格最小内容不适合内容

    我试图通过显式分配行 列和元素大小来将一些 div 放入网格中 并允许 CSS 网格使用以下 CSS 执行列和行大小调整工作 display grid grid auto columns min content 价值min content应
  • 被一个简单的正则表达式难住了

    我想看看字符串是否s包含正则表达式中的任何符号 下面的正则表达式可以正常工作rubular http rubular com s asd d s 但在 Ruby 1 9 2 中 它给出了以下错误消息 syntax error unexpec
  • 如何将txt文件添加到你的android项目中? [复制]

    这个问题在这里已经有答案了 我的Android studio版本是1 5 1 显然这个 never 版本没有 txt 文件的 asset 文件夹 您打算如何将这些文件包含到您的项目中 以及如何进一步使用您内部的应用程序 谢谢你的建议 Pro
  • gtest 和 gmock 有什么区别?

    我试图理解的目的google mock Google 的 C 模拟框架 https github com google googletest blob master googlemock README md 我已经与gtest较早 但我还是
  • asp.net c# 将数据集中的数据转换为电子邮件正文?

    从数据集到电子邮件正文的最佳方式是什么 我有一个 net 控制台应用程序 用于根据存储过程的结果发送电子邮件通知 并且想知道如何最好地从 SQL 数据转到电子邮件正文 带有颜色和字体的 html 正文是最好的 但纯文本也可以 thanks
  • 打开文件路径在 python 中不起作用[重复]

    这个问题在这里已经有答案了 我正在编写一个数据库程序 personica 是我的测试主题 我通常在文件路径的位置有一个变量 但出于测试和演示的目的 我只有一个字符串 在我的计算机上的这个确切位置有一个文本文件 顺便说一句 因为我很偏执 所以
  • 从反应本机SectionList中的节项访问节数据

    我需要访问有关反应本机SectionList 中的renderItem 内的部分 索引 值 的信息 根据http docs w3cub com react native sectionlist renderitem http docs w3
  • 通过名称查找进程ID

    如何在 Ruby 中通过名称或完整命令行找到 pid 而不调用外部可执行文件 我正在将 SIGUSR2 发送到命令行包含的进程ruby job rb 我想在不打电话的情况下执行以下操作pgrep uid Process uid pid pg
  • CMake 和 Visual Studio:如何获得快速、安静的命令行构建?

    我有一个 cmake 项目 它成功地完成了我想要的一切 但我有大约 100 个文件 当我只需要重新编译一个文件时 我厌倦了每次看到生成的巨大输出 每个文件 30 行 明确地说 我正在编译cmake build 得到这个结果 我需要传递给编译
  • 需要一个正则表达式将 css 类添加到第一个和最后一个列表项

    更新 谢谢大家的意见 一些附加信息 它实际上只是我正在使用的一小部分标记 20 行 目的是利用正则表达式来完成工作 我还能够修改脚本 电子商务脚本 以在构建导航时插入类 我想限制我所采用的黑客数量 以便在更新到软件的最新版本时让事情变得更容
  • 以编程方式结束/退出粘合作业

    我正在使用 Glue 书签来处理数据 我的工作是每天安排的 但也可以 手动 启动 由于我使用书签 有时胶水作业可以在没有新数据要处理的情况下启动 然后读取的数据帧为空 在这种情况下 我想好好地结束我的工作 因为它没有什么关系 我试过 if
  • Correct_user 和 admin 的多个 before_filter 语句

    我有一个组资源 我正在尝试使用适当的授权来设置该资源 我试图实现的授权逻辑是这样的 只有群组成员才能查看他们的群组 管理员可以查看any组 以及采取其他行动 我尝试在组控制器中使用以下 before filter 语句来执行此操作 befo
  • NativeScript 错误。在列表模板中找到多个视图

    我有这样的ListView
  • 半流体布局 CSS/Html

    我有一个两列布局 其中右列的静态宽度为 350px 左列的宽度应填充页面的其余部分 或者至少这是我想要发生的事情 但不幸的是事实并非如此 看一下我的 css html http jsfiddle net CmJ7P http jsfiddl
  • C 中的复合语句表达式

    下面的代码不起作用 int i void 999 100 添加括号就可以了 为什么 int i void 999 100 还有另一种方法可以完成此类分配 int i void 999 100 是什么让他们与众不同 在这份声明中 int i
  • 如何将 vue3-openlayers 插件添加到 nuxt

    我有以下 main ts 文件Vue3 https v3 vuejs org import createApp from vue import App from App vue How to do this in nuxt3 import
  • 如何在 MFC 中调整对话框大小时移动控件?

    我已经在 MFC 中创建了对话框视图 从下图中可以清楚地看到 如滑块控件和编辑框等 当我调整对话框大小时 这些控件不会移动 在此输入图像描述 https i stack imgur com 7OxAK jpg 我想移动控件以适应对话框 但不