본문 바로가기
SoftWare_API_MFC

MFC에서 그리드 컨트롤 넣는 방법

by 건실한청년 2022. 3. 8.

1. Code Project에서 파일 복사(http://www.codeproject.com/Articles/8/MFC-Grid-control-2-27)

gridctrl_demo.zip
0.35MB

 

2. 이 상태에서 컴파일을 하면 오류가 발생하므로 MemDC.h 파일 내의 CMemDC 클래스의 이름을 다른 이름으로 변경

 

3. 커스텀 컨트롤 추가(ID를 IDC_GRID라고 해 본다)

 

4. 속성 -> Class 에 "MFCGridCtrl"이라고 넣음

 

5. 헤더파일에 #include "GridCtrl.h" 추가

 

6. 변버변수 추가 CGridCtrl m_Grid;

 

7. DoDataExchange에서 아래 내용 추가

   DDX_GridControl(pDX, IDC_GRID, m_Grid); // 추가

 

8. 초기화 한 다음 사용

 

9. 아래는 대충 샘플 코드

 

void CTab1Dlg::DrawGrid(CGridCtrl &tmp_grid)

{

tmp_grid.SetEditable(FALSE);

tmp_grid.SetListMode(TRUE);

tmp_grid.EnableDragAndDrop(FALSE);

tmp_grid.SetTextBkColor(RGB(0xFF, 0xFF, 0xE0));

 

tmp_grid.SetRowCount(13);            //5행

tmp_grid.SetColumnCount(2);        //4열

tmp_grid.SetFixedRowCount(1);        //1고정 행

//tmp_grid.SetFixedColumnCount(1);    //1고정 열

 

//int aa = tmp_grid.GetColumnWidth(0);

//int bb = tmp_grid.GetColumnWidth(1);

tmp_grid.SetColumnWidth(0,120);

tmp_grid.SetColumnWidth(1,70);

 

DWORD dwTextStyle = DT_CENTER|DT_VCENTER|DT_SINGLELINE;    //Text 스타일 정의

for (int row = 0; row < tmp_grid.GetRowCount(); row++) 

{

for (int col = 0; col < tmp_grid.GetColumnCount(); col++) 

GV_ITEM Item;

Item.mask = GVIF_TEXT|GVIF_FORMAT;

Item.row = row;

Item.col = col;

 

//셀의 텍스트 설정

if (row == 0 && col==0) 

{

Item.nFormat = DT_CENTER|DT_WORDBREAK;

Item.strText.Format(_T("시스템 명칭"),col);

 

else if (row == 0 && col==1) 

{

Item.nFormat = DT_CENTER|DT_WORDBREAK;

Item.strText.Format(_T("현재 값"),col);

 

else if (col < 1) 

{

Item.nFormat = dwTextStyle;

Item.strText.Format(_T("시스템 %d"),row);

 

else 

{

Item.nFormat = dwTextStyle;

Item.strText.Format(_T("%d"),row*col);

}

tmp_grid.SetItem(&Item);  

 

//셀의 색상을 랜덤으로

/*

if (rand() % 10 == 1) {

COLORREF clr = RGB(rand() % 128+128, 

rand() % 128+128, 

rand() % 128+128);

tmp_grid.SetItemBkColour(row, col, clr);

tmp_grid.SetItemFgColour(row, col, RGB(255,0,0));

}

*/

}

}

//1행 1열은 read-only로 설정

//tmp_grid.SetItemState(1,1, tmp_grid.GetItemState(1,1) | GVIS_READONLY);

 

//셀 너비는 자동으로 설정

//tmp_grid.AutoSize();

tmp_grid.SetRowHeight(0, 3*tmp_grid.GetRowHeight(0)/2);

}



출처: https://eachan.tistory.com/36?category=315753 [EACHAN's Blog]