⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xtable.cpp

📁 一个简单方便的表格控件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	for (int i= 0; i < rows; i++) 
	{
		for(int j=0; j < rows; j++)
		{
			RECT rect = GetRect (i,j);
			if (rect.top <= point.y && rect.bottom > point.y && rect.left <= point.x && rect.right > point.x)
			{
				row = i;
				col = j;

				return true;
			}
		}
	}

	return false;
}

int XTable::Draw(CDC* pDC)
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			XCell& cell = cells[i*MAX_COLS+j];

			if (cell.colSpan !=0 && cell.rowSpan != 0)
			{
				RECT rect = GetRect(i,j);

				if (cell.GetOverlap())
				{
					RECT textRect = GetRect(i,j);

					cell.CalcTextRect(pDC, &textRect);

					if (textRect.right > rect.right)
					{
						for (j = j+1; j < cols; j ++)
						{
							if (cells[i*MAX_COLS+j].text != "")
								break;
							rect.right += colWidth [j];
							if (rect.right > textRect.right)
								break;
						}
						j --;
					}

				}

				cell.Draw(pDC,rect);
			}
		}
	}

	RECT rect = GetRect (focusRow, focusCol);
	GetCells (focusRow, focusCol)->DrawHitBorder(pDC, rect, RGB(255, 0, 20));

	return 0;
}


BEGIN_MESSAGE_MAP(XTable, CWnd)
	ON_WM_PAINT()
	ON_WM_KEYDOWN()
	ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()



// XTable message handlers
void XTable::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	CDC* pDC = &dc;

	Draw(pDC);
}

void XTable::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	CPoint point;

	RECT rect = GetRect(focusRow,focusCol);
	point.x = rect.left;
	point.y = rect.top;

	switch (nChar)
	{
		case VK_DOWN:
			point.y =rect.bottom + 1;
			break;

		case VK_UP:
			point.y =point.y -2;
			break;

		case VK_LEFT:
			point.x = point.x - 2;
			break;

		case VK_RIGHT:
			point.x =rect.right + 1;
			break;

		default:
			return ;
	}

	HitTest (point,focusRow,focusCol);
	Invalidate();
	
	CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}

void XTable::OnLButtonDown(UINT nFlags, CPoint point)
{
	HitTest (point,focusRow,focusCol);
	
	SetFocus ();
	Invalidate();

	CWnd::OnLButtonDown(nFlags, point);
}


BOOL XTable::RegisterWindowClass()
{
    WNDCLASS wndcls;
    HINSTANCE hInst = AfxGetInstanceHandle();

    if (!(::GetClassInfo(hInst, XTABLE_CLASSNAME, &wndcls)))
    {
        // otherwise we need to register a new class
        wndcls.style            = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
        wndcls.lpfnWndProc      = ::DefWindowProc;
        wndcls.cbClsExtra       = wndcls.cbWndExtra = 0;
        wndcls.hInstance        = hInst;
        wndcls.hIcon            = NULL;
        wndcls.hCursor          = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
        wndcls.hbrBackground    = (HBRUSH) (COLOR_3DFACE + 1);
        wndcls.lpszMenuName     = NULL;
        wndcls.lpszClassName    = XTABLE_CLASSNAME;

        if (!AfxRegisterClass(&wndcls))
        {
            AfxThrowResourceException();
            return FALSE;
        }
    }

    return TRUE;
}

int XTable::Test()
{
	defaultHeight = 20;
	defaultwidth = 80;
	SetRows (46);
	SetCols (9);

	for (int i = 0; i < 46; i ++)
		SetBackColor (i, 0, RGB(0xC8, 0x96, 0x96));

	SetRowHeight (0, 10);
	SetRowHeight (2, 10);
	SetRowHeight (7, 10);
	SetRowHeight (9, 10);
	SetRowHeight (13, 10);
	SetRowHeight (15, 10);
	SetRowHeight (19, 10);
	SetRowHeight (21, 10);
	SetRowHeight (24, 10);
	SetRowHeight (28, 10);
	SetRowHeight (31, 10);
	SetRowHeight (34, 10);

	for (i = 0; i < 9; i ++)
		SetBackColor (0, i, RGB(0xC8, 0x96, 0x96));
	
	SetColWidth (0, 10);

	JoinCells (1,1,1,8);
	SetBackColor (1,1, RGB(0x64,0x96,0xC8));
	SetText (1,1, "Default edit control respects cell's celltype, font size and type");

	JoinCells (3,1,3,2);
	SetText (3,1, "Using default font");

	SetText (3,3, "Enter text");

	JoinCells (3,5,5,6);
	SetText (3,5, "This cell contains multiline text, which provides multiline edit.");
	SetWordbreak (3,5,true);

	JoinCells (3,7,5,8);
	SetText (3,7, "This cell contains multiline text, which provides multiline edit.");
	SetWordbreak (3,7,true);

	SetText (4,1, "You can even edit a long (cell overlapping) string.");
	SetOverlap(4,1,true);

	SetText (4,3, "Test");

	JoinCells (5,1,6,2);
	SetText (5,1, "Enter your name here.");

	JoinCells (5,3,5,4);
	SetText (5,3, "Enter your text here ...");

	JoinCells (8,1,8,8);
	SetBackColor (8,1, RGB(0x64,0x96,0xC8));
	SetText (8,1, "Default edit controls also allow us to customize user's editing experience by:");

	JoinCells (10,1,10,2);
	SetBackColor (10,1, RGB(0xDC,0xC8,0xB4));
	SetText (10,1, "Converting input to upper case:");

	SetText (10,3, "TYPE HERE ...");

	JoinCells (10,5,10,6);
	SetBackColor (10,5, RGB(0xDC,0xC8,0xB4));
	SetText (10,5, "Converting input to lower case:");

	SetText (10,7, "type here ...");

	JoinCells (11,1,11,2);
	SetBackColor (11,1, RGB(0xDC,0xC8,0xB4));
	SetText (11,1, "Allowing password input:");

	SetText (11,3, "*********");

	SetText (11,5, "Entered password was:*********");
	SetOverlap(11,5,true);

	JoinCells (12,1,12,2);
	SetBackColor (12,1, RGB(0xDC,0xC8,0xB4));
	SetText (12,1, "Allowing aligned input:");

	SetText (12,3, "Left aligned");
	SetText (12,5, "Centered");
	SetAlignment (12,5,DT_CENTER);
	SetText (12,7, "Right aligned");
	SetAlignment (12,7,DT_RIGHT);

	JoinCells (14,1,14,8);
	SetBackColor (14,1, RGB(0x64,0x96,0xC8));
	SetText (14,1, "Ultimate Grid allow us to process user's input with various notifications.");

	JoinCells (16,1,16,5);
	SetBackColor (16,1, RGB(0xDC,0xC8,0xB4));
	SetText (16,1, "We can verify data it is entered, for example to limit number of char to 4:");

	SetText (16,6, "<Type here>");

	JoinCells (17,1,17,5);
	SetBackColor (17,1, RGB(0xDC,0xC8,0xB4));
	SetText (17,1, "or that the numeric value entered is under 500 (number < 500):");

	SetText (17,6, "<Type here>");

	JoinCells (18,1,18,5);
	SetBackColor (18,1, RGB(0xDC,0xC8,0xB4));
	SetText (18,1, "We can also test user's input after it is completed, or force user's input:");

	SetText (18,6, "<Type here>");

	JoinCells (20,1,20,8);
	SetBackColor (20,1, RGB(0x64,0x96,0xC8));
	SetText (20,1, "The Ultimate Grid also allows us to use other edit controls when needed:");

	JoinCells (22,1,23,5);
	SetBackColor (22,1, RGB(0xDC,0xC8,0xB4));
	SetText (22,1, "Just by setting a mask on a cell, the grid will change edit controls to use a mask edit. By default Ultimate Grid creates an instance of CUGMEdit to use as mask edit control.");
	SetWordbreak (22,1,true);

	JoinCells (22,6,22,7);
	SetText (22,6, "<Select mask>");
	JoinCells (23,6,23,7);
	SetText (23,6, "<Type here>");

	JoinCells (25,1,27,5);
	SetBackColor (25,1, RGB(0xDC,0xC8,0xB4));
	SetText (25,1, "The Ultimate Grid 5 also comes with a set of advanced edit control previosly only found in Ultimate ToolBox. We can now easily incorporate powerful controls like COXMaskEdit, COXNumericEdit, and COXCurrencyEdit into our applications.");
	SetWordbreak (25,1,true);

	JoinCells (29,1,30,5);
	SetBackColor (29,1, RGB(0xDC,0xC8,0xB4));
	SetText (29,1, "The COXMaskEdit works very similarly to the default mask edit control, to use it just set a mask on a cell and inform the grid that you want to use different edit control.");
	SetWordbreak (29,1,true);

	JoinCells (29,6,29,8);
	SetText (29,6, "<Select mask>");
	JoinCells (30,6,30,8);
	SetText (30,6, "<Type here>");

	JoinCells (32,1,33,5);
	SetBackColor (32,1, RGB(0xDC,0xC8,0xB4));
	SetText (32,1, "The COXNumericEdit and COXCurrencyEdit on the other hand allow us to provide additional setup information that will drive the way the edit control will behave.");
	SetWordbreak (32,1,true);

	JoinCells (35,1,35,2);
	SetBackColor (35,1, RGB(0xDC,0xC8,0xB4));
	SetText (35,1, "Using COXNumericEdit:");

	JoinCells (35,3,35,4);
	SetText (35,3, "<Type here>");

	JoinCells (35,5,35,6);
	SetBackColor (35,5, RGB(0xDC,0xC8,0xB4));
	SetText (35,5, "Using COXCurrencyEdit:");

	JoinCells (35,7,35,8);
	SetText (35,7, "<Type here>");

	JoinCells (36,1,36,3);
	SetBackColor (36,1, RGB(0x64,0x96,0xC8));
	SetText (36,1, "Following are properties we can set.");

	JoinCells (36,5,36,7);
	SetBackColor (36,5, RGB(0x64,0x96,0xC8));
	SetText (36,5, "Following are properties we can set.");

	JoinCells (37,1,37,2);
	SetBackColor (37,1, RGB(0xDC,0xC8,0xB4));
	SetText (37,1, "Decimal digit count :");

	JoinCells (37,5,37,6);
	SetBackColor (37,5, RGB(0xDC,0xC8,0xB4));
	SetText (37,5, "Decimal digit count :");

	JoinCells (38,1,38,2);
	SetBackColor (38,1, RGB(0xDC,0xC8,0xB4));
	SetText (38,1, "Decimal Separator :");

	JoinCells (38,5,38,6);
	SetBackColor (38,5, RGB(0xDC,0xC8,0xB4));
	SetText (38,5, "Decimal Separator :");

	JoinCells (39,1,39,2);
	SetBackColor (39,1, RGB(0xDC,0xC8,0xB4));
	SetText (39,1, "Fractional digit count :");

	JoinCells (39,5,39,6);
	SetBackColor (39,5, RGB(0xDC,0xC8,0xB4));
	SetText (39,5, "Fractional digit count :");

	JoinCells (40,1,40,2);
	SetBackColor (40,1, RGB(0xDC,0xC8,0xB4));
	SetText (40,1, "Group Separator :");

	JoinCells (40,5,40,6);
	SetBackColor (40,5, RGB(0xDC,0xC8,0xB4));
	SetText (40,5, "Group Separator :");

	JoinCells (41,1,41,2);
	SetBackColor (41,1, RGB(0xDC,0xC8,0xB4));
	SetText (41,1, "Digits in group :");

	JoinCells (41,5,41,6);
	SetBackColor (41,5, RGB(0xDC,0xC8,0xB4));
	SetText (41,5, "Digits in group :");

	JoinCells (42,1,42,2);
	SetBackColor (42,1, RGB(0xDC,0xC8,0xB4));
	SetText (42,1, "Negative Format :");

	JoinCells (42,5,42,6);
	SetBackColor (42,5, RGB(0xDC,0xC8,0xB4));
	SetText (42,5, "Positive Format :");

	JoinCells (43,1,43,2);
	SetBackColor (43,1, RGB(0xDC,0xC8,0xB4));
	SetText (43,1, "Show leading zero :");

	JoinCells (43,5,43,6);
	SetBackColor (43,5, RGB(0xDC,0xC8,0xB4));
	SetText (43,5, "Negative Format :");

	JoinCells (44,5,44,6);
	SetBackColor (44,5, RGB(0xDC,0xC8,0xB4));
	SetText (44,5, "Currency name :");

	JoinCells (45,5,45,6);
	SetBackColor (45,5, RGB(0xDC,0xC8,0xB4));
	SetText (45,5, "Show leading zero :");

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -