Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторная работа 11 / Polibiy / StaticTable
.cpp// StaticTable.cpp : implementation file
//
#include "stdafx.h"
#include "StaticTable.h"
// StaticTable
IMPLEMENT_DYNAMIC(StaticTable, CStatic)
StaticTable::StaticTable()
{
col = 1;
row = 1;
colWidth = 1;
rowHeight = 1;
data.resize(col*row);
}
StaticTable::~StaticTable()
{
}
void StaticTable::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
DrawHeaders(lpDrawItemStruct->hDC);
DrawContents(lpDrawItemStruct->hDC);
}
void StaticTable::DrawHeaders(HDC hDC)
{
wchar_t str[4] = {0};
int strLength = 0;
RECT place;
SIZE strSize;
GetClientRect(&place);
Rectangle(hDC, place.left, place.top, place.right, place.bottom);
HPEN pen = CreatePen(PS_SOLID, 1, RGB(128, 128, 128));
HFONT font = CreateFont(-15,0,0,0,FW_DONTCARE,false,false,false,DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH,
NULL);
SetBkMode(hDC, TRANSPARENT);
HPEN tempPen = (HPEN)SelectObject(hDC, pen);
HFONT tempFont = (HFONT)SelectObject(hDC, font);
COLORREF textColorTemp = SetTextColor(hDC, RGB(128,128,128));
for (int i=1; i<row; i++)
{
MoveToEx(hDC, place.left, place.top+i*rowHeight, NULL);
LineTo(hDC, place.right, place.top+i*rowHeight);
strLength = swprintf(str, 4, _T("%d"), i);
GetTextExtentPoint32(hDC, str, strLength, &strSize);
TextOut(hDC, place.left+(colWidth-strSize.cx)/2,
place.top+i*rowHeight+(rowHeight-strSize.cy)/2, str, strLength);
}
for (int i=1; i<col; i++)
{
MoveToEx(hDC, place.left+i*colWidth, place.top, NULL);
LineTo(hDC, place.left+i*colWidth, place.bottom);
strLength = swprintf(str, 4, _T("%d"), i);
GetTextExtentPoint32(hDC, str, strLength, &strSize);
TextOut(hDC, place.left+i*colWidth+(colWidth-strSize.cx)/2,
place.top+(rowHeight-strSize.cy)/2, str, strLength);
}
SetTextColor(hDC, textColorTemp);
SelectObject(hDC, tempPen);
SelectObject(hDC, tempFont);
DeleteObject(pen);
DeleteObject(font);
}
void StaticTable::DrawContents(HDC hDC)
{
SIZE strSize;
RECT place;
GetClientRect(&place);
HFONT font = CreateFont(-16,0,0,0,FW_BOLD,false,false,false,DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH,
NULL);
SetBkMode(hDC, TRANSPARENT);
HFONT tempFont = (HFONT)SelectObject(hDC, font);
for (int i=1; i<row; i++)
for (int j=1; j<col; j++)
{
GetTextExtentPoint32(hDC, data.at(i*col+j).GetBuffer(),
data.at(i*col+j).GetLength(), &strSize);
TextOut(hDC, place.left+j*colWidth+(colWidth-strSize.cx)/2,
place.top+i*rowHeight+(rowHeight-strSize.cy)/2,
data.at(i*col+j).GetBuffer(), data.at(i*col+j).GetLength());
}
SelectObject(hDC, tempFont);
DeleteObject(font);
}
int StaticTable::SetColNumber(const int Columns)
{
if (Columns > 1)
{
col = Columns+1;
data.clear();
data.resize(row*col);
return col*colWidth;
}
else return 0;
}
int StaticTable::SetRowNumber(const int Rows)
{
if (Rows > 1)
{
row = Rows+1;
data.clear();
data.resize(row*col);
return row*rowHeight;
}
else return 0;
}
int StaticTable::SetColWidth(const int Width)
{
if (Width > 0)
{
colWidth = Width;
return col*colWidth;
}
else return 0;
}
int StaticTable::SetRowHeight(const int Height)
{
if (Height > 0)
{
rowHeight = Height;
return row*rowHeight;
}
else return 0;
}
//1<=Row<=GetRowNumber()
//1<=Col<=GetColNumber()
int StaticTable::SetItemText(const int Row, const int Column, const CString &Text)
{
if (Row >= 1 && Row <= row && Column >= 1 && Column <= col)
{
data.at(Row*col+Column) = Text;
return 1;
}
else return 0;
}
BEGIN_MESSAGE_MAP(StaticTable, CStatic)
END_MESSAGE_MAP()
Соседние файлы в папке Polibiy