在视图类的ONCreate中
m_spin.Create(WS_CHILD|WS_VISIBLE|UDS_ARROWKEYS|UDS_ALIGNRIGHT|UDS_WRAP,CRect(0,0,0,0),this,2);
m_spin.SetBuddy(&m_edit);
m_spin.SetRange(100,1);
m_spin.SetPos(10);然后在Onlbuttondown中:
CString str;;
str.Format("%d",m_spin.GetPos());
MessageBox(str);
发现m_spin.GetPos的值为65536,根本不对,但是如果在创建时指定UDS_SETBUDDYINT样式,那么一切正常,
这是问什么呢???????????????
m_spin.Create(WS_CHILD|WS_VISIBLE|UDS_ARROWKEYS|UDS_ALIGNRIGHT|UDS_WRAP,CRect(0,0,0,0),this,2);
m_spin.SetBuddy(&m_edit);
m_spin.SetRange(100,1);
m_spin.SetPos(10);然后在Onlbuttondown中:
CString str;;
str.Format("%d",m_spin.GetPos());
MessageBox(str);
发现m_spin.GetPos的值为65536,根本不对,但是如果在创建时指定UDS_SETBUDDYINT样式,那么一切正常,
这是问什么呢???????????????
当位置改变时,使控件设置伙伴窗口的文本(使用WM_SETTEXT消息)。文本是按十进制或十六进制格式化后的位置值。
你再看看GetPos的值是65536还是65546?如果是65546,可以将GetPos的返回值强制转换为short型来用。
另外,你为何不用UDS_SETBUDDYINT风格?
{
// TODO: Add your message handler code here and/or call default
CSpinButtonCtrl *p=(CSpinButtonCtrl*)pScrollBar;
CString str; if(p==&m_spin){
str.Format("%f",m_spin.GetPos()*0.1);
m_edit.SetWindowText(str);
}
CView::OnVScroll(nSBCode, nPos, pScrollBar);
}
这样就可以变成小数了,但是就在m_spin.GetPos()出了问题.所以就发了这个帖子.
int CShiyanView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
m_edit.Create(WS_CHILD|WS_VISIBLE|WS_BORDER,CRect(100,100,180,140),this,1);
m_edit.SetWindowText("1.0");
m_spin.Create(WS_CHILD|WS_VISIBLE|UDS_ARROWKEYS|UDS_ALIGNRIGHT|UDS_WRAP,CRect(0,0,0,0),this,2);
m_spin.SetBuddy(&m_edit);
m_spin.SetRange(100,10);
m_spin.SetPos(10);
return 0;
}void CShiyanView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
CSpinButtonCtrl *p=(CSpinButtonCtrl*)pScrollBar;
CString str; if(p==&m_spin){
str.Format("%.1f",((short)m_spin.GetPos())*0.1);
m_edit.SetWindowText(str);
}
CView::OnVScroll(nSBCode, nPos, pScrollBar);
}
When processing this message, the up-down control updates its current position based on the caption of the buddy window. The up-down control returns an error if there is no buddy window or if the caption specifies an invalid or out-of-range value. Also, an error flag is set in the HIWORD of the return if the control is created without the UDS_SETBUDDYINT style, even though it returns a valid position value in the LOWORD of the return.