基于litecad的程序画坐标轴及坐标文字
2014 年 3 月 18 日 基于litecad的程序画坐标轴及坐标文字无评论
litecad库并没有包含坐标轴的自定义,只有包含很多点的grid,题设:需要实现一个坐标轴方格,并且包含坐标文字,位于cad图层之下,拖动时坐标轴位置不变,坐标文字实时更新。
解决方案:
通过监听litecad的Paint事件(lcOnEventPaint),在之前和之后画图。
1.声明回调函数:
lcOnEventPaint( &CCADView::ProcPaint );
2.函数实现:
void CALLBACK CCADView::ProcPaint( HANDLE hLcWnd, HANDLE hView, int Mode, HDC hDC, int Left, int Top, int Right, int Bottom )
{
int WIDTH = Right-Left;
int HEIGHT = Bottom-Top;
int DELTA = HEIGHT/6;
//获取当前cad坐标
currentLeft = lcPropGetFloat( hView, LC_PROP_VIEW_LEF );
currentTop = lcPropGetFloat( hView, LC_PROP_VIEW_TOP );
currentRight = lcPropGetFloat( hView, LC_PROP_VIEW_RIG );
currentBottom = lcPropGetFloat( hView, LC_PROP_VIEW_BOT );
double scaleX = (currentRight-currentLeft)/((WIDTH)*1.0);
double scaleY = (currentTop-currentBottom)/((HEIGHT)*1.0);
//定义字体格式
HFONT hFont=CreateFont(10,10,0,0,FW_DONTCARE,false,false,false,
CHINESEBIG5_CHARSET,OUT_CHARACTER_PRECIS,
CLIP_CHARACTER_PRECIS,DEFAULT_QUALITY,
FF_MODERN,L"Arial");
SelectObject(hDC, hFont);
SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(50,50,50));
SetTextAlign(hDC, TA_LEFT);
CString text;
//litecad绘制之前,在HDC上绘制坐标轴
if (Mode==0)
{
HPEN pen=CreatePen(NULL,2,RGB(50,50,50));
SelectObject(hDC,pen);
for (int i=1;(Top+DELTA*i)<Bottom;i++)
{
MoveToEx(hDC, Left, Top+DELTA*i, NULL);
LineTo(hDC, Right, Top+DELTA*i);
if (isCadInited)
{
text.Format(L"%.0f", currentTop-scaleY*DELTA*i);
TextOut(hDC, Left+3, Top+DELTA*i+3, text, text.GetLength());
}
}
for (int i=1;(Left+DELTA*i)<Right;i++)
{
MoveToEx(hDC, Left+DELTA*i, Top, NULL);
LineTo(hDC, Left+DELTA*i, Bottom);
if (isCadInited)
{
text.Format(L"%.0f", currentLeft+scaleX*DELTA*(i-1));
TextOut(hDC, Left+DELTA*(i-1)+3, Bottom-15, text, text.GetLength());
}
}
}
//litecad绘制之后
if (Mode==1)
{
//第一次初始化时要在cad画之后绘制坐标文字
if (!isCadInited)
{
for (int i=1;(Top+DELTA*i)<Bottom;i++)
{
text.Format(L"%.0f", currentTop-scaleY*DELTA*i);
TextOut(hDC, Left+3, Top+DELTA*i+3, text, text.GetLength());
}
for (int i=1;(Left+DELTA*i)<Right;i++)
{
text.Format(L"%.0f", currentLeft+scaleX*DELTA*(i-1));
TextOut(hDC, Left+DELTA*(i-1)+3, Bottom-15, text, text.GetLength());
}
isCadInited = true;
}
}
}
{
int WIDTH = Right-Left;
int HEIGHT = Bottom-Top;
int DELTA = HEIGHT/6;
//获取当前cad坐标
currentLeft = lcPropGetFloat( hView, LC_PROP_VIEW_LEF );
currentTop = lcPropGetFloat( hView, LC_PROP_VIEW_TOP );
currentRight = lcPropGetFloat( hView, LC_PROP_VIEW_RIG );
currentBottom = lcPropGetFloat( hView, LC_PROP_VIEW_BOT );
double scaleX = (currentRight-currentLeft)/((WIDTH)*1.0);
double scaleY = (currentTop-currentBottom)/((HEIGHT)*1.0);
//定义字体格式
HFONT hFont=CreateFont(10,10,0,0,FW_DONTCARE,false,false,false,
CHINESEBIG5_CHARSET,OUT_CHARACTER_PRECIS,
CLIP_CHARACTER_PRECIS,DEFAULT_QUALITY,
FF_MODERN,L"Arial");
SelectObject(hDC, hFont);
SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(50,50,50));
SetTextAlign(hDC, TA_LEFT);
CString text;
//litecad绘制之前,在HDC上绘制坐标轴
if (Mode==0)
{
HPEN pen=CreatePen(NULL,2,RGB(50,50,50));
SelectObject(hDC,pen);
for (int i=1;(Top+DELTA*i)<Bottom;i++)
{
MoveToEx(hDC, Left, Top+DELTA*i, NULL);
LineTo(hDC, Right, Top+DELTA*i);
if (isCadInited)
{
text.Format(L"%.0f", currentTop-scaleY*DELTA*i);
TextOut(hDC, Left+3, Top+DELTA*i+3, text, text.GetLength());
}
}
for (int i=1;(Left+DELTA*i)<Right;i++)
{
MoveToEx(hDC, Left+DELTA*i, Top, NULL);
LineTo(hDC, Left+DELTA*i, Bottom);
if (isCadInited)
{
text.Format(L"%.0f", currentLeft+scaleX*DELTA*(i-1));
TextOut(hDC, Left+DELTA*(i-1)+3, Bottom-15, text, text.GetLength());
}
}
}
//litecad绘制之后
if (Mode==1)
{
//第一次初始化时要在cad画之后绘制坐标文字
if (!isCadInited)
{
for (int i=1;(Top+DELTA*i)<Bottom;i++)
{
text.Format(L"%.0f", currentTop-scaleY*DELTA*i);
TextOut(hDC, Left+3, Top+DELTA*i+3, text, text.GetLength());
}
for (int i=1;(Left+DELTA*i)<Right;i++)
{
text.Format(L"%.0f", currentLeft+scaleX*DELTA*(i-1));
TextOut(hDC, Left+DELTA*(i-1)+3, Bottom-15, text, text.GetLength());
}
isCadInited = true;
}
}
}
Tags: axis cad litecad 坐标轴
发表评论