简体   繁体   English

如何使用CSpinButtonCtrl类在MFC中动态创建旋转按钮控件?

[英]How can I create a Spin Button Control dynamically in MFC using CSpinButtonCtrl class?

I realize that this is a trivial problem and I even looked at an MFC book(Programming Windows with MFC by Prosise). 我意识到这是一个小问题,我什至看了一本MFC书(Prosise的《使用MFC编程Windows》)。 However, I couldn't really find a solution. 但是,我找不到真正的解决方案。

I am trying to create a Spin Button Control dynamically and here is a simplified code: 我正在尝试动态创建旋转按钮控件,这是一个简化的代码:

    CEdit* m_editControl = new CEdit();
    m_EditControl->Create(WS_VISIBLE | WS_CHILD , rectEdit, this, EditID);

    CSpinButtonCtrl* m_spinControlCtrl = new CSpinButtonCtrl;
    m_spinControlCtrl->Create(WS_VISIBLE | WS_CHILD, rectSpinButton, this, SpinID);

    m_spinControlCtrl->SetBase(10);
    m_spinControlCtrl->SetBuddy(m_editControl );
    m_spinControlCtrl->SetRange(-55, 55);

My problem is that the spin button does not change the value of the CEdit. 我的问题是旋转按钮不会更改CEdit的值。 Am I missing something? 我想念什么吗? How can I create a Spin Button Control dynamically? 如何动态创建旋转按钮控件?

Your spin control is missing the style UDS_SETBUDDYINT : 您的旋转控件缺少UDS_SETBUDDYINT样式:

UDS_SETBUDDYINT Causes the up-down control to set the text of the buddy window (using the WM_SETTEXT message) when the position changes. UDS_SETBUDDYINT导致位置更改时,上下控件(使用WM_SETTEXT消息)设置伙伴窗口的文本。 The text consists of the position formatted as a decimal or hexadecimal string. 文本由格式化为十进制或十六进制字符串的位置组成。

I also suggest setting UDS_ARROWKEYS so the arrow keys can be used to increment or decrement the value when the focus is on the edit control. 我还建议设置UDS_ARROWKEYS以便当焦点位于编辑控件上时,可以使用箭头键增加或减少值。

For the edit control I would add WS_TABSTOP so the user can navigate using the TAB key and WS_EX_CLIENTEDGE so the edit control shows the regular themed border. 对于编辑控件,我将添加WS_TABSTOP以便用户可以使用TAB键和WS_EX_CLIENTEDGE进行导航,以便编辑控件显示常规主题边框。

I also noticed that you use dynamic memory allocation for the controls, which is not necessary. 我还注意到,您对控件使用了动态内存分配,这不是必需的。 Just create non-pointer member variables like CEdit m_EditControl; 只需创建非指针成员变量,如CEdit m_EditControl; so you don't have to worry about deallocation. 因此您不必担心释放问题。

Fixed code: 固定代码:

m_EditControl.CreateEx(WS_EX_CLIENTEDGE, L"Edit", L"0", WS_VISIBLE|WS_CHILD|WS_TABSTOP, 
                       rectEdit, this, EditID);

m_spinControlCtrl.Create(WS_VISIBLE|WS_CHILD|UDS_SETBUDDYINT|UDS_ARROWKEYS, 
                         rectSpinButton, this, SpinID);

m_spinControlCtrl.SetBase(10);
m_spinControlCtrl.SetBuddy(&m_EditControl);
m_spinControlCtrl.SetRange(-55, 55);

I also strongly suggest learning to use Spy++ . 我也强烈建议学习使用Spy ++ This is how I actually arrived at this answer. 这就是我实际上得出这个答案的方式。 Using the resource editor I just dropped an edit control and an up-down control onto a dialog and used Spy++ to observe the default window styles. 使用资源编辑器,我只是将一个编辑控件和一个上下控件放到了对话框上,并使用Spy ++来观察默认的窗口样式。

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

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