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

3
我在MFC中创建了一个对话框视图。
如下图所示,您可以看到一些控件,如滑块控件和编辑框。
当我调整对话框的大小时,这些控件不会移动。
我希望能够将控件移动到适合对话框大小的位置,但不需要调整控件的大小。
为解决这个问题,我该怎么做?
更新1
特别地,我想设置滑块的位置。
因此,我首先向对话框类添加了滑块的成员变量“CSliderCtrl sss”,然后在OnSize()函数中执行以下操作:
 OnSize()
{
....
sss.MoveWindow(10, 10,cx-20,cy-60);
....

}

顺便提一下,问题是调整对话框大小后滑块的第一个位置发生了变化。

因此我想知道如何在InitialDialog中设置滑块的第一个位置。

更新2

我已经跟进了你的源代码,但你和我之间的工作方式不同。

第一次运行时,我得到了这个结果 enter image description here

当我稍微调整对话框大小后,我得到了这个结果 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();
     //https://dev59.com/TpDea4cB1Zd3GeqPjPzJ
//   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;
};

更新3

我已经更新了,但仍然无法正常工作。 当我将对话框向左调整一点时。 enter image description here

当我将对话框向左调整很多时。 enter image description here

当我将对话框向右调整时。 enter image description here


请参考以下链接:https://dev59.com/VnVC5IYBdhLWcg3w9GA9 - Mark Ransom
我不理解你的第二次编辑。也许你想在调整大小时将控件向上/向下移动,同时增加/减少宽度? - Barmak Shemirani
@Barmak Shemirani:是的,你说得对。我想在调整大小时移动控件。因为在调整大小时控件会消失。 - cabot
2个回答

3
另一种方法是使用此函数:

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);
}

dxdy可以为零,如果你只需要水平/垂直偏移中的一个。

顺便说一下,您可能还需要在消息映射中添加ON_WM_GETMINMAXINFO,以防止对话框变得太小:

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

在此输入图片描述 在此输入图片描述

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

编辑2

编辑3

复制/粘贴这两个函数OffsetChildWindowCmfc_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);
}

谢谢,但是当我在 "CPoint p = { 0 };" 进行构建时,出现了以下错误信息:"mfc_test5Dlg.cpp(260):错误 C2552:'p':非聚合体不能用初始化器列表进行初始化 'CPoint':具有基础的类型不是聚合体"。 - cabot
我还收到了一个错误消息:“Unhandled exception at 0x00361fbe in mfc_test5.exe: 0xC0000005: Access violation reading location 0x00000020.”,当我运行到这里“if (!child->GetParent()) return;”时。我认为有一些问题。 - cabot
不,我还在使用你的源代码。我遇到了一些问题。这可能是因为你和我之间的工作方式不同。 - cabot
我已经更新了“mydialog.cpp”和“mydialog.h”文件。 - cabot
抱歉,我复制时又出现了一个错误,应该是 OffsetChildWindow(m_pTestView, 0,0,dx,dy); - Barmak Shemirani
显示剩余4条评论


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接