MyCombo.cpp
资源名称:44757463.rar [点击查看]
上传用户:lj3531212
上传日期:2007-06-18
资源大小:346k
文件大小:3k
源码类别:
绘图程序
开发平台:
Visual C++
- // MyCombo.cpp : implementation file
- //
- #include "stdafx.h"
- #include "GraphSoft.h"
- #include "MyCombo.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMyCombo
- CMyCombo::CMyCombo()
- {
- }
- CMyCombo::~CMyCombo()
- {
- }
- BEGIN_MESSAGE_MAP(CMyCombo, CComboBox)
- //{{AFX_MSG_MAP(CMyCombo)
- ON_WM_KEYDOWN()
- ON_WM_KEYUP()
- ON_WM_SYSKEYDOWN()
- ON_WM_SYSKEYUP()
- ON_WM_CHAR()
- ON_CONTROL_REFLECT(CBN_EDITCHANGE, OnEditchange)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMyCombo message handlers
- void CMyCombo::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- // TODO: Add your message handler code here and/or call default
- if(nChar>=(UINT)'0'&&nChar<=(UINT)'9')
- CComboBox::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- void CMyCombo::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- // TODO: Add your message handler code here and/or call default
- if(nChar>=(UINT)'0'&&nChar<=(UINT)'9')
- CComboBox::OnKeyUp(nChar, nRepCnt, nFlags);
- }
- void CMyCombo::OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- // TODO: Add your message handler code here and/or call default
- if(nChar>=(UINT)'0'&&nChar<=(UINT)'9')
- CComboBox::OnSysKeyDown(nChar, nRepCnt, nFlags);
- }
- void CMyCombo::OnSysKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- // TODO: Add your message handler code here and/or call default
- if(nChar>=(UINT)'0'&&nChar<=(UINT)'9')
- CComboBox::OnSysKeyUp(nChar, nRepCnt, nFlags);
- }
- void CMyCombo::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- // TODO: Add your message handler code here and/or call default
- if(nChar>=(UINT)'0'&&nChar<=(UINT)'9')
- CComboBox::OnChar(nChar, nRepCnt, nFlags);
- }
- void CMyCombo::OnEditchange()
- {
- // TODO: Add your control notification handler code here
- CString m_str;
- GetWindowText(m_str);
- if(m_str.GetLength()>0)
- {
- if(IsNumber(m_str))
- {
- if(atoi(m_str)>999){
- SetWindowText(m_str.Left(3));
- SetEditSel(3,3);
- }else if(atoi(m_str)<-999){//多了一个‘-’号
- SetWindowText(m_str.Left(4));
- SetEditSel(4,4);
- }
- }
- else
- {
- SetWindowText(GetNumberStr(m_str));
- SetEditSel(GetNumberStr(m_str).GetLength(),GetNumberStr(m_str).GetLength());
- }
- }
- }
- BOOL CMyCombo::PreTranslateMessage(MSG* pMsg)
- {
- // TODO: Add your specialized code here and/or call the base class
- return CComboBox::PreTranslateMessage(pMsg);
- }
- BOOL CMyCombo::IsNumber(CString str)
- {
- int i,j;
- i=0;
- j=str.GetLength();
- if((str[0]<'0'||str[0]>'9')&&str[0]!='-')
- return FALSE;
- for(i=1;i<j;i++)
- {
- if(str[i]<'0'||str[i]>'9')
- return FALSE;
- }
- return TRUE;
- }
- CString CMyCombo::GetNumberStr(CString str)
- {
- int i,j,k;
- char* m_pchar=new char[str.GetLength()];
- k=0;
- j=str.GetLength();
- i=0;
- if(str[0]=='-'){
- i=1;
- m_pchar[k++]='-';
- }
- for(;i<j;i++)
- {
- if(str[i]<'0'||str[i]>'9')
- {
- }
- else
- {
- m_pchar[k++]=str[i];
- }
- }
- m_pchar[k]=' ';
- CString m_str(m_pchar);
- delete[] m_pchar;
- return m_str;
- }