VC++ 绘制简单电子元件
工具:
http://blog.csdn.net/bcbobo21cn/article/details/44200205
demo工程:
http://pan.baidu.com/s/1qYCE9rQ
新建一個工程
更改窗口標題;
添加工具欄圖標;
添加工具欄按鈕的狀態(tài)欄提示;
添加工具欄按鈕的事件函數(shù),此時需要手動添加;
視類頭文件手動添加
//{{AFX_MSG(CDzyjView)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnDrawdz(); ?//手動添加
afx_msg void OnDrawdr();??//手動添加
afx_msg void OnDrawdg();??//手動添加
afx_msg void OnDrawerjg();??//手動添加
//}}AFX_MSG
視類實現(xiàn)文件添加
BEGIN_MESSAGE_MAP(CDzyjView, CView)
//{{AFX_MSG_MAP(CDzyjView)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
ON_COMMAND(ID_BUTTONdz, CDzyjView::OnDrawdz)??//手動添加
ON_COMMAND(ID_BUTTONdr, CDzyjView::OnDrawdr)??//手動添加
ON_COMMAND(ID_BUTTONdg, CDzyjView::OnDrawdg)??//手動添加
ON_COMMAND(ID_BUTTONerjg, CDzyjView::OnDrawerjg)??//手動添加
END_MESSAGE_MAP()
新加工具欄按鈕的響應(yīng)函數(shù)中設(shè)定繪制元件類別
void CDzyjView::OnDrawdz()
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ?//MessageBox("dz","提示",MB_OK);
yjtype=1;
? ? ? ? ? ? ? ?}
視類頭文件添加成員變量和函數(shù)
private:
CPoint point1;
int yjtype;
CPoint Four_Point[4];
// Operations
public:
void Drawdz(CDC *pDC);
void Drawdr(CDC *pDC);
void Drawdg(CDC *pDC);
void Drawerjg(CDC *pDC);
添加鼠標單擊響應(yīng)函數(shù)
鼠標左鍵按下時,獲取繪制點中心坐標;刷新窗口;
void CDzyjView::OnLButtonDown(UINT nFlags, CPoint point)?
{
// TODO: Add your message handler code here and/or call default
point1.x=point.x;
point1.y=point.y;
Invalidate();
CView::OnLButtonDown(nFlags, point);
}
畫電阻函數(shù)
void CDzyjView::Drawdz(CDC *pDC)
{
pDC->MoveTo(point1.x-20,point1.y);
pDC->LineTo(point1.x-12,point1.y);
pDC->MoveTo(point1.x+20,point1.y);
pDC->LineTo(point1.x+12,point1.y);
Four_Point[0].x=point1.x-12;
Four_Point[0].y=point1.y-5;
Four_Point[1].x=point1.x+12;
Four_Point[1].y=point1.y-5;
Four_Point[2].x=point1.x+12;
Four_Point[2].y=point1.y+5;
Four_Point[3].x=point1.x-12;
Four_Point[3].y=point1.y+5;
pDC->Polygon(Four_Point,4);
//pDC->Polyline(Four_Point,4);
/*
pDC->MoveTo(Four_Point[0]);
pDC->LineTo(Four_Point[1]);
pDC->MoveTo(Four_Point[1]);
pDC->LineTo(Four_Point[2]);
pDC->MoveTo(Four_Point[2]);
pDC->LineTo(Four_Point[3]);
pDC->MoveTo(Four_Point[3]);
pDC->LineTo(Four_Point[0]);
*/
}
在OnDraw中調(diào)用畫電阻函數(shù)
void CDzyjView::OnDraw(CDC* pDC)
{
CDzyjDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
switch(yjtype)
{
case 1:
Drawdz(pDC);
break;
case 2:
break;
default:
break;
}
}
看下效果;
關(guān)于??local function definitions are illegal 錯誤
F:\vc6prj\圖形圖像\dzyj\dzyjView.cpp(80) : error C2601: 'OnPreparePrinting' : local function definitions are illegal
......
執(zhí)行 cl.exe 時出錯.
編譯時出現(xiàn)一堆??local function definitions are illegal 錯誤;一個可能原因是丟失了反大括號;
void CDzyjView::OnDraw(CDC* pDC)
{
CDzyjDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
switch(yjtype)
{
case 1:
Drawdz(pDC);
break;
case 2:
break;
default:
break;
}
} ? ? // 丟失此大括號會產(chǎn)生?local function definitions are illegal
今天先到這里......
總結(jié)
以上是生活随笔為你收集整理的VC++ 绘制简单电子元件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中文分词学习总结
- 下一篇: http协议状态码和web错误解决总结