⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 switchboxview.cpp

📁 这是一个开关盒布线程序
💻 CPP
字号:
// SwitchBoxView.cpp : implementation of the CSwitchBoxView class
//

#include "stdafx.h"
#include "SwitchBox.h"

#include "SwitchBoxDoc.h"
#include "SwitchBoxView.h"
#include "TimeSet.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


int count = 0;
/////////////////////////////////////////////////////////////////////////////
// CSwitchBoxView

IMPLEMENT_DYNCREATE(CSwitchBoxView, CView)

BEGIN_MESSAGE_MAP(CSwitchBoxView, CView)
	//{{AFX_MSG_MAP(CSwitchBoxView)
	ON_WM_TIMER()
	ON_COMMAND(IDD_TIME, OnTime)
	ON_COMMAND(ID_SWITCHBOX_SET, OnSwitchboxSet)
	ON_WM_RBUTTONDOWN()
	//}}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)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSwitchBoxView construction/destruction

CSwitchBoxView::CSwitchBoxView()
{
	// TODO: add construction code here
	point[0].x = 300;
	point[0].y = 100;
	point[1].x = 400;
	point[1].y = 100;
	point[2].x = 500;
	point[2].y = 200;
	point[3].x = 500;
	point[3].y = 300;
	point[4].x = 350;
	point[4].y = 400;
	point[5].x = 200;
	point[5].y = 320;
	point[6].x = 200;
	point[6].y = 250;
	point[7].x = 200;
	point[7].y = 170;
	data[0]=data[1]=data[2]=data[3]=data[4]=data[5]=data[6]=data[7]=1;
}

CSwitchBoxView::~CSwitchBoxView()
{
}

BOOL CSwitchBoxView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CSwitchBoxView drawing

void CSwitchBoxView::OnDraw(CDC* pDC)
{
	CSwitchBoxDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CRect rc;
	GetClientRect(&rc);
	CBrush brushCtl;
	brushCtl.CreateSolidBrush(RGB(200,230,255));
  	pDC->Rectangle(rc);
	pDC->FillRect(rc,&brushCtl) ;
	brushCtl.DeleteObject();
	CDC *pdC = GetDC();
	pDC->TextOut(170,30,"请点击“开关盒布线”来设置布线方案和布线时间间隔");
	pdC->MoveTo(200,100);
	pdC->LineTo(500,100);
	pdC->LineTo(500,400);
	pdC->LineTo(200,400);
	pdC->LineTo(200,100);

	pdC->SetTextColor(RGB(200,0,200));
	CRect rect(290,80,320,100);
	pdC->DrawText("1",&rect,DT_CENTER);
	rect.SetRect(390,80,420,100);
	pdC->DrawText("2",&rect,DT_CENTER);
	rect.SetRect(500,190,520,210);
	pdC->DrawText("3",&rect,DT_CENTER);
	rect.SetRect(500,290,520,310);
	pdC->DrawText("4",&rect,DT_CENTER);
	rect.SetRect(340,405,350,420);
	pdC->DrawText("5",&rect,DT_CENTER);
	rect.SetRect(180,310,200,330);
	pdC->DrawText("6",&rect,DT_CENTER);
	rect.SetRect(180,240,200,260);
	pdC->DrawText("7",&rect,DT_CENTER);
	rect.SetRect(180,160,200,180);
	pdC->DrawText("8",&rect,DT_CENTER);

	CBrush myrush,*oldrush;
	myrush.CreateSolidBrush(RGB(0,255,0));
	oldrush=pDC->SelectObject(&myrush);
	for (int i=0;i<8;i++)
	pDC->Ellipse(point[i].x-5,point[i].y-5,point[i].x+5,point[i].y+5);
	
}

/////////////////////////////////////////////////////////////////////////////
// CSwitchBoxView printing

BOOL CSwitchBoxView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CSwitchBoxView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CSwitchBoxView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CSwitchBoxView::DrawLine(CDC *pDC, CPoint start, CPoint end )
{
// 	CPen pen;
// 	pen.CreatePen(PS_SOLID, 1, RGB(0,0,255));  
// 	CPen *pOldPen;
// 	pOldPen=pDC->SelectObject(&pen); 
// 	pDC->SelectObject(&pen);   
	pDC->MoveTo( start );
	pDC->LineTo( end );
}
/////////////////////////////////////////////////////////////////////////////
// CSwitchBoxView diagnostics

#ifdef _DEBUG
void CSwitchBoxView::AssertValid() const
{
	CView::AssertValid();
}

void CSwitchBoxView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CSwitchBoxDoc* CSwitchBoxView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSwitchBoxDoc)));
	return (CSwitchBoxDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSwitchBoxView message handlers

void CSwitchBoxView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CDC *pDC = GetDC();
    CPen aPen, *oldpen;
	aPen.CreatePen(PS_SOLID,2,RGB(255,0,0));
	oldpen=pDC->SelectObject(&aPen);
		if ( count == 0)
	{
		DrawLine(pDC,point[data[0]-1],point[data[1]-1]);
		count++;
		return;
	}
	if ( count == 1)
	{
		DrawLine(pDC,point[data[2]-1],point[data[3]-1]);
		count++;
		return;
	}
	if ( count == 2)
	{
		DrawLine(pDC,point[data[4]-1],point[data[5]-1]);
		count++;
		return;
	}
	if (count == 3)
	{
		DrawLine(pDC,point[data[6]-1],point[data[7]-1]);
		count = 0;
	    MessageBox(" 布线完毕,数据已保存!","完毕");
		KillTimer(100);
		return ;
	
	}
	ReleaseDC(pDC);
	CView::OnTimer(nIDEvent);

}

void CSwitchBoxView::OnTime() 
{	
	CTimeSet timeset;
	timeset.DoModal();
	SetTimer(100,int(timeset.m_time*1000),NULL);
}

void CSwitchBoxView::OnSwitchboxSet() 
{
	CBoxSet boxset;
	if ( boxset.DoModal() == IDOK )
	{
		data[0] = boxset.m_Group1_1;
		data[1] = boxset.m_Group1_2;
		data[2] = boxset.m_Group2_1;
		data[3] = boxset.m_Group2_2;
		data[4] = boxset.m_Group3_1;
		data[5] = boxset.m_Group3_2;
		data[6] = boxset.m_Group4_1;
		data[7] = boxset.m_Group4_2;
		Invalidate(true);
	}
}

void CSwitchBoxView::SaveAsBmp(CString filename)
{
	int iWidth = 800;
	int iHeight = 600;
    int iPixel  = 16;
	//图形格式参数
    LPBITMAPINFO lpbmih = new BITMAPINFO;
    lpbmih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    lpbmih->bmiHeader.biWidth = iWidth;
    lpbmih->bmiHeader.biHeight = iHeight;
    lpbmih->bmiHeader.biPlanes = 1;
    lpbmih->bmiHeader.biBitCount = iPixel;
    lpbmih->bmiHeader.biCompression = BI_RGB;
    lpbmih->bmiHeader.biSizeImage = 0;
    lpbmih->bmiHeader.biXPelsPerMeter = 0;
    lpbmih->bmiHeader.biYPelsPerMeter = 0;
    lpbmih->bmiHeader.biClrUsed = 0;
    lpbmih->bmiHeader.biClrImportant = 0;

    //创建位图数据
    HDC hdc,hdcMem;
    HBITMAP hBitMap = NULL;
    CBitmap *pBitMap = NULL;
    CDC *pMemDC = NULL;
    BYTE *pBits;

    hdc = CreateIC(TEXT("DISPLAY"),NULL,NULL,NULL);
    hdcMem = CreateCompatibleDC(hdc);
    hBitMap = CreateDIBSection(hdcMem,lpbmih,DIB_PAL_COLORS,(void **)&pBits,NULL,0);
    pBitMap = new CBitmap;
    pBitMap->Attach(hBitMap);
    pMemDC = new CDC;
    pMemDC->Attach(hdcMem);
    pMemDC->SelectObject(pBitMap);
     //
	CRect rc(0,0,iWidth,iHeight);
	pMemDC->SetBkMode(TRANSPARENT);


    //保存到文件并创建位图结构
    BITMAPFILEHEADER bmfh;
    ZeroMemory(&bmfh,sizeof(BITMAPFILEHEADER));
    *((char *)&bmfh.bfType) = 'B';
    *(((char *)&bmfh.bfType) + 1) = 'M';
    bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    bmfh.bfSize = bmfh.bfOffBits + (iWidth * iHeight) * iPixel / 8;

    TCHAR szBMPFileName[128];
    int iBMPBytes = iWidth * iHeight * iPixel / 8;
    strcpy(szBMPFileName,filename);
    CFile file;
    if(file.Open(szBMPFileName,CFile::modeWrite | CFile::modeCreate))
    {
        file.Write(&bmfh,sizeof(BITMAPFILEHEADER));
        file.Write(&(lpbmih->bmiHeader),sizeof(BITMAPINFOHEADER));
        file.Write(pBits,iBMPBytes);
        file.Close();
    }

    pMemDC->DeleteDC();
    delete pMemDC;	pMemDC  = NULL;
    delete pBitMap; pBitMap = NULL;
    delete lpbmih;  lpbmih  = NULL;

}

void CSwitchBoxView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CFileDialog dlg(false,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		"位图文件(*.bmp)|*.bmp|",NULL);
	if (dlg.DoModal()!= IDOK) return;
	CString filename = dlg.GetFileName() + ".bmp";
	SaveAsBmp(filename);	
	
	
	CView::OnRButtonDown(nFlags, point);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -