我有一个带有旋转控件的编辑控件,初始位置设置为0。单击向上箭头时,编辑框从0变为1,这很好。但是,当我使用GetPos()时,MyValue为0。当旋转控件从1再次增加到2时,MyValue变为1。按下向下箭头时,编辑框从2变为1,但值变为2。似乎MyValue始终是自旋控件后面的一项操作。

 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;


 }

我尝试从editcontrol获取值,但是该值表现出相同的行为。如何获取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;
}

最佳答案

首先,考虑将您的编辑框映射到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);
}
对我来说,弹出消息和编辑控件中的结果是相同的。
更新资料
您可以通过查看结构来锻炼新值(value):
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 值(而不是1)来改进代码和增加/减少。这将有助于加速。所以:
int iCurrentPos = pNMUpDown->iPos + pNMUpDown->iDelta;

07-26 06:59