时间:04/06/2006
LVS_SMALLICON: 为每个item显示小图标
LVS_LIST: 显示一列带有小图标的item
LVS_REPORT: 显示item详细资料
lStyle. = GetWindowLong(m_list.m_hWnd, GWL_STYLE);//获取当前窗口style
lStyle. &= ~LVS_TYPEMASK; //清除显示方式位
lStyle.|= LVS_REPORT; //设置style
SetWindowLong(m_list.m_hWnd, GWL_STYLE, lStyle);//设置style
DWORD dwStyle. = m_list.GetExtendedStyle();
dwStyle.|= LVS_EX_FULLROWSELECT;//选中某行使整行高亮(只适用与report风格的listctrl)
dwStyle.|= LVS_EX_GRIDLINES;//网格线(只适用与report风格的listctrl)
dwStyle.|= LVS_EX_CHECKBOXES;//item前生成checkbox控件
m_list.SetExtendedStyle(dwStyle); //设置扩展风格
注:listview的style请查阅msdn
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceshellui5/html/wce50lrflistviewstyles.asp
m_list.InsertColumn( 1, "NAME", LVCFMT_LEFT, 50 );
int nRow = m_list.InsertItem(0, “11”);//插入行
m_list.SetItemText(nRow, 1, “jacky”);//设置数据
选中style中的Show selection always,或者在上面第2点中设置LVS_SHOWSELALWAYS
//选中
m_list.SetItemState(nIndex, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
//取消选中
m_list.SetItemState(nIndex, 0, LVIS_SELECTED|LVIS_FOCUSED);
CString str;
for(int i=0; i<m_list.GetItemCount(); i++)
{
if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED || m_list.GetCheck(i))
{
str.Format(_T("第%d行的checkbox为选中状态"), i);
AfxMessageBox(str);
}
}
方法一:
CString str;
for(int i=0; i<m_list.GetItemCount(); i++)
{
if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED )
{
str.Format(_T("选中了第%d行"), i);
AfxMessageBox(str);
}
}
POSITION pos = m_list.GetFirstSelectedItemPosition();
if (pos == NULL)
TRACE0("No items were selected!/n");
else
{
while (pos)
{
int nItem = m_list.GetNextSelectedItem(pos);
TRACE1("Item %d was selected!/n", nItem);
// you could do your own processing on nItem here
}
}
LVITEM lvi;
lvi.iItem = nItemIndex;
lvi.iSubItem = 0;
lvi.mask = LVIF_TEXT;
lvi.pszText = szBuf;
lvi.cchTextMax = 1024;
m_list.GetItem(&lvi);
Q173242: Use Masks to Set/Get Item States in CListCtrl
http://support.microsoft.com/kb/173242/en-us
char str[256];
int nColNum;
CString strColumnName[4];//假如有4列
lvcol.mask = LVCF_TEXT;
lvcol.pszText = str;
lvcol.cchTextMax = 256;
while(m_list.GetColumn(nColNum, &lvcol))
{
strColumnName[nColNum] = lvcol.pszText;
nColNum++;
}
m_list.EnsureVisible(i, FALSE);
int nHeadNum = m_list.GetHeaderCtrl()->GetItemCount();
while ( m_list.DeleteColumn (0))
因为你删除了第一列后,后面的列会依次向上移动。
int nColumns = 4;
for (int i=nColumns-1; i>=0; i--)
m_list.DeleteColumn (i);
void CTest6Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
// 方法一:
/*
DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
m_list.ScreenToClient(&point);
LVHITTESTINFO lvinfo;
lvinfo.pt = point;
lvinfo.flags = LVHT_ABOVE;
int nItem = m_list.SubItemHitTest(&lvinfo);
if(nItem != -1)
{
CString strtemp;
strtemp.Format("单击的是第%d行第%d列", lvinfo.iItem, lvinfo.iSubItem);
AfxMessageBox(strtemp);
}
*/
// 方法二:
/*
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(pNMListView->iItem != -1)
{
CString strtemp;
strtemp.Format("单击的是第%d行第%d列",
pNMListView->iItem, pNMListView->iSubItem);
AfxMessageBox(strtemp);
}
*/
*pResult = 0;
}
void CTest6Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
m_list.ScreenToClient(&point);
LVHITTESTINFO lvinfo;
lvinfo.pt = point;
lvinfo.flags = LVHT_ABOVE;
UINT nFlag;
int nItem = m_list.HitTest(point, &nFlag);
//判断是否点在checkbox上
if(nFlag == LVHT_ONITEMSTATEICON)
{
AfxMessageBox("点在listctrl的checkbox上");
}
*pResult = 0;
}
void CTest6Dlg::OnRclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(pNMListView->iItem != -1)
{
DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
CMenu menu;
VERIFY( menu.LoadMenu( IDR_MENU1 ) );
CMenu* popup = menu.GetSubMenu(0);
ASSERT( popup != NULL );
popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this );
}
*pResult = 0;
}
void CTest6Dlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
CString sTemp;
if((pNMListView->uOldState & LVIS_FOCUSED) == LVIS_FOCUSED &&
(pNMListView->uNewState & LVIS_FOCUSED) == 0)
{
sTemp.Format("%d losted focus",pNMListView->iItem);
}
else if((pNMListView->uOldState & LVIS_FOCUSED) == 0 &&
(pNMListView->uNewState & LVIS_FOCUSED) == LVIS_FOCUSED)
{
sTemp.Format("%d got focus",pNMListView->iItem);
}
if((pNMListView->uOldState & LVIS_SELECTED) == LVIS_SELECTED &&
(pNMListView->uNewState & LVIS_SELECTED) == 0)
{
sTemp.Format("%d losted selected",pNMListView->iItem);
}
else if((pNMListView->uOldState & LVIS_SELECTED) == 0 &&
(pNMListView->uNewState & LVIS_SELECTED) == LVIS_SELECTED)
{
sTemp.Format("%d got selected",pNMListView->iItem);
}
*pResult = 0;
}
http://www.codeproject.com/threads/int64_memsteal.asp
Q131284: How To Select a Listview Item Programmatically
http://support.microsoft.com/kb/131284/en-us
m_list.SetItem(..); //具体参数请参考msdn
BOOL CTest6Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
HIMAGELIST himlSmall;
HIMAGELIST himlLarge;
SHFILEINFO sfi;
char cSysDir[MAX_PATH];
CString strBuf;
memset(cSysDir, 0, MAX_PATH);
GetWindowsDirectory(cSysDir, MAX_PATH);
strBuf = cSysDir;
sprintf(cSysDir, "%s", strBuf.Left(strBuf.Find("//")+1));
himlSmall = (HIMAGELIST)SHGetFileInfo ((LPCSTR)cSysDir,
0,
&sfi,
sizeof(SHFILEINFO),
SHGFI_SYSICONINDEX | SHGFI_SMALLICON );
himlLarge = (HIMAGELIST)SHGetFileInfo((LPCSTR)cSysDir,
0,
&sfi,
sizeof(SHFILEINFO),
SHGFI_SYSICONINDEX | SHGFI_LARGEICON);
if (himlSmall && himlLarge)
{
::SendMessage(m_list.m_hWnd, LVM_SETIMAGELIST,
(WPARAM)LVSIL_SMALL, (LPARAM)himlSmall);
::SendMessage(m_list.m_hWnd, LVM_SETIMAGELIST,
(WPARAM)LVSIL_NORMAL, (LPARAM)himlLarge);
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CTest6Dlg::AddFiles(LPCTSTR lpszFileName, BOOL bAddToDocument)
{
int nIcon = GetIconIndex(lpszFileName, FALSE, FALSE);
CString strSize;
CFileFind filefind;
// get file size
if (filefind.FindFile(lpszFileName))
{
filefind.FindNextFile();
strSize.Format("%d", filefind.GetLength());
}
else
strSize = "0";
// split path and filename
CString strFileName = lpszFileName;
CString strPath;
int nPos = strFileName.ReverseFind('//');
if (nPos != -1)
{
strPath = strFileName.Left(nPos);
strFileName = strFileName.Mid(nPos + 1);
}
// insert to list
int nItem = m_list.GetItemCount();
m_list.InsertItem(nItem, strFileName, nIcon);
m_list.SetItemText(nItem, 1, strSize);
m_list.SetItemText(nItem, 2, strFileName.Right(3));
m_list.SetItemText(nItem, 3, strPath);
}
int CTest6Dlg::GetIconIndex(LPCTSTR lpszPath, BOOL bIsDir, BOOL bSelected)
{
SHFILEINFO sfi;
memset(&sfi, 0, sizeof(sfi));
if (bIsDir)
{
SHGetFileInfo(lpszPath,
FILE_ATTRIBUTE_DIRECTORY,
&sfi,
sizeof(sfi),
SHGFI_SMALLICON | SHGFI_SYSICONINDEX |
SHGFI_USEFILEATTRIBUTES |(bSelected ? SHGFI_OPENICON : 0));
return sfi.iIcon;
}
else
{
SHGetFileInfo (lpszPath,
FILE_ATTRIBUTE_NORMAL,
&sfi,
sizeof(sfi),
SHGFI_SMALLICON | SHGFI_SYSICONINDEX |
SHGFI_USEFILEATTRIBUTES | (bSelected ? SHGFI_OPENICON : 0));
return sfi.iIcon;
}
return -1;
}
//更新内容
m_list.SetRedraw(TRUE);
m_list.Invalidate();
m_list.UpdateWindow();
或者参考
http://support.microsoft.com/kb/250614/en-us
Q141834: How to change the icon or the bitmap of a CListCtrl item in Visual C++
http://support.microsoft.com/kb/141834/en-us
http://support.microsoft.com/kb/151897/en-us
具体解释参阅 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/listview/structures/lvcolumn.asp
http://www.codeguru.com/cpp/controls/listview/advanced/article.php/c4151/
http://www.codeproject.com/listctrl/virtuallist.asp
http://support.microsoft.com/kb/125694/en-us
http://support.microsoft.com/kb/234310/en-us
PRB: OnTimer() Is Not Called Repeatedly for a List Control
http://support.microsoft.com/kb/200054/en-us
http://www.codeproject.com/listctrl/dragtest.asp
http://support.microsoft.com/kb/148738/en-us
(2) 多功能listctrl
支持subitem可编辑,图标,radiobutton,checkbox,字符串改变颜色的类
http://www.codeproject.com/listctrl/quicklist.asp
支持排序,subitem可编辑,subitem图标,subitem改变颜色的类
http://www.codeproject.com/listctrl/ReportControl.asp
http://www.codeproject.com/listctrl/CListCtrlLink.asp
http://www.codeproject.com/listctrl/ctooltiplistctrl.asp
http://www.codeproject.com/listctrl/ProgressListControl.asp
http://www.codeproject.com/listctrl/napster.asp
http://www.codeguru.com/Cpp/controls/listview/article.php/c4187/
http://www.codeproject.com/listctrl/highlightlistctrl.asp
http://www.codeguru.com/Cpp/controls/listbox/colorlistboxes/article.php/c4757/
(7) 类vb属性对话框
http://www.codeproject.com/listctrl/propertylistctrl.asp
http://www.codeguru.com/Cpp/controls/listview/propertylists/article.php/c995/
http://www.codeguru.com/Cpp/controls/listview/propertylists/article.php/c1041/
(8) 选中subitem(只高亮选中的item)
http://www.codeproject.com/listctrl/SubItemSel.asp
http://www.codeproject.com/listctrl/ListSubItSel.asp
(9) 改变行高
http://www.codeproject.com/listctrl/changerowheight.asp
(10) 改变行颜色
http://www.codeproject.com/listctrl/coloredlistctrl.asp
(11) 可编辑subitem的listctrl
http://www.codeproject.com/listctrl/nirs2000.asp
http://www.codeproject.com/listctrl/editing_subitems_in_listcontrol.asp
(12) subitem可编辑,插入combobox,改变行颜色,subitem的tooltip提示
http://www.codeproject.com/listctrl/reusablelistcontrol.asp
(13) header 中允许多行字符串
http://www.codeproject.com/listctrl/headerctrlex.asp
(14) 插入combobox
http://www.codeguru.com/Cpp/controls/listview/editingitemsandsubitem/article.php/c979/
(15) 添加背景图片
http://www.codeguru.com/Cpp/controls/listview/backgroundcolorandimage/article.php/c4173/
http://www.codeguru.com/Cpp/controls/listview/backgroundcolorandimage/article.php/c983/
http://www.vchelp.net/vchelp/archive.asp?type_id=9&class_id=1&cata_id=1&article_id=1088&search_term=
(16) 自适应宽度的listctrl
http://www.codeproject.com/useritems/AutosizeListCtrl.asp
处理
NM_CUSTOMDRAW
http://www.codeproject.com/listctrl/lvcustomdraw.asp
(18) 改变header颜色
http://www.pocketpcdn.com/articles/hdr_color.html
【文章来源】http://www.6suv.com/20070816/37356/
// 创建快捷方式
// 如何使用
CreateLink("c://a.txt", "d://a.lnk");
{
AfxOleInit();
......
{
CView::OnInitialUpdate();
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");//创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb","","",adModeUnknown);///连接数据库
///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51; }
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!/r/n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
}
{
m_pConnection->Close();
m_pConnection.Release();
}
{
_variant_t RecordsAffected;
m_pConnection->Execute("CREATE TABLE new(ID INTEGER,username TEXT,old INTEGER)",&RecordsAffected,adCmdText);
}
{
_variant_t RecordsAffected;
m_pConnection->Execute("DROP TABLE new",&RecordsAffected,adCmdText);
}
{
_variant_t RecordsAffected;
m_pConnection->Execute("ALTER TABLE new ADD newcolumn1 INTEGER",&RecordsAffected,adCmdText);
}
{
_variant_t RecordsAffected;
m_pConnection->Execute("ALTER TABLE new DROP newcolumn1 INTEGER",&RecordsAffected,adCmdText);
}
{
_variant_t RecordsAffected;
for(int i = 1;i < 10; i ++)
{
CString strSQL;
strSQL.Format("INSERT INTO new(ID,username,old) VALUES (%d, 'Washington',%d)",i,i*9);
m_pConnection->Execute((_bstr_t)strSQL,&RecordsAffected,adCmdText);
}
{
_variant_t RecordsAffected;
m_pConnection->Execute("UPDATE new SET ld= old+1",&RecordsAffected,adCmdText);
}
{
_RecordsetPtr m_pRecordset;
_variant_t RecordsAffected;
m_pRecordset =m_pConnection->Execute("SELECT COUNT(*) FROM new where ID > 0",&RecordsAffected,adCmdText);
_variant_t vIndex = (long)0;
_variant_t vCount = m_pRecordset->GetCollect(vIndex);
///取得第一个字段的值放入vCount变量
m_pRecordset->Close();///关闭记录集
CString Message;
Message.Format("共有%d条记录",vCount.lVal);
AfxMessageBox(Message);///显示当前记录条数
}
{
_variant_t RecordsAffected;
m_pConnection->Execute("CREATE UNIQUE INDEX id ON new(ID)",&RecordsAffected,adCmdText);
}
{
_RecordsetPtr m_pRecordset;
_variant_t RecordsAffected;
m_pRecordset =m_pConnection->Execute("select MAX(old) from new",&RecordsAffected,adCmdText);
_variant_t vIndex = (long)0;
_variant_t vCount = m_pRecordset->GetCollect(vIndex);
///取得第一个字段的值放入vCount变量
m_pRecordset->Close();///关闭记录集
m_pRecordset.Release();
CString Message;
Message.Format("最大值是%d",vCount.lVal);
AfxMessageBox(Message);
}
void CTestadoView::OnOldMin()
{
_RecordsetPtr m_pRecordset;
_variant_t RecordsAffected;
m_pRecordset =m_pConnection->Execute("select MIN(old) from new",&RecordsAffected,adCmdText);
_variant_t vIndex = (long)0;
_variant_t vCount = m_pRecordset->GetCollect(vIndex);
///取得第一个字段的值放入vCount变量
m_pRecordset->Close();///关闭记录集
m_pRecordset.Release();
CString Message;
Message.Format("最小值是%d",vCount.lVal);
AfxMessageBox(Message);
}
{
_RecordsetPtr m_pRecordset;
_variant_t RecordsAffected;
m_pRecordset =m_pConnection->Execute("select SUM(old) from new",&RecordsAffected,adCmdText);
_variant_t vIndex = (long)0;
_variant_t vCount = m_pRecordset->GetCollect(vIndex);
///取得第一个字段的值放入vCount变量
m_pRecordset->Close();///关闭记录集
m_pRecordset.Release();
CString Message;
Message.Format("总和是%d",(long)vCount);
AfxMessageBox(Message);
}
{
_RecordsetPtr m_pRecordset;
_variant_t RecordsAffected;
m_pRecordset =m_pConnection->Execute("select AVG(old) from new",&RecordsAffected,adCmdText);
_variant_t vIndex = (long)0;
_variant_t vCount = m_pRecordset->GetCollect(vIndex);
///取得第一个字段的值放入vCount变量
m_pRecordset->Close();///关闭记录集
m_pRecordset.Release();
CString Message;
Message.Format("平均值是%d",(long)vCount);
AfxMessageBox(Message);
}}
2. 在需要使用时,通过FindReourse, LoadResource, LockResource加载资源。LockResource返回的资源在内存中的指针可以直接用于将资源数据(这里也就是您的数据文件数据)写入磁盘文件。
注:应该注意不能对由LoadResource返回的HGLOBAL使用GlobalLock,只能使用LockResource返回资源的内存指针。另外没有必要在使用完资源后再调用UnlockResource。
下面是加载资源的自定义函数LoadZIP()
BOOL LoadZIP()
{
HRSRC hrc = FindResource(
NULL,
MAKEINTRESOURCE(IDR_ZIP1),
_T("ZIP"));
if(hrc == NULL)
return FALSE;
HGLOBAL hGlobal = LoadResource(
NULL,
hrc);
if(hGlobal == NULL)
return FALSE;
//create temp file in system temp directory
_TCHAR pszTempPath[MAX_PATH] = {0};
if(0 >= GetTempPath(
MAX_PATH,
pszTempPath))
{
return FALSE;
}
CString strFilePath(pszTempPath);
DWORD dwSize = ::SizeofResource(
NULL,
hrc);
LPVOID pData = ::LockResource(hGlobal);
//write to file
strFilePath.TrimRight(_T("//"));
CString strFileName;
strFileName.Format(_T(
strFilePath += strFileName;
CFile file;
if(!file.Open(strFilePath, CFile::modeCreate | CFile::modeWrite))
{
return FALSE;
}
file.WriteHuge(pData, dwSize);
file.Close();
}
【文章来源】http://www.czvc.com/view.asp?id=5
CSize sz=dc.GetTextExtent(str); //设置一个CClientDC对象来获取str的长度
CString str=t.Format("%H:%M:%S"); //格式可以参考MSDN中的strftime函数
int index=0;
index=m_wndStatusBar.CommandToIndex(IDS_TIMER); //此处也可以直接输入IDS_TIMER在indicators数组中从0开始的序号。
m_wndStatusBar.SetPaneText(index,str); //设置IDS_TIMER指示器字符串
m_progress.SetPos(50); /*进度栏显示为50%*/
m_wndStatusBar.GetItemRect(m_wndStatusBar.CommandToIndex(IDS_PROGRESS),&rect); /*获取IDS_PROGRESS栏位的CRect值,付给rect*/
m_progress.Create(WS_CHILD|WS_VISIBLE, rect, &m_wndStatusBar, IDS_PROGRESS); /*在IDS_PROGRESS的位置显示一个50%的进度栏*/
m_progress.SetPos(50);
//{{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnViewNewtool();
afx_msg void OnUpdateViewNewtool(CCmdUI* pCmdUI);
//}}AFX_MSG
afx_msg void OnProgress(); //定义消息响应原形
DECLARE_MESSAGE_MAP()
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_WM_TIMER()
ON_COMMAND(IDM_VIEW_NEWTOOL, OnViewNewtool)
ON_UPDATE_COMMAND_UI(IDM_VIEW_NEWTOOL, OnUpdateViewNewtool)
//}}AFX_MSG_MAP
ON_MESSAGE(UM_PROGRESS,OnProgress) //添加消息映射
END_MESSAGE_MAP()
{
CRect rect;
m_wndStatusBar.GetItemRect(m_wndStatusBar.CommandToIndex(IDS_PROGRESS),&rect);
m_progress.Create(WS_CHILD|WS_VISIBLE, rect, &m_wndStatusBar, IDS_PROGRESS);
m_progress.SetPos(50);
}
这时在IDS_PROGRESS状态栏中就能看到进度栏了。但是还存在问题,当改变窗口大小时,进度栏会离开IDS_PROGRESS栏位位置。
m_wndStatusBar.GetItemRect(m_wndStatusBar.CommandToIndex(IDS_PROGRESS),&rect);
if (!m_progress.m_hWnd) /*注意这里必须增加一个判断,否则在窗体再次发生重绘时,
{
m_progress.Create(WS_CHILD|WS_VISIBLE|PBS_SMOOTH , rect, &m_wndStatusBar, IDS_PROGRESS);
}
else
{
m_progress.MoveWindow(rect); /*移动窗口,比SetWindowPos简单。*/
}
m_progress.SetPos(50);
str.Format("x=%d,y=%d",point.x,point.y); /*对鼠标坐标进行格式化*/
((CMainFrame*)GetParent())->m_wndStatusBar.SetWindowText(str); /*由于作用的是View窗口的父窗口MainFrame下的m_wndStatusBar对象,所以必须将m_wndStatusBar设置为public,然后用(CMainFrame*)指名其为框架类指针,否则->后面不会出现m_wndStatusBar对象。注意在View程序中必须包含MainFrame的头文件*/
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,设置一个指示器数组
sizeof(indicators)/sizeof(UINT)))//设定数组中的元素
{
TRACE0("Failed to create status bar/n");
return -1; // fail to create
static UINT indicators[] =
{
ID_SEPARATOR, // 状态栏的最长的一条
IDS_TIMER,//我们添加的一个时间的项 (要在字符串资源中加入两个项分别表示时间与进度)
IDS_PROGRESS,//我们添加的一个进度的项
ID_INDICATOR_CAPS,//键盘大小写的项
ID_INDICATOR_NUM,//键盘数字的项
ID_INDICATOR_SCRL,//键盘滚动的项
};为了在状态栏里加入一个时间,我们想到了类CTime里有一个GetCurrentTime()成员函数得到当前的时间,然后用我们这个类下的Format方法可以将时间进行格式化返回一个CString的对象,用%D ( Total days in this CTime) %H (Hours in the current day) %M (Minutes in the current hour) %S ( Seconds in the current minute)方式格式化,例子如下
CTime t=CTime::GetCurrentTime();//定义一个CTime 的对象t,并调用CTime::GetCurrentTime()函数对t进行给值.
CString str;//定义一个strr字符串
str=t.Format("%H:%M:%S");//并调用t.Format("%H:%M:%S")函数对strt进行格式化
CClientDC dc(this);//构造一个DC
CSize sz=dc.GetTextExtent(str);//得到
int index=0;
index=m_wndStatusBar.CommandToIndex(IDS_TIMER);
m_wndStatusBar.SetPaneInfo(index,IDS_TIMER,SBPS_NORMAL,sz.cx);//调用SetPaneInfo获得一个时钟的长度
m_wndStatusBar.SetPaneText(index,str);
但是这样子只能得到一个静止的时间,于是我们要调用SetTimer()在CMainFrame.:OnTimer(UINT nIDEvent) 下添加以下代码就可以实现一个时间而不再静止时间了.
CTime t=CTime::GetCurrentTime();
CString str=t.Format("%H:%M:%S");
CClientDC dc(this);
CSize sz=dc.GetTextExtent(str);
m_wndStatusBar.SetPaneInfo(1,IDS_TIMER,SBPS_NORMAL,sz.cx);
m_wndStatusBar.SetPaneText(1,str);
我们知道,view窗口始终是在框架窗口之上,所以我们到C**DView类里添加一个OnMouseMove(UINT nFlags, CPoint point)函数的消息响应,在这个函数里,我们的步骤如下.
1、 CString str;
2、 str.Format("x=%d,y=%d",point.x,point.y);//对鼠标点的X,Y坐标进行格式化
3、有四种方法来实现把鼠标移动时坐标点位置显示在状态栏的第一个面板上
A、((CMainFrame*)GetParent())->m_wndStatusBar.SetWindowText(str);因为框架窗口是view窗口父类,首先得到一个父窗口GetParent(),又因为GetParent()返回的是CWnd指针,机时这里要得到是一个CMainFrame的指针,所以要用(CMainFrame*)对GetParent()强制转换,然后调用m_wndStatusBar的成员变量,同时要把m_wndStatusBar定义改为Public的成员变量,最后用SetWindowText(str)(设置窗口文本)函数将鼠标移动时坐标点位置到状态栏的面板上,这样就可以把鼠标移动时坐标点位置显示在状态栏的第一个面板上.
B、((CMainFrame*)GetParent())->SetMessageText(str);由于这个函数是将鼠标移动时坐标点位置直接放到状态栏面板上,于是,就不必调用m_wndStatusBar的成员变量,
C、((CMainFrame*)GetParent())->GetMessageBar()->SetWindowText(str);GetMessageBar()可以返回一个指向状态栏窗口的指针,我们就可直接调用GetMessageBar()获得一个状态栏窗口的指针,接着再去