一、关于spin控件按钮上下增加数出现想反的情况
spin 控件设定属性 auto buddy 和 set buddy integer 之后就会与最近的edit控件自动关联
然而会出现按上箭头减少,下箭头数增加的情况
解决该问题方法是: 调用 SetRange() 函数 就ok了
((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN))->SetRange(0, 100);
二、用函数获取控件checkbox 按钮的选定状态,用GetCheck函数出错
正确调用 :
int check = ((CButton *)GetDlgItem( IDC_CHECK)) -> GetCheck(); 就ok了
check 的值 可能为 0 、1 、2;
三、树形控件 CTreeViewCtrl
//添加父项和子项//treectrl const int DeptCount = 4;
CString CompName = _T("父项");
CString TreeHeader[DeptCount] = {_T("子项1"), _T("子项2"), _T(“子项3"), _T("子项4")};
HTREEITEM hTreeItem[DeptCount];
HTREEITEM hComp;
hComp = m_tree.InsertItem( CompName );
for (i = 0; i < DeptCount; ++i) {hTreeItem[i] = m_tree.InsertItem( TreeHeader[i], hComp );
m_tree.SetItemData(hTreeItem[i], (DWORD)(20 + i));
//设置每个子项对应的值 }
////获取每个子项的值
HTREEITEM SelItem;
SelItem = m_Tree.GetSelectedItem(); //所选择的项
DWORD w = m_Tree.GetItemData( SelItem ); //w为20为子项1 21为子项2
为树形控件添加图标
m_imgTree.Create(16, 16, ILC_COLOR4 | ILC_MASK, 4, 1); m_imgTree.Add( AfxGetApp()->LoadIcon( IDI_ICON_TREE_DEPT )); m_imgTree.Add( AfxGetApp()->LoadIcon( IDI_ICON_TREE_DEPTSEL )); m_imgTree.Add( AfxGetApp()->LoadIcon( IDI_ICON_TREE_PEOPLE )); m_imgTree.Add( AfxGetApp()->LoadIcon( IDI_ICON_TREE_PEOPLESEL )); m_tree.SetImageList( &m_imgTree, TVSIL_NORMAL ); InsertNodeToTree( TVI_ROOT, CDeptTree::GetInstance().GetDept(0) ); m_tree.Expand( m_tree.GetRootItem(), TVE_EXPAND ); HTREEITEM hChild = m_tree.GetNextItem( m_tree.GetRootItem(), TVGN_CHILD ); if (hChild) { m_hActive = hChild; m_tree.SelectItem( m_hActive ); m_tree.SetItemImage( m_hActive, 1, 1 ); //图标的序号为添加时的序号 } else m_hActive = m_tree.GetRootItem();
四、CToolBar 工具条的创建和图标的添加
//toolbar
m_toolbar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP | CBRS_ALIGN_ANY /*| CCS_NODIVIDER */ | CBRS_TOOLTIPS );
m_toolbar.LoadToolBar( IDR_TOOLBAR );
//工具条资源ID
UINT ID[] = {ID_TOOLBAR_1, ID_TOOLBAR_2, ID_SEPARATOR };
m_toolbar.SetButtons( ID, sizeof(ID)/sizeof(UINT) );
m_toolbar.SetButtonText(0, _T("保存"));
m_toolbar.SetButtonText(1, _T("取消"));
m_imageList.Create(16, 16, ILC_COLOR24|ILC_MASK, 4, 1);
//图标大小,颜色值,初始化个数为4,默认增加数为1
m_imageList.Add( AfxGetApp()->LoadIcon( IDI_ICON_SAVE ) );
//加载图标资源
m_imageList.Add( AfxGetApp()->LoadIcon( IDI_ICON_CANCEL ) );
m_toolbar.GetToolBarCtrl().SetImageList( &m_imageList);
m_toolbar.GetToolBarCtrl().SetButtonSize( CSize( BUTTON_WIDTH, TOOLBAR_HEIGHT ) );
//设置工具条宽度和高度(常量)
m_toolbar.SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW );
//设置工具条放置的位置
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0); //显示工具条
五、时间控件 DateTimePIckerCtrl
CTime t1 , t2;
用CTimeSpan ts=t1-t2;函数,得到两个时间的相差天数
//设置时间控件显示格式00:00 ((CDateTimeCtrl *)GetDlgItem( IDC_DATETIMEPICKER ))->SetFormat(_T("HH:mm"));
可用classwizard 为控件设置两个变量 control and value type
////////////设置控件的初始值
COleDateTime time;
CString strTime ;
strTime.Format( _T("%s:00"), m_pClass->GetOnTime() );
if ( time.ParseDateTime( strTime ) )
{
((CDateTimeCtrl *)GetDlgItem( IDC_DATETIMEPICKER_ONWORK ))->SetTime( time );
}
//////////设置控件时间日期的范围
COleDateTime t = COleDateTime::GetCurrentTime();
COleDateTime t1 = COleDateTime(t.GetYear()-50, 1, 1, 0, 0, 0);
COleDateTime t2 = COleDateTime(t.GetYear()+50, 12, GetDaysOfMonth( t.GetYear()+50, 12), 23, 59, 59 );
((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_OW_SD))->SetRange( &t1, &t2 );
((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_OW_ED))->SetRange( &t1, &t2 );
六、ListCtrl 控件
删除控件中的某一行后设置光标在下一行高亮显示:
m_ListCtrl.DeleteItem(nIndex);
delete pOver;
int items = m_ListCtrl.GetItemCount();
nIndex = nIndex == items? items-1:nIndex;
m_ListCtrl.SetFocus();
m_ListCtrl.SetItemState(nIndex,LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
m_ListCtrl.EnsureVisible(nIndex, FALSE);
七、手动添加状态栏
#define IDS_STRING_OPERATOR_NAME 1 #define IDS_INDICATOR_TIME 2
#define IDS_INDICATOR_RESERVE1 3 #define IDS_INDICATOR_RESERVE2 4 #define IDS_INDICATOR_RESERVE3 5 static UINT indicators[] = { IDS_STRING_OPERATOR_NAME, IDS_INDICATOR_TIME, IDS_INDICATOR_RESERVE1, IDS_INDICATOR_RESERVE2, IDS_INDICATOR_RESERVE3 };
//加载状态栏 { m_StausBar.Create(this); m_StausBar.SetIndicators(indicators, sizeof(indicators)/sizeof(int)); m_StausBar.SetPaneInfo(0, IDS_STRING_OPERATOR_NAME, SBPS_NORMAL, 160); //设置每一栏的宽度 m_StausBar.SetPaneInfo(1, IDS_INDICATOR_TIME, SBPS_NORMAL , 160); m_StausBar.SetPaneInfo(2, IDS_INDICATOR_RESERVE1, SBPS_NORMAL, 30); m_StausBar.SetPaneInfo(3, IDS_INDICATOR_RESERVE2, SBPS_NORMAL , 40); m_StausBar.SetPaneInfo(4, IDS_INDICATOR_RESERVE3, SBPS_STRETCH , 0); m_StausBar.SetPaneText(0, _T("操作员:系统管理员")); //设置文本 CTime t1 = CTime::GetCurrentTime(); m_StausBar.SetPaneText(1, t1.Format("%Y-%m-%d %H:%M:%S")); RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,IDS_INDICATOR_RESERVE3); m_StausBar.ShowWindow( SW_SHOW ); SetTimer( 1, 1000, NULL ); }