www.pudn.com > CListCtrlTest.rar > CListCtrlTestDlg.cpp
// CListCtrlTestDlg.cpp : implementation file // #include "stdafx.h" #include "CListCtrlTest.h" #include "CListCtrlTestDlg.h" #include#include using namespace std; #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCListCtrlTestDlg dialog CCListCtrlTestDlg::CCListCtrlTestDlg(CWnd* pParent /*=NULL*/) : CDialog(CCListCtrlTestDlg::IDD, pParent) { //{{AFX_DATA_INIT(CCListCtrlTestDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CCListCtrlTestDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CCListCtrlTestDlg) DDX_Control(pDX, IDC_LIST1, m_MyListCtrl); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CCListCtrlTestDlg, CDialog) //{{AFX_MSG_MAP(CCListCtrlTestDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_ADD, OnAdd) ON_BN_CLICKED(IDC_BUTTON3, OnButton3) ON_BN_CLICKED(IDC_CREATE, OnCreate) ON_BN_CLICKED(IDC_BKCOLOR, OnBkcolor) ON_BN_CLICKED(IDC_TEXTCOLOR, OnTextcolor) ON_BN_CLICKED(IDC_GRIDPRO, OnGridpro) ON_BN_CLICKED(IDC_GRIDCOLOR, OnGridcolor) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCListCtrlTestDlg message handlers BOOL CCListCtrlTestDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here CString strname[3]; strname[0]="Screen Name"; strname[1]="Form ID"; strname[2]="Category Path"; for(int k = 0; k < 3; k++) { m_MyListCtrl.InsertColumn(k, strname[k], LVCFMT_LEFT, 164 );//LVCFMT_RIGHT, or LVCFMT_CENTER. } m_MyListCtrl.SetColumnCount(k); //调用自定义函数设置列数量 // DWORD dwStyle = ::GetWindowLong(m_MyListCtrl.m_hWnd, GWL_STYLE); // ::SetWindowLong(m_MyListCtrl.m_hWnd, GWL_STYLE, dwStyle|LVS_REPORT ); // DWORD style = m_MyListCtrl.GetExtendedStyle(); // m_MyListCtrl.SetExtendedStyle(style | LVS_EX_GRIDLINES); // m_MyListCtrl.LockWindowUpdate();//禁止本listbox刷新。 // for(int j=0;j<9999;j++ ) // { // m_MyListCtrl.AddString("test"); // m_MyListCtrl.InsertItem(); // }//添加数据。 // this->RedrawWindow(NULL,NULL,RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE); /* CListCtrl* pmyListCtrl = & m_MyListCtrl; CString strText; int nColumnCount = pmyListCtrl->GetHeaderCtrl()->GetItemCount(); // Insert 10 items in the list view control. for (int i=0;i < 10;i++) { strText.Format(TEXT("item %d"), i); // Insert the item, select every other item. pmyListCtrl->InsertItem( LVIF_TEXT|LVIF_STATE, i, strText, (i%2)==0 ? LVIS_SELECTED : 0, LVIS_SELECTED, 0, 0); // Initialize the text of the subitems. for (int j=1;j < nColumnCount;j++) { strText.Format(TEXT("sub-item %d %d"), i, j); pmyListCtrl->SetItemText(i, j, strText); } } */ // m_MyListCtrl.SetExtendedStyle( LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT/*|LVS_EX_CHECKBOXES*/); // for(int i = 0; i < 10; i++) // { // m_MyListCtrl.InsertItem(i, "abc"); // } // for(int kk=0;kk GetDC(); textsize = dc3->GetTextExtent(str3); if(textsize.cx != 0) { if(textsize.cx > firstrect.right - firstrect.left) m_MyListCtrl.SetColumnWidth(k, 20); } this->ReleaseDC(dc3); */ m_MyListCtrl.Invalidate(); //调用重绘窗口 CRect * rect11 = new CRect; m_MyListCtrl.InsertItem(k, str.c_str()); if(k == 3 || k == 1) { m_MyListCtrl.GetDrawTextRect(rect11, str3, k, 0, true); m_MyListCtrl.AddTextInfo(str3, k, 0, *rect11, true, RGB(255, 0, 0), true); } else { m_MyListCtrl.GetDrawTextRect(rect11, str3, k, 0, false); m_MyListCtrl.AddTextInfo(str3, k, 0, *rect11, false, RGB(255, 255, 255), false); } delete rect11; /* CDC * cdc1 = m_MyListCtrl.GetDC(); size = cdc1->GetTextExtent(str3); m_MyListCtrl.ReleaseDC(cdc1); */ /* if(m_MyListCtrl.GetItemRect(k, &rowrect, LVIR_BOUNDS)) //得到每一行的矩形区域 { width = m_MyListCtrl.GetColumnWidth(k); firstrect = rowrect; firstrect.right += width; //第一列的矩形区域坐标 } else { AfxMessageBox("添加数据异常!"); return; } */ getline(cin2, str); str1.Format("%s", str.c_str() ); if(str1.Find("??") != -1) { str2 = str1.Right(str1.GetLength() - str1.Find("??")-2); str3 = str1.Left(str1.Find("??")); char * p = str3.GetBuffer(str3.GetLength()); str = string(p); str3.ReleaseBuffer(); } else str3.Format("%s", str.c_str()); m_MyListCtrl.SetItemText(k,1,str.c_str()); CRect * rect12 = new CRect; if(k == 3) { m_MyListCtrl.GetDrawTextRect(rect12, str3, k, 1, false); m_MyListCtrl.AddTextInfo(str3, k, 1, *rect12, false, RGB(0, 255, 0), true); } else { m_MyListCtrl.GetDrawTextRect(rect12, str3, k, 1, true); m_MyListCtrl.AddTextInfo(str3, k, 1, *rect12, true, RGB(0, 0, 255), true); } delete rect12; getline(cin2, str); str1.Format("%s", str.c_str() ); if(str1.Find("??") != -1) { str2 = str1.Right(str1.GetLength() - str1.Find("??")-2); str3 = str1.Left(str1.Find("??")); char * p = str3.GetBuffer(str3.GetLength()); str = string(p); str3.ReleaseBuffer(); } else str3.Format("%s", str.c_str()); m_MyListCtrl.SetItemText(k,2,str.c_str()); CRect * rect13 = new CRect; m_MyListCtrl.GetDrawTextRect(rect13, str3, k, 2, false); m_MyListCtrl.AddTextInfo(str3, k, 2, *rect13, false, RGB(0, 0, 0), false); delete rect13; } cin.close(); m_MyListCtrl.UpdateData(); } void CCListCtrlTestDlg::OnButton3() { // TODO: Add your control notification handler code here CClientDC dc(this); CBrush brush(RGB(0, 128, 128)); CBrush * DefBrush = dc.SelectObject(&brush); dc.Rectangle(0, 0, 100, 100); dc.SelectObject(DefBrush); } void CCListCtrlTestDlg::ViewBitmap(const char* filepath, const CPoint point, const int &width, const int &hight) { CBitmap bmp; HBITMAP hbmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), filepath, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION|LR_LOADFROMFILE); if (hbmp != NULL) { bmp.Attach(hbmp); CDC dcMemory, *pDC; pDC = this->GetDC(); dcMemory.CreateCompatibleDC(pDC); // Select the bitmap into the in-memory DC CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp); pDC->BitBlt(point.x, point.y, width, hight, &dcMemory, 0, 0, SRCCOPY); dcMemory.SelectObject(pOldBitmap); this->ReleaseDC(pDC); this->ReleaseDC(&dcMemory); } else { TRACE0("ERROR: Where's IDB_BITMAP1?\n"); AfxMessageBox("bitmap loading fail!"); } } void CCListCtrlTestDlg::OnCreate() { // TODO: Add your control notification handler code here m_MyListCtrl.ShowImage( "bitmap3.bmp", 0, 0, false); m_MyListCtrl.AddImgInfo("bitmap3.bmp", 0, 0, false); m_MyListCtrl.ShowImage( "bitmap3.bmp", 1, 0, true); m_MyListCtrl.AddImgInfo("bitmap3.bmp", 1, 0, true); m_MyListCtrl.ShowImage( "bitmap3.bmp", 2, 0, false); m_MyListCtrl.AddImgInfo("bitmap3.bmp", 2, 0, false); m_MyListCtrl.ShowImage( "bitmap3.bmp", 0, 1, true); m_MyListCtrl.AddImgInfo("bitmap3.bmp", 0, 1, true); m_MyListCtrl.ShowImage( "bitmap3.bmp", 1, 1, true); m_MyListCtrl.AddImgInfo("bitmap3.bmp", 1, 1, true); m_MyListCtrl.ShowImage( "bitmap3.bmp", 2, 1, true); m_MyListCtrl.AddImgInfo("bitmap3.bmp", 2, 1, true); m_MyListCtrl.ShowImage( "bitmap3.bmp", 0, 2, false); m_MyListCtrl.AddImgInfo("bitmap3.bmp", 0, 2, false); m_MyListCtrl.ShowImage( "bitmap3.bmp", 1, 2, false); m_MyListCtrl.AddImgInfo("bitmap3.bmp", 1, 2, false); m_MyListCtrl.ShowImage( "bitmap3.bmp", 2, 2, false); m_MyListCtrl.AddImgInfo("bitmap3.bmp", 2, 2, false); m_MyListCtrl.ShowImage( "bitmap3.bmp", 3, 0, true); m_MyListCtrl.AddImgInfo("bitmap3.bmp", 3, 0, true); m_MyListCtrl.ShowImage( "bitmap3.bmp", 3, 1, false); m_MyListCtrl.AddImgInfo("bitmap3.bmp", 3, 1, false); } void CCListCtrlTestDlg::OnBkcolor() { // TODO: Add your control notification handler code here CColorDialog dlg; if(dlg.DoModal() == IDOK) { m_MyListCtrl.SetBkColorEx(dlg.m_cc.rgbResult); } } void CCListCtrlTestDlg::OnTextcolor() { // TODO: Add your control notification handler code here CColorDialog dlg; if(dlg.DoModal() == IDOK) { m_MyListCtrl.SetTextColorEx(dlg.m_cc.rgbResult); } } void CCListCtrlTestDlg::OnGridpro() { // TODO: Add your control notification handler code here if ( IDOK == this->MessageBox("是否显示网格线!", "提示", MB_OKCANCEL ) ) m_MyListCtrl.SetGridPro(true); //true 显示网格 else m_MyListCtrl.SetGridPro(false); } void CCListCtrlTestDlg::OnGridcolor() { // TODO: Add your control notification handler code here CColorDialog dlg; if(dlg.DoModal() == IDOK) { m_MyListCtrl.SetGridColorEx(dlg.m_cc.rgbResult); } }