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

📄 ncdlg.cpp

📁 自己编的把数控代码用RS232口由计算机传输到数控机床的程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// NCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NC.h"
#include "NCDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "MySocket.h"

#include "Com1Set.h"
#include "Com2Set.h"
#include "MachineAdd.h"
#include "MachineEdit.h"
#include <comdef.h>
#include <afxdisp.h>
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNCDlg dialog

CNCDlg::CNCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNCDlg)
	m_strEdit1 = _T("");
	m_strEdit2 = _T("");
	m=1;
	n=1;
	m_name = _T("");
	m_com = _T("");
	m_description = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CNCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNCDlg)
	DDX_Control(pDX, IDC_PROGRESS2, m_progress2);
	DDX_Control(pDX, IDC_PROGRESS1, m_progress1);
	DDX_Control(pDX, IDC_TREE, m_tree);
	DDX_Control(pDX, IDC_MSCOM1, m_ctrlComm1);
	DDX_Control(pDX, IDC_MSCOM2, m_ctrlComm2);
	DDX_Text(pDX, IDC_EDIT1, m_strEdit1);
	DDX_Text(pDX, IDC_EDIT2, m_strEdit2);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNCDlg, CDialog)
	//{{AFX_MSG_MAP(CNCDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_NCEDIT, OnNcedit)
	ON_COMMAND(ID_TOINTERNET, OnTointernet)
	ON_COMMAND(IDD_ABOUTBOX, OnAboutbox)
	ON_NOTIFY(NM_RCLICK, IDC_TREE, OnRclickTree)
	ON_COMMAND(ID_MACHINE_ADD, OnMachineAdd)
	ON_COMMAND(ID_MACHINE_DEL, OnMachineDel)
	ON_COMMAND(ID_COM_RECEIVE, OnComReceive)
	ON_COMMAND(ID_COM_SEND, OnComSend)
	ON_COMMAND(ID_MACHINE_EDIT, OnMachineEdit)
	ON_COMMAND(ID_NCSAVE1, OnNcsave1)
	ON_COMMAND(ID_NCSAVE2, OnNcsave2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNCDlg message handlers

BOOL CNCDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here


	/*HICON hIcon = NULL;
	hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_KEBIAO), IMAGE_ICON, 24, 24, 0);
	m_iImageList.Add(hIcon);
	m_tree.SetImageList ( &m_iImageList,TVSIL_NORMAL );*/
	DWORD dwStyle = GetWindowLong(m_tree.m_hWnd,GWL_STYLE);
	dwStyle |= TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT;
	SetWindowLong(m_tree.m_hWnd,GWL_STYLE,dwStyle);
    m_hRoot=m_tree.InsertItem( "数控机床管理器");
	if(m_pSet.IsOpen())
		m_pSet.Close();
	m_pSet.Open();
	m_pSet.MoveFirst();
	int j=m_pSet.GetRecordCount();//AfxMessageBox((CString)j);
/*	for(int i=0;i<j;i++)
	{
		CString mach;
	    mach=m_pSet.m_name;
	    m_tree.InsertItem(mach, 0, 0, m_hRoot);
        m_pSet.MoveNext();
	}*/
	CString mach;
	
	while(!m_pSet.IsEOF())
		{
		    mach=m_pSet.m_name;
			m_tree.InsertItem(mach, 0, 0, m_hRoot);
			m_pSet.MoveNext();
		}

	
//	BrowseDir( "管理器", m_hRoot );
	m_tree.Expand(m_hRoot,TVE_EXPAND);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CNCDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CNCDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CNCDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CNCDlg::OnNcedit() 
{
  CFileDialog dlg(true);
  if(dlg.DoModal()==IDOK)
  {
      ShellExecute(NULL, NULL,dlg.GetPathName(),NULL, NULL, SW_SHOW);//SW_SHOWNORMAL);
  }
}

void CNCDlg::OnTointernet() 
{
	// TODO: Add your command handler code here
	CMySocket *dlg=new CMySocket;
    dlg->Create(IDD_DIALOG1,NULL);
    dlg->ShowWindow(SW_SHOW);
	
}

void CNCDlg::OnAboutbox() 
{
	// TODO: Add your command handler code here
	CAboutDlg dlg;
	dlg.DoModal();
	
}

BEGIN_EVENTSINK_MAP(CNCDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CNCDlg)
	ON_EVENT(CNCDlg, IDC_MSCOM1, 1 /* OnComm */, OnAcceptCom1, VTS_NONE)
	ON_EVENT(CNCDlg, IDC_MSCOM2, 1 /* OnComm */, OnAcceptCom2, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

//添加pop菜单

void CNCDlg::OnRclickTree(NMHDR* pNMHDR, LRESULT* pResult) 
{

	CMenu* menu;
    CMenu* pPopup;
    menu = new CMenu();
    
    menu->LoadMenu(IDR_MENU1);
    
    pPopup=menu->GetSubMenu(0);
    CPoint Point;
    GetCursorPos(&Point);
    pPopup->TrackPopupMenu(TPM_LEFTALIGN,Point.x,Point.y,
    AfxGetMainWnd(),NULL);
 
    delete menu;
	*pResult = 0;
}
//线程
CWinThread	*pThreadSendFile1;	//发送文件线程-->_SendFileThread
CWinThread	*pThreadSendFile2;	//发送文件线程-->_SendFileThread

UINT _SendFileThread1(LPVOID lparam)
{
    CNCDlg *pDlg=(CNCDlg *)lparam;
    
	    CFile myFile;
		myFile.Open(pDlg->m_fileName1, CFile::modeRead );
		
        int lent=myFile.GetLength();
        char *pBuft = new char[lent+1];
        memset(pBuft,'\0',lent+1);
		memset(pBuft,0,lent);
        myFile.Read(pBuft,lent); 
        myFile.Close();
		CString strt1 = pBuft;
		pDlg->SetDlgItemText(IDC_EDIT1,strt1);
//		pDlg->m_strEdit1=strt1;

        CByteArray array;
		int n,m;
  
          n=lent/256;
		  m=lent%256;

		  if(n!=0)
		  {
			  for(int j=0;j<n;j++)
			  {
				  array.RemoveAll();
		          array.SetSize(256);
		  
		        for(int i=0;i<256;i++)
				{
					
					array.SetAt(i,pBuft[j*256+i]);
				    pDlg->m_progress1.SetPos((j*256+i)*100/lent);
				}
			    pDlg->m_ctrlComm1.SetOutput(COleVariant(array));//发送数据
			  }
	          array.RemoveAll();
		      array.SetSize(m);
		      for(int k=0;k<m;k++)
			  {
				  array.SetAt(k,pBuft[n*256+k]);
			      pDlg->m_progress1.SetPos((n*256+k)*100/lent);

			  }
			     pDlg->m_ctrlComm1.SetOutput(COleVariant(array));//发送数据
		  }
		  else
		  {
			  array.RemoveAll();
		      array.SetSize(lent);
			  for(int l=0;l<lent;l++)
				{
				    array.SetAt(l,pBuft[l]);
				    pDlg->m_progress1.SetPos(l*100/lent);
				}
			    pDlg->m_ctrlComm1.SetOutput(COleVariant(array));//发送数据

		  } 
        delete pBuft;
        return 0;

}
UINT _SendFileThread2(LPVOID lparam)
{
    CNCDlg *pDlg=(CNCDlg *)lparam;
    
	    CFile myFile;
		myFile.Open(pDlg->m_fileName2, CFile::modeRead );
		
        int lent=myFile.GetLength();
        char *pBuft = new char[lent+1];
        memset(pBuft,'\0',lent+1);
		memset(pBuft,0,lent);
        myFile.Read(pBuft,lent); 
        myFile.Close();
        CString strt2 = pBuft;
//		pDlg->SetDlgItemText(IDC_EDIT2,"");
		pDlg->SetDlgItemText(IDC_EDIT2,strt2);
//		pDlg->m_strEdit2=strt2;
		CByteArray array;
		int n,m;
  
          n=lent/256;
		  m=lent%256;

		  if(n!=0)
		  {
			  for(int j=0;j<n;j++)
			  {
				  array.RemoveAll();
		          array.SetSize(256);
		  
		        for(int i=0;i<256;i++)
				{
					
					array.SetAt(i,pBuft[j*256+i]);
				    pDlg->m_progress2.SetPos((j*256+i)*100/lent);
				}
			    pDlg->m_ctrlComm2.SetOutput(COleVariant(array));//发送数据
			  }
	          array.RemoveAll();
		      array.SetSize(256);
		      for(int k=0;k<m;k++)
			  {
				  array.SetAt(k,pBuft[n*256+k]);
			      pDlg->m_progress2.SetPos((n*256+k)*100/lent);

			  }
			     pDlg->m_ctrlComm2.SetOutput(COleVariant(array));//发送数据
		  }
		  else
		  {
			  array.RemoveAll();
		      array.SetSize(lent);
			  for(int l=0;l<lent;l++)
				{
				    array.SetAt(l,pBuft[l]);
				    pDlg->m_progress2.SetPos(l*100/lent);
				}
			    pDlg->m_ctrlComm2.SetOutput(COleVariant(array));//发送数据

		  } 
        delete pBuft;
        return 0;

}
//com1发送
void CNCDlg::OnCom1Send() 
{
//	SetDlgItemText(IDC_EDIT1,"");
	if(m_ctrlComm1.GetPortOpen())
	m_ctrlComm1.SetPortOpen(false);

    m_ctrlComm1.SetCommPort(1);
    if( !m_ctrlComm1.GetPortOpen())
        m_ctrlComm1.SetPortOpen(TRUE);//打开串口
    else
    AfxMessageBox("不能打开!");
	HTREEITEM  m_hTreeItem = m_tree.GetSelectedItem();
    CString s1 = m_tree.GetItemText(m_hTreeItem);
	SetDlgItemText(IDC_STATICCOM1,"COM1-"+s1); 
	SetDlgItemText(IDC_STATICCOM11,"COM1-"+s1);

	
	CString m_jiaoyancode;
	CString m_set;
    CCom1Set dlg;
	if(dlg.DoModal()==IDOK)
	{

	    if(dlg.m_jiaoyan=="缺省")
		    m_jiaoyancode="n";
	    if(dlg.m_jiaoyan=="偶")
		    m_jiaoyancode="e";
	    if(dlg.m_jiaoyan=="奇")
		    m_jiaoyancode="o";

⌨️ 快捷键说明

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