Всем привет.Нужно преобразовать из С++ в C#. Буду благодарен!
Код:
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);
}