Kardano:
KardanoSquare.cpp
#include "StdAfx.h"
#include "KardanoSquare.h"
#include <stdlib.h>
KardanoSquare::KardanoSquare(void)
{
size = 4;
squareData = new char[size*size];
}
KardanoSquare::~KardanoSquare(void)
{
delete[] squareData;
}
int KardanoSquare::SetSize(const int NewSize)
{
if (NewSize >=4 && NewSize <= 64)
{
size = NewSize;
delete[] squareData;
squareData = new char[size*size];
Generate();
return 1;
}
else return 0;
}
int KardanoSquare::GetSize() const
{
return size;
}
void KardanoSquare::Generate()
{
for (int k=0; k<size*size; k++) squareData[k] = 0;
int i = rand()%size, j = rand()%size;
int c;
for (int k=0; k<size*size/4; k++)
{
c = 0;
while (c<5 && squareData[i*size+j]!=0)
{
i = rand()%size;
j = rand()%size;
c++;
}
if (squareData[i*size+j]!=0)
{
c = 0;
while (squareData[c]!=0) c++;
i = c/size;
j = c%size;
}
squareData[i*size+j] = 0x01;
squareData[(size-1-j)*size+i] = 0x02;
squareData[(size-1-i)*size+(size-1-j)] = 0x03;
squareData[j*size+(size-1-i)]= 0x04;
}
}
void KardanoSquare::Rotate()
{
for (int i=0; i<size*size; i++)
squareData[i] = (squareData[i]==4) ? 1 : squareData[i]+1;
}
int KardanoSquare::GetValue(const int Row, const int Col) const
{
if (Row >= 0 && Row <= size && Col >= 0 && Col <= size)
return squareData[Row*size+Col];
else return 0;
}
StaticTable.cpp
#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);
needDrawHHeader = true;
needDrawVHeader = true;
mainFont = CreateFont(-16,0,0,0,FW_BOLD,false,false,false,DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH,
NULL);
}
StaticTable::~StaticTable()
{
DeleteObject(mainFont);
}
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);
if (needDrawVHeader)
{
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);
if (needDrawHHeader)
{
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);
if (!needDrawHHeader) place.top -= rowHeight;
if (!needDrawVHeader) place.left -= colWidth;
SetBkMode(hDC, TRANSPARENT);
HBRUSH highlightBrush = CreateSolidBrush(RGB(210, 210, 210));
HFONT tempFont = (HFONT)SelectObject(hDC, mainFont);
HBRUSH tempBrush = (HBRUSH)SelectObject(hDC, highlightBrush);
for (int i=1; i<row; i++)
for (int j=1; j<col; j++)
{
if (highlights.at(i*col+j))
Rectangle(hDC, place.left+j*colWidth+1, place.top+i*rowHeight+1,
place.left+(j+1)*colWidth, place.top+(i+1)*rowHeight);
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);
SelectObject(hDC, tempBrush);
DeleteObject(highlightBrush);
}
int StaticTable::HighlightItem(const int Row, const int Col)
{
if (Row >= 1 && Row <= row && Col >= 1 && Col <= col)
{
highlights.at(Row*col+Col) = 1;
return 1;
}
else return 0;
}
int StaticTable::UnHighlightItem(const int Row, const int Col)
{
if (Row >= 1 && Row <= row && Col >= 1 && Col <= col)
{
highlights.at(Row*col+Col) = 0;
return 1;
}
else return 0;
}
void StaticTable::ClearHighlights()
{
for (int i=0; i<row*col; i++)
highlights.at(i) = 0;
}
int StaticTable::SetColNumber(const int Columns)
{
if (Columns > 1)
{
col = Columns+1;
data.clear();
data.resize(row*col);
highlights.clear();
highlights.resize(row*col);
return colWidth*((needDrawVHeader) ? col : col-1);
}
else return 0;
}
int StaticTable::SetRowNumber(const int Rows)
{
if (Rows > 1)
{
row = Rows+1;
data.clear();
data.resize(row*col);
highlights.clear();
highlights.resize(row*col);
return rowHeight*((needDrawHHeader) ? row : row-1);
}
else return 0;
}
int StaticTable::SetColWidth(const int Width)
{
if (Width > 0)
{
colWidth = Width;
return colWidth*((needDrawVHeader) ? col : col-1);
}
else return 0;
}
int StaticTable::SetRowHeight(const int Height)
{
if (Height > 0)
{
rowHeight = Height;
return rowHeight*((needDrawHHeader) ? row : row-1);
}
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;
}
CString StaticTable::GetItemText(const int Row, const int Column) const
{
if (Row >= 1 && Row <= row && Column >= 1 && Column <= col)
return data.at(Row*col+Column);
else return _T("");
}
void StaticTable::SetHHeaderVisibility(const bool isVisible)
{
needDrawHHeader = isVisible;
}
void StaticTable::SetVHeaderVisibility(const bool isVisible)
{
needDrawVHeader = isVisible;
}
void StaticTable::ClearItemsText()
{
for (int i=0; i<row*col; i++)
data.at(i) = _T("");
}
BEGIN_MESSAGE_MAP(StaticTable, CStatic)
END_MESSAGE_MAP()