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

📄 labviewandvc1dlg.cpp

📁 本例子是LABVIEW与VC++6.0混合编程的例子
💻 CPP
字号:
// LabViewAndVc1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "LabViewAndVc1.h"
#include "LabViewAndVc1Dlg.h"
#include "cwsafearray.h"
#include <math.h>
#include "cwgraph.h"

// Dispatch interfaces referenced by this interface
#include "cwpicturedisp.h"
#include "fontdisp.h"
#include "cwplot.h"
#include "cwannotation.h"
#include "cwplots.h"
#include "cwaxes.h"
#include "cwcursors.h"
#include "cwimage.h"
#include "PictureDisp.h"
#include "cwbindings.h"
#include "CWAnnotations.h"

#include <afxdisp.h>

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CLabViewAndVc1Dlg dialog

CLabViewAndVc1Dlg::CLabViewAndVc1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLabViewAndVc1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLabViewAndVc1Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	rate=10;
	gainn1=10;
	positionn1=0;
	gainn2=5;
	positionn2=0;
	count=1;
	m_bedisplay=1;
}

void CLabViewAndVc1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLabViewAndVc1Dlg)
	DDX_Control(pDX, IDC_CWGRAPH1, m_graph);
	DDX_Control(pDX, IDC_CWBOOLEAN1, m_button);
	DDX_Control(pDX, IDC_CWKNOB1, m_nob);
	DDX_Control(pDX, IDC_CWKNOB2, m_nob2);
	DDX_Control(pDX, IDC_CWNUMEDIT1, m_numedit1);
	DDX_Control(pDX, IDC_CWSLIDE1, m_slide1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLabViewAndVc1Dlg, CDialog)
	//{{AFX_MSG_MAP(CLabViewAndVc1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLabViewAndVc1Dlg message handlers

BOOL CLabViewAndVc1Dlg::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
	m_graph.ClearData();
//	m_graph.SetTrackMode(4);
	CString str="通用示波器";
	CString str1="开关";
	CString str2="调相";
	m_graph.SetCaption(str);
	m_button.SetCaption(str1);
	m_nob.SetCaption(str2);
	str2.Format("调幅");
	m_nob2.SetCaption(str2);
	str2.Format("滑动条");
	m_slide1.SetCaption(str2);;
//	double X[2],Yn[2];
 //   X[0]=5;  
 //   Yn[0]=5; 
//	X[1]=8; 
  //  Yn[1]=8; 
  //  X[0]=count;      
	//第count个点
   // Yn[0]=sin((double)count*2*3.1415926/rate)*gainn1+positionn1; 
//rate为扫描频率gainn、positionn为第n通道增益、偏移
//	VARIANT  cwxDataX=COleVariant(X[0]);
//	VARIANT  cwyDataYn=COleVariant(Yn[1]);
//	int n;
/*	for(n=0;n<2;n++)
	{	
		m_graph.SetXYData(COleVariant(X[n]));
	m_graph.SetXYData(COleVariant(Yn[n]));
	}*/
//	for(n=0;n<2;n++)
 //m_graph.GetPlots().Item(COleVariant((double)n)).ChartXvsY(COleVariant(X[n]),COleVariant(Yn[n]));
 //   CCWSafeArray cwxDataX(X,1);
 //   CCWSafeArray cwyDataYn(Yn,1);
//	for(n=0;n<2;n++)
 //   m_graph.GetPlots().Item(COleVariant(1.0)).ChartXvsY(cwxDataX,cwyDataYn);
	SetTimer(1,500,NULL);//刷新频率为1000Hz,为最高定时精度
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CLabViewAndVc1Dlg::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 CLabViewAndVc1Dlg::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 CLabViewAndVc1Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CLabViewAndVc1Dlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
//	 m_graph.ClearData();//示波器清屏
  int count1=-50;
  int i;
  double X[100],Y[100];
 for(i=0;i<100;i++)
 {
   X[i]=count1+i;  
   Y[i]=sin((double)(count+i)*3.1415926/rate)*gainn1+positionn1;   
 }
   CCWSafeArray cwxDataX1(X,100);
   CCWSafeArray cwyDataY1(Y,100);
   CCWSafeArray cwxDataX01(X,1);
   CCWSafeArray cwyDataY01(Y,1);
 for(i=0;i<100;i++)
 {
   Y[i]=sin((double)(count+i)*3.1415926/rate)*gainn2+positionn2+rand()%10; 
 }
 //  CCWSafeArray cwxDataX2(X,100);
   CCWSafeArray cwyDataY2(Y,100); 
   CCWSafeArray cwxDataX02(X,1);
   CCWSafeArray cwyDataY02(Y,1); 

/*  SAFEARRAY * psa;	// The safearray
  SAFEARRAYBOUND rgsabound[1];	// A one dimensional array
  double * pData;
  long lValue, lIndex;
  double  lValue1;*/
  rgsabound[0].lLbound = 0;	// With lower bound 0;
  rgsabound[0].cElements = 100;	// With 10 elements;
  psa = SafeArrayCreate(VT_R8, 1, rgsabound);	// Create an array of integers
  SafeArrayAccessData(psa,(void**)&pData);	// Get a pointer to the data

  for(i=0;i<100;i++) 
  {   
   pData[i]=sin((double)(count+i)*3.1415926/rate)*gainn2+positionn2; 
 }
  SafeArrayUnaccessData(psa);	// pData is now invalid.
 /* lIndex = 5;
  SafeArrayGetElement(psa,&lIndex,(void*)&lValue);	// lValue is now 5
  ++lValue;								// lValue is now 6
  SafeArrayPutElement(psa,&lIndex,(void*)&lValue);	// psa[5] is now 6*/
   CCWSafeArray cwyDataY3(psa,VT_R8); 
   CCWSafeArray cwxDataX03(X,1);
   CCWSafeArray cwyDataY03(pData,1);
    //显示第1通道的波形
 if(m_bedisplay)
  {
  m_graph.GetPlots().Item(COleVariant(1.0)).PlotXvsY(cwxDataX1,cwyDataY1); 
  m_graph.GetPlots().Item(COleVariant(2.0)).PlotXvsY(cwxDataX1,cwyDataY2); //显示1 0 0个点
  m_graph.GetPlots().Item(COleVariant(3.0)).PlotXvsY(cwxDataX1,cwyDataY3);
  }
  //当要消隐第n通道波形时
  else
  {
  m_graph.GetPlots().Item(COleVariant(1.0)).PlotXvsY(cwxDataX01,cwyDataY01);//只显示一个点
  m_graph.GetPlots().Item(COleVariant(2.0)).PlotXvsY(cwxDataX02,cwyDataY02);//只显示一个点
  m_graph.GetPlots().Item(COleVariant(3.0)).PlotXvsY(cwxDataX03,cwyDataY03);
  }
  //m-graph.GetPlots().Item(COleVariant(n.0)).PlotXvsY(cwxDataX0,cwyDataY0);//只显示一个点
  SafeArrayDestroy(psa);
  CDialog::OnTimer(nIDEvent);
}

BEGIN_EVENTSINK_MAP(CLabViewAndVc1Dlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CLabViewAndVc1Dlg)
	ON_EVENT(CLabViewAndVc1Dlg, IDC_CWBOOLEAN1, 1 /* ValueChanged */, OnValueChangedCwboolean1, VTS_BOOL)
	ON_EVENT(CLabViewAndVc1Dlg, IDC_CWGRAPH1, -600 /* Click */, OnClickCwgraph1, VTS_NONE)
	ON_EVENT(CLabViewAndVc1Dlg, IDC_CWKNOB1, -600 /* Click */, OnClickCwknob1, VTS_NONE)
	ON_EVENT(CLabViewAndVc1Dlg, IDC_CWKNOB2, -600 /* Click */, OnClickCwknob2, VTS_NONE)
	ON_EVENT(CLabViewAndVc1Dlg, IDC_CWNUMEDIT1, 1 /* ValueChanged */, OnValueChangedCwnumedit1, VTS_PVARIANT VTS_PVARIANT VTS_BOOL)
	ON_EVENT(CLabViewAndVc1Dlg, IDC_CWSLIDE1, 1 /* PointerValueChanged */, OnPointerValueChangedCwslide1, VTS_I4 VTS_PVARIANT)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CLabViewAndVc1Dlg::OnValueChangedCwboolean1(BOOL Value) 
{
	// TODO: Add your control notification handler code here
	m_bedisplay=!m_bedisplay;
}

void CLabViewAndVc1Dlg::OnClickCwgraph1() 
{
	// TODO: Add your control notification handler code here
	
}

void CLabViewAndVc1Dlg::OnClickCwknob1()  //旋钮
{
	// TODO: Add your control notification handler code here
    //AfxMessageBox("测试一下!");
   count++;
}

void CLabViewAndVc1Dlg::OnClickCwknob2() 
{
	// TODO: Add your control notification handler code here
	gainn1++;
	gainn2++;
}

void CLabViewAndVc1Dlg::OnValueChangedCwnumedit1(VARIANT FAR* Value, VARIANT FAR* PreviousValue, BOOL OutOfRange) 
{
	// TODO: Add your control notification handler code here
	CString  str=m_numedit1.GetText();	
	int i=atoi(str);
	positionn1=(float)i;
//	str.Format("偏移量为%d",i);
//	AfxMessageBox(str);
}

void CLabViewAndVc1Dlg::OnPointerValueChangedCwslide1(long Pointer, VARIANT FAR* Value) 
{
	// TODO: Add your control notification handler code here
  	float      a11;
	VARIANT  test1;
	test1=m_slide1.GetValue();	
	a11=test1.dblVal;
    positionn2=a11;

}

void CLabViewAndVc1Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
/*	VARIANT  test1;
	test1.vt=VT_R8;
	test1.dblVal=3.67;
	m_slide1.SetValue(test1);*/
	/*  CString  str;
	VARIANT aa=m_slide1.GetValue();	
	BYTE i=aa.bVal;
	str.Format("偏移量为%d",i);
	AfxMessageBox(str);*/
	CString  str;
	float      a11;
	VARIANT  test1;
	test1=m_slide1.GetValue();	 //经测试,数值为double类型
	str=VariantToString(&test1);
//	a11=test1.dblVal;
//	str.Format("偏移量为%f",a11);
	AfxMessageBox(str);

}

CString CLabViewAndVc1Dlg::VariantToString(VARIANT *va)
{

	CString s;

    switch(va->vt)

      { /* vt */

       case VT_BSTR:

          return CString(va->bstrVal);

       case VT_BSTR | VT_BYREF:

          return CString(*va->pbstrVal);

       case VT_I4:

          s.Format(_T("%d"), va->lVal);

          return s;

       case VT_I4 | VT_BYREF:

          s.Format(_T("%d"), *va->plVal);

       case VT_R8:

          s.Format(_T("%f"), va->dblVal);

          return s;

      // ... 剩下的类型转换由读者自己完成

       default:

          ASSERT(FALSE); // unknown VARIANT type (this ASSERT is optional)

        return CString("");

      } /* vt */

}

⌨️ 快捷键说明

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