MS FlexGrid(MSFlexGrid)
.SetCols()
.SetTextMatrix()
.SetRow()
.SetCol()
.SetCellBackColor()
.SetCellForeColor()
#include "EditFlexGrid.h"
class CEditableGridDlg : public CDialog
{
// Construction
public:
CEditableGridDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CEditableGridDlg)
enum { IDD = IDD_EDITABLEGRID_DIALOG };
CEditFlexGrid m_grid;
//}}AFX_DATA
...
};
void CEditableGridDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEditableGridDlg)
DDX_Control(pDX, IDC_MSFLEXGRID1, m_grid);
//}}AFX_DATA_MAP
}
...
BOOL CEditableGridDlg::OnInitDialog()
{
...
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
// {
CString s;
m_grid.SetRows(20);
m_grid.SetCols(11);
for (int a=0;a<20;a++) {
for (int b=0;b<11;b++) {
s.Format("Row %d, Col %d",a,b);
m_grid.SetTextMatrix(a,b,s);
}
}
m_grid.SetRow(1);
for (a=1;a<11;a++) {
static long
color[]={0x00ff0000,0x0000ff00,0x000000ff,0x00cc00ff,0x0000ccff,0x00ffffcc,0x002000aa,0x00ffccff,0x0020aab0,0x00b53589};
m_grid.SetCol(a);
m_grid.SetCellBackColor(color[a-1]);
m_grid.SetCellForeColor(color[10-a]);
}
// }
return TRUE; // return TRUE unless you set the focus to a control
}
class CMSFlexGrid : public CWnd
{
...
// {
void SetText(long nNewValue);
// }
void SetText(LPCTSTR lpszNewValue);
...
// {
void AddItem(long Item);
void AddItem(LPCTSTR Item);
// }
void AddItem(LPCTSTR Item, const VARIANT& index);
...
};
void CMSFlexGrid::SetText(long nNewValue)
{
static BYTE parms[] =
VTS_I4;
InvokeHelper(0x0, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms, nNewValue);
}
void CMSFlexGrid::SetText(LPCTSTR lpszNewValue)
{
static BYTE parms[] =
VTS_BSTR;
InvokeHelper(0x0, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms,
lpszNewValue);
}
...
void CMSFlexGrid::AddItem(long Item)
{
static BYTE parms[] =
VTS_I4;
InvokeHelper(0x42, DISPATCH_METHOD, VT_EMPTY, NULL, parms, Item);
}
void CMSFlexGrid::AddItem(LPCTSTR Item)
{
static BYTE parms[] =
VTS_BSTR;
InvokeHelper(0x42, DISPATCH_METHOD, VT_EMPTY, NULL, parms, Item);
}
void CMSFlexGrid::AddItem(LPCTSTR Item, const VARIANT& index)
{
static BYTE parms[] =
VTS_BSTR VTS_VARIANT;
InvokeHelper(0x42, DISPATCH_METHOD, VT_EMPTY, NULL, parms,
Item, &index);
}
class CGridExDlg : public CDialog
{
// Construction
public:
CGridExDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CGridExDlg)
enum { IDD = IDD_GRIDEX_DIALOG };
CMSFlexGrid m_FlexGrid;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CGridExDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
...
// Generated message map functions
//{{AFX_MSG(CGridExDlg)
...
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnCellforecolor();
afx_msg void OnCellbackcolor();
afx_msg void OnText();
afx_msg void OnFloat();
afx_msg void OnMergecol();
afx_msg void OnMergerow();
afx_msg void OnBitmap();
afx_msg void OnIcon1();
afx_msg void OnClose();
afx_msg void OnForecolor();
afx_msg void OnBackcolor();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
BOOL InitializeGrid();
COLORREF m_crFore;
COLORREF m_crBack;
};
CGridExDlg::CGridExDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGridExDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGridExDlg)
// 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);
// {
m_crFore = RGB(0,0,0);
m_crBack = RGB(255,255,255);
// }
}
void CGridExDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGridExDlg)
DDX_Control(pDX, IDC_MSFLEXGRID, m_FlexGrid);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGridExDlg, CDialog)
//{{AFX_MSG_MAP(CGridExDlg)
...
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CELLFORECOLOR, OnCellforecolor)
ON_BN_CLICKED(IDC_CELLBACKCOLOR, OnCellbackcolor)
ON_BN_CLICKED(IDC_TEXT, OnText)
ON_BN_CLICKED(IDC_FLOAT, OnFloat)
ON_BN_CLICKED(IDC_MERGECOL, OnMergecol)
ON_BN_CLICKED(IDC_MERGEROW, OnMergerow)
ON_BN_CLICKED(IDC_BITMAP, OnBitmap)
ON_BN_CLICKED(IDC_ICON1, OnIcon1)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_FORECOLOR, OnForecolor)
ON_BN_CLICKED(IDC_BACKCOLOR, OnBackcolor)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
...
BOOL CGridExDlg::OnInitDialog()
{
...
// 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
// {
InitializeGrid();
// }
return TRUE; // return TRUE unless you set the focus to a control
}
...
BOOL CGridExDlg::InitializeGrid()
{
m_FlexGrid.SetCols(8);
m_FlexGrid.SetRows(1);
m_FlexGrid.SetAllowUserResizing(2);
m_FlexGrid.SetFormatString("^A|^B|^C|^D|^E|^F|^G|^H");
m_FlexGrid.SetColWidth(-1,1000);
for(int i = 1; i <= 10; i++)
m_FlexGrid.AddItem(i);
return TRUE;
}
void CGridExDlg::OnCellforecolor()
{
// TODO: Add your control notification handler code here
UINT rt = IsDlgButtonChecked( IDC_CELLFORECOLOR );
if (rt == 0)
{
m_crFore = RGB(0,0,0);
}
else
{
m_crFore = RGB(0,255,0);;
}
}
void CGridExDlg::OnCellbackcolor()
{
// TODO: Add your control notification handler code here
UINT rt = IsDlgButtonChecked( IDC_CELLBACKCOLOR );
if (rt == 0)
{
m_crBack = RGB(255,255,255);
}
else
{
m_crBack = RGB(255,0,0);
}
}
void CGridExDlg::OnText()
{
// TODO: Add your control notification handler code here
m_FlexGrid.SetCellForeColor(m_crFore);
m_FlexGrid.SetCellBackColor(m_crBack);
m_FlexGrid.SetText("텍스트");
}
void CGridExDlg::OnFloat()
{
// TODO: Add your control notification handler code here
m_FlexGrid.SetCellForeColor(m_crFore);
m_FlexGrid.SetCellBackColor(m_crBack);
m_FlexGrid.SetText(200);
}
void CGridExDlg::OnMergecol()
{
// TODO: Add your control notification handler code here
m_FlexGrid.SetTextMatrix(1,1,"텍스트");
m_FlexGrid.SetTextMatrix(1,2,"텍스트");
m_FlexGrid.SetTextMatrix(1,3,"텍스트");
m_FlexGrid.SetMergeCells(2);
m_FlexGrid.SetMergeRow(1,TRUE);
}
void CGridExDlg::OnMergerow()
{
// TODO: Add your control notification handler code here
m_FlexGrid.SetTextMatrix(1,3,"컬럼 머지");
m_FlexGrid.SetTextMatrix(2,3,"컬럼 머지");
m_FlexGrid.SetTextMatrix(3,3,"컬럼 머지");
m_FlexGrid.SetMergeCells(3);
m_FlexGrid.SetMergeCol(3,TRUE);
}
void CGridExDlg::OnBitmap()
{
// TODO: Add your control notification handler code here
IDispatch* pDisp = 0;
PICTDESC pictDesc;
memset(&pictDesc, 0, sizeof(pictDesc));
pictDesc.cbSizeofstruct = sizeof(pictDesc);
pictDesc.bmp.hpal = 0;
UpdateData(TRUE);
HBITMAP hBmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BITMAP),
IMAGE_BITMAP,
0,0,
LR_LOADMAP3DCOLORS);
pictDesc.bmp.hbitmap = hBmp;
pictDesc.picType = PICTYPE_BITMAP;
HRESULT hr;
hr = OleCreatePictureIndirect(&pictDesc,
IID_IDispatch,
FALSE,
(void**)&pDisp);
if(SUCCEEDED(hr))
{
m_FlexGrid.SetRefCellPicture(pDisp);
pDisp->Release();
}
}
void CGridExDlg::OnIcon1()
{
// TODO: Add your control notification handler code here
IDispatch* pDisp = 0;
PICTDESC pictDesc;
memset(&pictDesc, 0, sizeof(pictDesc));
pictDesc.cbSizeofstruct = sizeof(pictDesc);
HICON hIcon;
hIcon = ::LoadIcon(AfxGetApp()->m_hInstance,
MAKEINTRESOURCE(IDI_ICON));
if(hIcon)
{
pictDesc.icon.hicon = hIcon;
pictDesc.picType = PICTYPE_ICON;
HRESULT hr;
hr = OleCreatePictureIndirect(&pictDesc,
IID_IDispatch,
FALSE,
(void**)&pDisp);
if(SUCCEEDED(hr))
{
m_FlexGrid.SetRefCellPicture(pDisp);
pDisp->Release();
}
}
}
void CGridExDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
EndDialog(IDOK);
CDialog::OnClose();
}
void CGridExDlg::OnForecolor()
{
// TODO: Add your control notification handler code here
m_FlexGrid.SetForeColor(RGB(255,128,255));
}
void CGridExDlg::OnBackcolor()
{
// TODO: Add your control notification handler code here
m_FlexGrid.SetBackColor(RGB(128,128,255));
}
참조 사이트:
http://support.microsoft.com/kb/196833/en-us/
http://www.codeguru.com/cpp/controls/controls/gridcontrol/print.php/c5291