【问题标题】:Spin Control GetPos() value is delayedSpin Control GetPos() 值被延迟
【发布时间】:2018-12-25 11:26:15
【问题描述】:

我有一个编辑控件和一个初始位置设置为 0 的旋转控件。单击向上箭头时,编辑框从 0 变为 1,这很好。但是当我使用GetPos() 时,MyValue 为 0。当自旋控件再次从 1 增加到 2 时,MyValue 变为 1。按下向下箭头时,编辑框从 2 变为 1,但值变为 2 . 看来MyValue 永远是spin control 后面的一个action。

 BOOL CAlphaDlg::OnInitDialog() 
 {
           // default code left out to keep it short ...
           // TODO: Add extra initialization here
           // set range and initial position
           mSpinControl.SetRange(0, 3600); // range
           mSpinControl.SetPos(0); // inital position
           MyValue =  mSpinControl.GetPos();
           // display initial value in buddy editcontrol
           mEditControlDisplay.Format("%d", MyValue);
           UpdateData(false);
           return TRUE;
 }


 void CAlphaDlg::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
 {
        LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
        // TODO: Add your control notification handler code here
        UpdateData(true);
        int MyValue= mSpinControl.GetPos();
        std::cout << MyValue << std::endl;
        *pResult = 0;
   

 }

我尝试从编辑控件中获取值,但该值表现出相同的行为。如何获取 GetPos() 的值以匹配编辑控件中显示的内容?

提前谢谢你。

编辑:这里是完整的代码

// AlphaDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Alpha.h"
#include "AlphaDlg.h"
#include "afxdialogex.h"

#include <iostream>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

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

// Dialog Data
#ifdef AFX_DESIGN_TIME
    enum { IDD = IDD_ABOUTBOX };
#endif

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

// Implementation
protected:
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CAlphaDlg dialog



CAlphaDlg::CAlphaDlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(IDD_ALPHA_DIALOG, pParent)
    , mEditControlDisplay(_T(""))
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAlphaDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_EDIT1, mEditControlDisplay);
    DDX_Control(pDX, IDC_SPIN1, mSpinControl);
}

BEGIN_MESSAGE_MAP(CAlphaDlg, CDialogEx)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN1, &CAlphaDlg::OnDeltaposSpin1)
END_MESSAGE_MAP()


// CAlphaDlg message handlers

BOOL CAlphaDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

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

    // 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
    mSpinControl.SetRange(0, 10);
    mSpinControl.SetPos(0);
    int MyValue = mSpinControl.GetPos();
    mEditControlDisplay.Format("%d", MyValue);
    UpdateData(false);
    return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAlphaDlg::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 CAlphaDlg::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 CAlphaDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}

void CAlphaDlg::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
    // TODO: Add your control notification handler code here
    *pResult = 0;
    UpdateData(true);
    int MyValue = mSpinControl.GetPos();
    std::cout << mEditControlDisplay << std::endl;
    std::cout << MyValue << std::endl;
}

【问题讨论】:

  • 目前还不清楚,为什么要调用UpdateData 然后继续手动填充控件,或者手动读回它们。这就是UpdateData 应该已经为你做的事情了。
  • @IInspectable 我的错误,当我试图从中获取值时,OnDeltaposSpin1 中的 UpdateDate(true) 用于从编辑控件中检索数据,因为它显示了正确的值。它不起作用并且具有相同的延迟行为。我删除了 std::cout
  • OnInitDialog 中调用UpdateData 怎么样?不需要那个,除非您没有调用基类实现。你应该打电话给哪个。
  • @IInspectable OnInitDialog中调用的UpdateData用于在编辑控制框中显示初始值0。没有它,在单击旋转控件之前,编辑控件框将保持空白。
  • 这意味着,您没有调用基类实现。不调用基类实现会阻止对话框正确初始化。这就是您要提供完整代码的原因。

标签: c++ visual-studio-2015 mfc controls


【解决方案1】:

首先,考虑将您的编辑框映射到int,然后您不需要转换为字符串。并且默认值是 0 开始,所以微调器就可以了。您也可以关闭自动好友。

在 deltapos 处理程序中,您可以这样做:

void CMFCApplication1Dlg::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);

    // TODO: Add your control notification handler code here
    *pResult = 0;

    SetDlgItemInt(IDC_EDIT1, pNMUpDown->iPos);

    UpdateData(TRUE);

    CString a;
    a.Format(_T("%d"), iNumberValue);
    AfxMessageBox(a);
}

对我来说,弹出消息和编辑控件中的结果是一样的。

更新

您可以通过查看结构来确定新值是什么:

void CMFCApplication1Dlg::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);

    int iCurrentPos = pNMUpDown->iPos;
    if (pNMUpDown->iDelta > 0)
        iCurrentPos++;
    else
        iCurrentPos--;

    CString strNewValue;
    strNewValue.Format(_T("%d"), iCurrentPos);
    AfxMessageBox(strNewValue);

    *pResult = 0;
}

供参考

您可能想考虑一下CSpinButtonCtrl::SetAccel,它设置了旋转按钮控件的加速度。

在您的情况下,我认为这无关紧要,因为您的范围只有 10 单位。但是如果你有一个更大的范围,那么它可能会增加一个以上的单位。这只是要记住的事情。

根据UDN_DELTAPOS 的参考,它说:

结构的 iDelta 成员是一个有符号整数,包含建议的位置变化。

因此,您可以根据 iDelta 值而不是 1 来改进代码和递增/递减。这将考虑加速。所以:

int iCurrentPos = pNMUpDown->iPos + pNMUpDown->iDelta;

【讨论】:

  • 有了这个实现,在第一次按下 spincontrol 时,AfxMessageBox(a) 会显示 int 0。在我确认“ok”之后,编辑控件会增加到 1。有没有办法让 AfxMessageBox (a) 在初始按下时显示 1 而不是 0?谢谢
  • @TMa 刚刚用更多信息更新了我的答案。感谢您接受它。如果可以的话,也不要忘记投票。 :)
  • 谢谢。它非常适合我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-26
  • 2018-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多