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

📄 spectrumanalyzerdemodlg.cpp

📁 一个组太软件中
💻 CPP
字号:
// SpectrumAnalyzerDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SpectrumAnalyzerDemo.h"
#include "SpectrumAnalyzerDemoDlg.h"

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


#define WM_USER_SCAN_NEW_POINT WM_USER+0x100  // when a new signal strength arrives from radio
#define WM_USER_SET_TO_FREQ WM_USER+0x102  // when user clicks the spectrum for setting to freq
#define WM_USER_SHOW_CURRENT_FREQ WM_USER+0x103 // when user moves the mouse, show current frequency 
#define WM_USER_SCAN_FINISHED WM_USER+0x104  // when scan finishes. do complementary jobs

typedef struct
{
    BOOL* m_bpDoContinue;
    HWND m_hWnd;
	DWORD m_iScanStartFreq;
	DWORD m_iScanEndFreq;
	DWORD m_iScanStepFreq;
	BOOL *m_bpIfIsScanning;
	BOOL *m_bpIfContinous;
	
	CPoint* m_pNewPoint;

} TestSweepThreadParams;

UINT TestSweepThread(LPVOID pParam);


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

/////////////////////////////////////////////////////////////////////////////
// CSpectrumAnalyzerDemoDlg dialog

CSpectrumAnalyzerDemoDlg::CSpectrumAnalyzerDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSpectrumAnalyzerDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSpectrumAnalyzerDemoDlg)
		m_iLevelEdit = 80;
		m_iStartFreq = 100000000;
		m_iStep = 10000;
		m_iStopFreq = 150000000;
		m_bScanIfContinous = FALSE;
	m_iSetFreq = 0;
	m_iCurFreq = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSpectrumAnalyzerDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSpectrumAnalyzerDemoDlg)
	DDX_Control(pDX, IDC_LEVEL_SPIN, m_LevelSpin_Ctrl);
	DDX_Text(pDX, IDC_LEVEL_EDIT, m_iLevelEdit);
	DDX_Text(pDX, IDC_START_EDIT, m_iStartFreq);
	DDX_Text(pDX, IDC_STEP_EDIT, m_iStep);
	DDX_Text(pDX, IDC_STOP_EDIT, m_iStopFreq);
	DDX_Check(pDX, IDC_CONTINOUSSCAN_CHK, m_bScanIfContinous);
	DDX_Text(pDX, IDC_SETFREQ_EDIT, m_iSetFreq);
	DDX_Text(pDX, IDC_CURFREQ_EDIT, m_iCurFreq);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSpectrumAnalyzerDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CSpectrumAnalyzerDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_NOTIFY(UDN_DELTAPOS, IDC_LEVEL_SPIN, OnDeltaposLevelSpin)
	ON_BN_CLICKED(IDC_SCAN_BTN, OnScanBtn)
	ON_BN_CLICKED(IDC_ZOOMOUT_BTN, OnZoomoutBtn)
	ON_BN_CLICKED(IDC_ZOOMIN_BTN, OnZoominBtn)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_USER_SCAN_NEW_POINT, OnScanNewPoint)
	ON_MESSAGE(WM_USER_SET_TO_FREQ, OnSetToFreq)
	ON_MESSAGE(WM_USER_SHOW_CURRENT_FREQ, OnShowCurrentFreq)
	ON_MESSAGE(WM_USER_SCAN_FINISHED, OnFinishScan)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSpectrumAnalyzerDemoDlg message handlers

BOOL CSpectrumAnalyzerDemoDlg::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_LevelSpin_Ctrl.SetRange(0, 120);
	m_bIsScanning=FALSE;
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

UINT TestSweepThread(LPVOID pParam)
{
    // The thread that simulates scanning the frequency; After getting signal strength, it sends the data (freq, signal strength) 
	// to the dialog (parent) via the message WM_USER_SCAN_NEW_POINT
	// after finishing, it sends the message WM_USER_SCAN_FINISHED to parent to enable controls

	// Extracting needed data from pParam
	// Note No. 13830203-8
	TestSweepThreadParams* pTParam = (TestSweepThreadParams*) pParam;
    
	BOOL* bpCont = pTParam->m_bpDoContinue;
	HWND hWnd = pTParam->m_hWnd;
    DWORD iStartFreq = pTParam->m_iScanStartFreq;
	DWORD iEndFreq = pTParam->m_iScanEndFreq;
	DWORD iStepFreq= pTParam->m_iScanStepFreq;
	
	BOOL *bIfContinous=pTParam->m_bpIfContinous;
	
	CPoint *ptNew= pTParam->m_pNewPoint;

	delete pTParam;  // this pointer was created in StartBtn

	DWORD iFreq;
	DWORD iSMeter=0;
	int iWaitingTime = 100;
	
	do
	{
		::PostMessage (hWnd, WM_USER_SCAN_NEW_POINT, 0, 1); // to clear the list, especially usefull if Continous scanning is enabled!

		for (iFreq=iStartFreq; iFreq<=iEndFreq; iFreq+=iStepFreq)
		{
			
			::Sleep(iWaitingTime);
			
			
			iSMeter++;
			
			ptNew->x=iFreq;
			if (iSMeter>100)
				iSMeter=1;
			ptNew->y=iSMeter;
						
			if (!(*bpCont))  // user has clicked "stop" button
				break;
			::PostMessage (hWnd, WM_USER_SCAN_NEW_POINT, WPARAM (ptNew), 0);
		}
		if (!(*bpCont))  // user has clicked "stop" button
			break;
	}
	while (*bIfContinous); // if Continous Scan is enabled, go on scanning.

	::PostMessage (hWnd, WM_USER_SCAN_FINISHED, 0, 0);
	return 0;
}

void CSpectrumAnalyzerDemoDlg::OnDeltaposLevelSpin(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
	// TODO: Add your control notification handler code here


	UpdateData();
	m_iLevelEdit += pNMUpDown->iDelta;
	if (m_iLevelEdit > 100)
		m_iLevelEdit = 100;
	if (m_iLevelEdit < 0)
		m_iLevelEdit = 1;
	
	UpdateData(FALSE);

	if (m_SpectrumAnalyzer_Ctrl.HasValidWin())
	{
		m_SpectrumAnalyzer_Ctrl.SetVHighIndicator(m_iLevelEdit);
		m_SpectrumAnalyzer_Ctrl.Clear();
		m_SpectrumAnalyzer_Ctrl.UpdateLabels();
		m_SpectrumAnalyzer_Ctrl.DrawLine();
	}

	
	*pResult = 0;
}

void CSpectrumAnalyzerDemoDlg::OnScanBtn() 
{
	
	if (!m_bIsScanning)
	{
				
		UpdateData();

		m_bDoContinueScan=TRUE;

		// Create SpectrumAnalyzer Control
		CPoint ptRangeX(m_iStartFreq, m_iStopFreq);
		CRect rect; 
		//Get the rectangular coordinates of the rectangle we already placed on dialog
		GetDlgItem(IDC_SPECTRUM_STATIC)->GetWindowRect(rect);
		
		//Convert to the client coordinates
		ScreenToClient(rect);
		
		//Create and place the spectrum control on screen
		if (m_SpectrumAnalyzer_Ctrl)
			m_SpectrumAnalyzer_Ctrl.Destroy();
		
		m_SpectrumAnalyzer_Ctrl.Create(rect, this, IDC_SPECTRUM_STATIC, ptRangeX, CPoint(0, 120));
		m_SpectrumAnalyzer_Ctrl.SetVHighIndicator(m_iLevelEdit);
		// Note No. 13830203-5
		m_SpectrumAnalyzer_Ctrl.SetCallbackSetToFreq(m_hWnd, WM_USER_SET_TO_FREQ);
		m_SpectrumAnalyzer_Ctrl.SetCallbackShowFreq(m_hWnd, WM_USER_SHOW_CURRENT_FREQ);
		
		// Filling ThreadParameters
		// Note No. 13830203-8
		TestSweepThreadParams* pThreadParam = new TestSweepThreadParams;
		
		pThreadParam->m_bpDoContinue=&m_bDoContinueScan;
		pThreadParam->m_hWnd = m_hWnd;
		pThreadParam->m_iScanStartFreq = m_iStartFreq;
		pThreadParam->m_iScanEndFreq = m_iStopFreq;
		pThreadParam->m_iScanStepFreq=m_iStep;
		
		pThreadParam->m_pNewPoint=&m_ptNewPoint;
		pThreadParam->m_bpIfIsScanning=&m_bIsScanning;
		pThreadParam->m_bpIfContinous=&m_bScanIfContinous;
		
		m_bScanIfContinous =((CButton *) GetDlgItem(IDC_CONTINOUSSCAN_CHK))->GetCheck();
		
		AfxBeginThread (TestSweepThread, pThreadParam);
		m_bIsScanning=TRUE;
		GetDlgItem(IDC_SCAN_BTN)->SetWindowText(_T("Stop!"));
	}
	else
	{
		m_bDoContinueScan=FALSE;
		// Do nothing else, because the Scanning thread itself calls 
		// OnFinishScan(0,0);
	}
	
}

LRESULT CSpectrumAnalyzerDemoDlg::OnScanNewPoint(WPARAM wParam, LPARAM lParam)
{
	// Message handler for WM_USER_SCAN_NEW_POINT, it adds the point to the list and draws it
	
	if (lParam == 1)
	{   // it is only used for clearing the list!
		m_SpectrumAnalyzer_Ctrl.ClearList();
		m_SpectrumAnalyzer_Ctrl.Clear();
		m_SpectrumAnalyzer_Ctrl.UpdateLabels();
		return 1;
	}

	m_SpectrumAnalyzer_Ctrl.AddPoint(m_ptNewPoint);
	m_SpectrumAnalyzer_Ctrl.DrawLine();
	
	
	m_bScanIfContinous =((CButton *) GetDlgItem(IDC_CONTINOUSSCAN_CHK))->GetCheck();
	
	return 1;

}

LRESULT CSpectrumAnalyzerDemoDlg::OnSetToFreq(WPARAM wParam, LPARAM lParam)
{
	// message handler for message WM_USER_SET_TO_FREQ
	// when user clicks a frequency in CSpectrumAnalyzer
	// then CSpectrumAnalyzer sends this message back to parent (via Callback)
		
	if (m_bIsScanning)
		return 0;

	CPoint *ptDestFreq=(CPoint*) wParam;
	
	m_iSetFreq = ptDestFreq->x;
	
	delete ptDestFreq;  //this pointer was created in CSpectrumAnalyzerCtrl::OnLButtonDown
	UpdateData(FALSE);

	return 1;
		
}

LRESULT CSpectrumAnalyzerDemoDlg::OnShowCurrentFreq(WPARAM wParam, LPARAM lParam)
{
	// message handler for the message WM_USER_SHOW_CURRENT_FREQ
	// this message is sent by CSpectrumAnalyzer when the user moves the mouse 
	// so, freq. must be updated

	if (m_bIsScanning) // do not show freq of mouse while scanning
		return 0;
	
	CPoint *ptCurPoint= (CPoint*) wParam;

	
	m_iCurFreq = ptCurPoint->x;
	
	delete ptCurPoint; // this CPoint* was created in CSpectrumAnalyzer::OnMouseMove
	UpdateData(FALSE);

	return 1;
	
}

LRESULT CSpectrumAnalyzerDemoDlg::OnFinishScan(WPARAM wParam, LPARAM lParam)
{
	// message handler for the message WM_USER_SCAN_FINISHED
	// the message is sent by thread indicating thread has finished

	m_bDoContinueScan=FALSE;
	GetDlgItem(IDC_SCAN_BTN)->SetWindowText(_T("Start"));
	
	m_bIsScanning=FALSE;	

	return 1;	
}

void CSpectrumAnalyzerDemoDlg::OnZoomoutBtn() 
{
	if (m_SpectrumAnalyzer_Ctrl.HasValidWin())
		m_SpectrumAnalyzer_Ctrl.ReSetStep(TRUE);	
}


void CSpectrumAnalyzerDemoDlg::OnZoominBtn() 
{
	if (m_SpectrumAnalyzer_Ctrl.HasValidWin())
        m_SpectrumAnalyzer_Ctrl.ReSetStep(FALSE);	
}

⌨️ 快捷键说明

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