• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            asm, c, c++ are my all
            -- Core In Computer
            posts - 139,  comments - 123,  trackbacks - 0
            [轉]GridCtrl使用詳解

            在單文檔中的使用方法
            步驟一 初始化


            在CView類的.h頭文件中包含文件:
            ??? #include "Gridctrl.h"

            并且手寫加入如下的成員函數:
            ????CGridCtrl * m_pGridCtrl;

            步驟二 構造與析構

            構造函數中:
            ???m_pGridCtrl = NULL;
            析構函數中:
            ???if(m_pGridCtrl)
            ???????delete m_pGridCtrl;


            步驟三 如果需要打印功能的話添加同名打印函數代碼

            在CView類的OnBeginPrinting()函數中添加如下代碼:
            if(m_pGridCtrl)
            ????m_pGridCtrl->OnBeginPrinting(pDC,pInfo);
            //簡單吧,這就是類的好處

            其它兩個打印函數也一樣的做法.

            步驟四 在OnInitaUpdate()函數中或者你自己添加的要顯示Grid的消息函數中如下初始化:

            				   //創建非模式對話框
            CDlg *dlg;
            dlg=new CDlg();
            dlg->Create(IDD_Dlg,this);

            //初始化GridCtrl控件
            if(m_pGridCtrl!=NULL)
            {
            delete m_pGridCtrl;
            m_pGridCtrl=NULL;
            }
            if (m_pGridCtrl == NULL)
            {
            // Create the Gridctrl object
            m_pGridCtrl = new CGridCtrl;
            if (!m_pGridCtrl) return 0;
            // Create the Gridctrl window
            CRect rect;
            GetClientRect(rect);
            m_pGridCtrl->Create(rect, this, 100);
            // fill it up with stuff
            m_pGridCtrl->SetEditable(false);
            m_pGridCtrl->SetTextBkColor(RGB(0xFF, 0xFF, 0xE0)); //黃色背景
            m_pGridCtrl->EnableDragAndDrop(false);
            try {
            m_pGridCtrl->SetRowCount(k); //設置行數為k行
            m_pGridCtrl->SetColumnCount(4); //k列
            m_pGridCtrl->SetFixedRowCount(1); //標題行為一行
            m_pGridCtrl->SetFixedColumnCount(1); //同上
            }
            catch (CMemoryException* e)
            {
            e->ReportError();
            e->Delete();
            return 0;
            }
            //填充列標題
            int row=0;
            for(int col=0;col<4;col++)
            {
            GV_ITEM Item;
            Item.mask = GVIF_TEXT|GVIF_FORMAT;
            Item.row = row;
            Item.col = col;
            if(col==0){
            Item.nFormat = DT_CENTER|DT_WORDBREAK;
            Item.strText.Format(_T("【類別】"),col);
            }
            else if(col==1){
            Item.nFormat = DT_LEFT|DT_WORDBREAK;
            Item.strText.Format(_T("第一列"),col);
            }
            else if(col==2){
            Item.nFormat = DT_LEFT|DT_WORDBREAK;
            Item.strText.Format(_T("第二列"),col);
            }
            m_pGridCtrl->SetItem(&Item);
            }
            // fill rows/cols with text
            for (row = 1; row < k; row++)
            for (col = 0; col < h; col++)
            {
            GV_ITEM Item;
            Item.mask = GVIF_TEXT|GVIF_FORMAT;
            Item.row = row;
            Item.col = col;
            if (col < 1) { //行標題頭
            Item.nFormat = DT_CENTER|DT_VCENTER
            |DT_SINGLELINE|DT_END_ELLIPSIS
            |DT_NOPREFIX;
            Item.strText.Format(_T("%d"),row);
            }
            else if(col==1){ //第一列的值
            Item.nFormat = DT_CENTER|DT_VCENTER
            |DT_SINGLELINE|DT_END_ELLIPSIS
            |DT_NOPREFIX;
            str="aa";
            Item.strText.Format(_T("%s"),str);
            }else if(col==2){ //第二列第值
            Item.nFormat = DT_CENTER|DT_VCENTER
            |DT_SINGLELINE|DT_END_ELLIPSIS
            |DT_NOPREFIX;
            CString str;
            str="bb";
            Item.strText.Format(_T("%s"),str);
            }
            m_pGridCtrl->SetItem(&Item);
            }
            m_pGridCtrl->AutoSize();

            //--------------設置行列距------------------
            for(int a=1;a<m;a++)
            m_pGridCtrl->SetRowHeight(a,21); //設置各行高
            m_pGridCtrl->SetRowHeight(0,24); //設置0行高
            m_pGridCtrl->SetColumnWidth(1,110); //設置2列寬
            m_pGridCtrl->SetColumnWidth(2,160); //設置3列寬
            m_pGridCtrl->SetColumnWidth(3,100); //設置4列寬
            }
            上例取自實際工程,稍有修改!
            部分注釋:
            void SetVirtualMode(TRUE)
             //設為虛模式
            BOOL SetRowCount(int nRows) //設置總的行數。
            BOOL SetFixedRowCount(int nFixedRows = 1)//設置固定的行數據
            BOOL SetColumnCount(int nCols) //設置列數
            BOOL SetFixedColumnCount(int nFixedCols = 1)//設置固定的列數


            步驟五: 添加WM_SIZE消息,調整控件的界面占屏幕大小

            ??if(m_pGridCtrl->GetSafeHWnd())
            ???{
            ??????CRect rect;
            ?????GetClientRect(rect);
            ?????m_pGridCtrl->MoveWindow(rect);
            ???}



            ?在對話框中的使用方法

            步驟一 創建數據顯示表格對話框

            在資源管理器中新創建一個對話框,假設為CDlgTestReportBox。 從工具箱中加入Custom Control,就是人頭像的那個,將其區域拉伸至要顯示數據表格的大小,充滿整個對話框。

            在CDlgTestReportBox類的頭文件中:

            #include "GridCtrl.h"

            再定義成員變量:

            CGridCtrl* m_pGrid;

            添加OnShowWindow()消息處理函數如下:

            				void CDlgTestReportBox::OnShowWindow(BOOL bShow, UINT nStatus) 
            {
            CDialog::OnShowWindow(bShow, nStatus);
            // TODO: Add your message handler code here
            if(m_pGrid!=NULL)
            {
            delete m_pGrid;
            m_pGrid=NULL;
            }
            if(m_pGrid==NULL)
            {
            m_pGrid=new CGridCtrl;
            CRect rect;
            GetDlgItem(IDC_ReportAera)->GetWindowRect(rect); //得到顯示區域
            ScreenToClient(&rect);
            m_pGrid->Create(rect,this,100);
            m_pGrid->SetEditable(false);
            m_pGrid->SetTextBkColor(RGB(0xFF, 0xFF, 0xE0)); //黃色背景
            try
            {
            m_pGrid->SetRowCount(10); //初始為10行
            m_pGrid->SetColumnCount(11); //初始化為11列
            m_pGrid->SetFixedRowCount(1); //表頭為一行
            m_pGrid->SetFixedColumnCount(1); //表頭為一列
            }
            catch (CMemoryException* e)
            {
            e->ReportError();
            e->Delete();
            // return FALSE;
            }
            for (int row = 0; row < m_pGrid->GetRowCount(); row++)
            for (int col = 0; col < m_pGrid->GetColumnCount(); col++)
            {
            //設置表格顯示屬性
            GV_ITEM Item;
            Item.mask = GVIF_TEXT|GVIF_FORMAT;
            Item.row = row;
            Item.col = col;
            if(row==0&&col==0) //第(0,0)格
            {
            Item.nFormat = DT_CENTER|DT_WORDBREAK;
            Item.szText.Format(_T("報表顯示"),col);
            }
            else if (row < 1) //設置0行表頭顯示
            {
            Item.nFormat = DT_CENTER|DT_WORDBREAK;
            Item.szText.Format(_T(" 項目%d"),col);
            }
            else if (col < 1) //設置0列表頭顯示
            {
            if(row< m_pGrid->GetRowCount()-4)
            {
            Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
            Item.szText.Format(_T("第%d次"),row);
            }
            }
            else
            {
            Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
            Item.szText.Format(_T(""),2);
            }
            m_pGrid->SetItem(&Item);
            }
            m_pGrid->Invalidate();
            }
            //--------------設置行列距------------------
            for(int a=0;aGetRowCount();a++)
            m_pGrid->SetRowHeight(a,16); //設置各行高
            m_pGrid->SetColumnWidth(0,58); //設置0列寬
            for(int b=1;bGetColumnCount();b++)
            m_pGrid->SetColumnWidth(b,59); //設置各列寬
            }

            步驟二 嵌入上面的對話框 顯示數據

            在你需要顯示數據的對話框上的頭文件中,假設為CDlgTest,加入

            #include "GridCtrl.h"

            CDlgTestReportBox* m_pTestReportBox;

            將數據顯示對話框放入你的對話框相應位置上,在CDlgTest::OnInitDialog() 中:

            if(!m_pTestReportBox)
            {
            ?????m_pTestReportBox=new CDlgTestReportBox(this);
            }

            m_pTestReportBox->Create(IDD_DlgTestReportBox,this);

            //定義區域變量
            CRect rectDraw;
            GetDlgItem(IDC_AeraReport)->GetWindowRect(rectDraw);
            ScreenToClient(&rectDraw); //動態測試數據顯示區域rectDraw

            //將對應的對話框放到指定區域
            m_pTestReportBox->MoveWindow(rectDraw);
            m_pTestReportBox->ShowWindow(SW_SHOW);

            自定義填充數據的函數:CDlgTest::FillGrid() 如下:

            				CGridCtrl* pGrid=m_pTestReportBox->m_pGrid;
            for (int row = pGrid->GetRowCount()-1; row >= pGrid->GetRowCount()-3; row--)
            {
            for (int col = 1; col <= pGrid->GetColumnCount(); col++)
            {
            GV_ITEM Item;
            Item.mask = GVIF_TEXT|GVIF_FORMAT;
            Item.row = row;
            Item.col = col;
            if(row==pGrid->GetRowCount()-3&&col>0) //平均值
            {
            if(col==10){
            Item.nFormat = DT_CENTER|DT_WORDBREAK;
            Item.szText.Format(_T(" %6.2f "),avjch);
            }
            else{
            Item.nFormat = DT_CENTER|DT_WORDBREAK;
            Item.szText.Format(_T(" %6.2f "),av[col-1]);
            }
            }
            pGrid->SetItem(&Item); //提交數據
            if(row==0||col==0)
            {
            COLORREF clr = RGB(0, 0, 0);
            pGrid->SetItemBkColour(row, col, clr);
            pGrid->SetItemFgColour(row, col, RGB(255,0,0));
            }
            }//循環結束
            pGrid->Invalidate();
            }
            好累啊,忙了一天時間終于寫完了!
            posted on 2006-06-24 19:50 Jerry Cat 閱讀(5417) 評論(5)  編輯 收藏 引用

            FeedBack:
            # re: [轉]GridCtrl使用詳解
            2007-05-08 16:03 | lusoja
            看了你的總結受益非淺。
            你講的很詳細。
            謝謝你的無私幫助!  回復  更多評論
              
            # re: [轉]GridCtrl使用詳解
            2007-10-17 14:55 | 天之驕子
            謝謝  回復  更多評論
              
            # re: [轉]GridCtrl使用詳解
            2008-01-22 22:59 |
            很有才啊  回復  更多評論
              
            # re: [轉]GridCtrl使用詳解
            2009-02-02 14:36 | tik
            寫的很詳細。可惜沒有下載源碼的地址。。  回復  更多評論
              
            # re: [轉]GridCtrl使用詳解
            2009-07-23 21:35 | 扯淡
            你寫的很不清楚,IDC_ReportAera是什么東西??  回復  更多評論
              

            <2006年11月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            常用鏈接

            留言簿(7)

            隨筆檔案

            最新隨筆

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            日韩一区二区久久久久久| 欧美精品一区二区久久| 无码人妻精品一区二区三区久久| 久久精品国产99国产精品导航| 色综合久久无码中文字幕| 香蕉久久一区二区不卡无毒影院| 久久久亚洲精品蜜桃臀| 亚洲精品乱码久久久久久 | 亚洲精品白浆高清久久久久久| 久久亚洲春色中文字幕久久久| 亚洲国产精品久久久久| 亚洲国产高清精品线久久| 狠狠久久亚洲欧美专区| 怡红院日本一道日本久久 | 欧美大战日韩91综合一区婷婷久久青草 | 99久久99这里只有免费的精品| 久久国产成人午夜AV影院| 久久av无码专区亚洲av桃花岛| 久久久久国产日韩精品网站| 国产成人无码久久久精品一| 亚洲国产成人久久一区WWW| 欧美亚洲国产精品久久蜜芽| 色综合久久久久无码专区 | 少妇内射兰兰久久| 97精品伊人久久大香线蕉| 久久精品国产一区二区三区 | 久久er国产精品免费观看8| 国内精品久久久久伊人av| 99久久免费国产精品特黄| 欧美成a人片免费看久久| 国产2021久久精品| 国产精品成人99久久久久91gav | 久久综合狠狠综合久久97色| 亚洲狠狠综合久久| 香港aa三级久久三级| 7国产欧美日韩综合天堂中文久久久久| 久久久久99精品成人片欧美| 久久久久久毛片免费播放| 久久99热国产这有精品| 93精91精品国产综合久久香蕉| 精品99久久aaa一级毛片|