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

📄 test3dlg.cpp

📁 可以找到串口,可以写MTK手机的IMEI
💻 CPP
字号:
// test3Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "test3.h"
#include "test3Dlg.h"
#include "Comm.h"
#include <process.h>

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
CComm     m_ComPort;
CBrush    m_brush; 
COLORREF  m_color;
COLORREF  m_clrBkgnd;
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()

/////////////////////////////////////////////////////////////////////////////
// CTest3Dlg dialog

CTest3Dlg::CTest3Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTest3Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTest3Dlg)
	m_baud = _T("");
	m_port = _T("");
	m_imei = _T("");
	m_reval = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_font.CreateFont(81,
					0,
					0,
					0,
					FW_BOLD,
					0,
					0,
					0,
					ANSI_CHARSET,
					OUT_DEFAULT_PRECIS,
					CLIP_CHARACTER_PRECIS,
					DEFAULT_QUALITY,
					DEFAULT_PITCH,
					"Arial"
					);
}

void CTest3Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTest3Dlg)
	DDX_Control(pDX, IDC_BUTTON1, m_btn);
	DDX_Control(pDX, IDC_COMBO1, m_por);
	DDX_Control(pDX, IDC_Baud, m_baud1);
	DDX_Control(pDX, IDC_BUTTONBIG, m_ShowBig);
	DDX_Control(pDX, IDC_CLOSEBTN, m_CloseBtn);
	DDX_CBString(pDX, IDC_Baud, m_baud);
	DDX_CBString(pDX, IDC_COMBO1, m_port);
	DDX_Text(pDX, IDC_IMEI, m_imei);
	DDX_Text(pDX, IDC_EDIT2, m_reval);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTest3Dlg, CDialog)
	//{{AFX_MSG_MAP(CTest3Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_SETCURSOR()
	ON_WM_LBUTTONDOWN()
	ON_BN_CLICKED(IDC_CLOSEBTN, OnClosebtn)
	ON_BN_CLICKED(IDC_BUTTONBIG, OnButtonbig)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTest3Dlg message handlers

BOOL CTest3Dlg::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
	
	//只有加了这一句后,才能将整个画面显示整个窗口,不至于仅仅在中心。
	ModifyStyle(WS_CAPTION, WS_MINIMIZEBOX, SWP_DRAWFRAME);	//设置图标

	m_bmpBackground.LoadBitmap(IDB_BITMAPBACKGROUND);
    //记住:Botton的属性值(Style)
	m_ShowBig.LoadBitmaps(IDB_BITMAPBIG,IDB_BITMAPBIG);
    m_CloseBtn.LoadBitmaps(IDB_BITMAPCLOSENORMAL,IDB_BITMAPON);

	SetTimer(1,2000,NULL);
	// TODO: Add extra initialization here
	m_baud1.AddString("115200");
	m_baud1.AddString("57600");
	m_baud1.SetCurSel(0);
	CString strCom;
	COMMCONFIG ci;
	DWORD dwSize=sizeof(COMMCONFIG);
	for(int i=1,p=-1;i<25;i++)
	{
		strCom.Format("COM%d", i);
		if(GetDefaultCommConfig(strCom,&ci,&dwSize))
		{
			p++;
			m_por.AddString(strCom.Mid(3));
		}
	}
	if (p>0){
		m_por.SetCurSel(p);}
	else {m_por.SetCurSel(0);
	}
	m_color=RGB(0,0,0);
	m_clrBkgnd = RGB( 255, 255, 0 );
	m_brush.CreateSolidBrush( m_clrBkgnd );
	return TRUE;  // return TRUE  unless you set the focus to a control
}
/*
   值       	 含义

SC_CLOSE 	    关闭按钮
SC_MAXIMIZE 	最大化按钮
SC_MINIMIZE 	最小化按钮
SC_RESTORE 	    恢复按钮*/

void CTest3Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		if(nID==SC_MINIMIZE)
		{
 
		}
		if(nID==SC_MAXIMIZE)
		{
			ShowWindow(SW_MAXIMIZE);
		//	MessageBox("OK");
		}

		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 CTest3Dlg::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();
		CPaintDC dc(this);				//对话框的dc
		CDC dcMem; 
		dcMem.CreateCompatibleDC(&dc);   //创建与对话框dc兼容的内存dc

		CRect rect;
		GetClientRect(&rect);

		BITMAP bitMap;
		m_bmpBackground.GetBitmap(&bitMap);

		CBitmap *pbmpOld=dcMem.SelectObject(&m_bmpBackground);		//将背景位图选入内存dc中
		dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bitMap.bmWidth,bitMap.bmHeight,SRCCOPY);   //将内存dc中的位图拉伸显示在对话框的dc中
	  

	 }
}

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

BOOL CTest3Dlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	if((pWnd->GetDlgCtrlID()==IDC_CLOSEBTN)||(pWnd->GetDlgCtrlID()==IDC_BUTTONBIG)||(pWnd->GetDlgCtrlID()==IDC_BUTTON1)||(pWnd->GetDlgCtrlID()==IDC_COMBO1)||(pWnd->GetDlgCtrlID()==IDC_Baud))
	{
    	SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR2));
		return true; //必须返回True否则无效
	}

    SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR1));
	return true;
	//return CDialog::OnSetCursor(pWnd, nHitTest, message);
}


//拖动整个窗体移动。
void CTest3Dlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x, point.y)); 	//向系统发送HTCAPTION消息,让系统以为鼠标点在标题栏上
	
	CDialog::OnLButtonDown(nFlags, point);
}

void CTest3Dlg::OnClosebtn() 
{
	// TODO: Add your control notification handler code here
	OnOK();
}

//最大化
void CTest3Dlg::OnButtonbig() 
{

	ShowWindow(SW_MINIMIZE);
	// TODO: Add your control notification handler code here
	
}

void CTest3Dlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
 
	// TODO: Add your message handler code here
	
}



HBRUSH CTest3Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	if(nCtlColor== CTLCOLOR_STATIC&&pWnd->GetDlgCtrlID()==IDC_COLOK){   
		pDC->SetBkColor(m_clrBkgnd);
//		pDC->SetBkMode(TRANSPARENT);
		pDC->SetTextColor(m_color);   
		return  m_brush;
	}
	// TODO: Return a different brush if the default is not desired
	return hbr;
}
DWORD WINAPI  ThreadProc(LPVOID  IpParam)
{

	((CTest3Dlg*) IpParam)->WriteIMEI();
	::_endthreadex(0);
	return  0;
}


void CTest3Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	HANDLE hThread;

    hThread=(HANDLE)::_beginthreadex(NULL,0,(unsigned int(_stdcall*)(void*))ThreadProc,LPVOID(this),0,NULL);

	CloseHandle(hThread);
}

void CTest3Dlg::WriteIMEI()
{
	m_btn.EnableWindow(false);//ShowWindow(SW_HIDE);
	char ch[32];
	CString strimei,strbaud,strport;
	GetDlgItem(IDC_IMEI)->GetWindowText(ch,32);
	strimei.Format("%s",ch);
	GetDlgItem(IDC_COLOK)->SetFont(&m_font); 
	if (strimei.GetLength()!=15){
		m_clrBkgnd=(RGB(200,200,0));
		SetDlgItemText(IDC_COLOK,"位数不对");
		SetDlgItemText(IDC_IMEI,"");
		m_btn.EnableWindow(true);
		return;
	}
	char cmd[128]="",cmdd[128]="";
	const char *IMEI,*Pfind;
	int i=0,baud=115200,port=1;
	CString strIMEI,imei;
	m_clrBkgnd=(RGB(255,255,0));
	m_color=(RGB(0,0,255));
	SetDlgItemText(IDC_COLOK,"正在写入");
	GetDlgItemText(IDC_Baud,strbaud);
	GetDlgItemText(IDC_COMBO1,strport);
	baud=atoi(strbaud);
	port=atoi(strport);
	m_ComPort.Open(port,baud);
	Sleep(100);
	if (!m_ComPort.Open(port,baud))
	{
		m_ComPort.Close();
		m_btn.EnableWindow(true);
		return;
	}
	Sleep(100);
	imei.Format("AT+EGMR=1,7,\"%s\"\n\r",strimei);
	IMEI=imei;
		for (i=0; i<10; i++)
		{
		m_ComPort.SendData("AT\n\r",4);
		Sleep(100);
		m_ComPort.ReadData(cmd,128);
		Pfind=strstr(cmd,"OK");
		if(Pfind!=NULL) break;
		Sleep(100);
		}
		if (strstr(cmd, "OK") == NULL){
			for (i=0; i<100; i++)
			{
				m_ComPort.SendData("AT\n\r",4);
				Sleep(15);
				m_ComPort.ReadData(cmd,128);
				Pfind=strstr(cmd,"OK");
				if(Pfind!=NULL) break;
				Sleep(15);
			}
			if (strstr(cmd, "OK") == NULL){
			m_clrBkgnd=(RGB(255,0,0));
			m_color=(RGB(0,0,0));
			SetDlgItemText(IDC_COLOK,"串口不通");
			SetDlgItemText(IDC_IMEI,"");
			SetDlgItemText(IDC_EDIT2,strimei);
			m_ComPort.Close();
			m_btn.EnableWindow(true);
			return;
		}
		}
		for (i=0; i<10; i++)
		{
			m_ComPort.SendData(IMEI,strlen(IMEI));
			Sleep(100);
			m_ComPort.ReadData(cmd,64);
//			token = strtok(cmd, "\"");
//			token = strtok(NULL, "\"");
//			strIMEI = token;
			if (strstr(cmd, "OK") != NULL) break;//&&(strIMEI==m_imei))
			Sleep(100);
		}
		if (strstr(cmd, "OK") != NULL){
		for (i=0; i<5; i++){
			m_ComPort.SendData("AT+EGMR=0,7\n\r",13);
			Sleep(50);
			m_ComPort.ReadData(cmdd,64);
			if ((strstr(cmdd, "OK") != NULL)&&(strstr(cmdd, strimei) != NULL)) break;
			}
		if (strstr(cmdd, strimei) != NULL){
			m_clrBkgnd=(RGB(0,255,0));
			m_color=(RGB(0,0,0));
			SetDlgItemText(IDC_COLOK,"写入成功");
			SetDlgItemText(IDC_EDIT2,strimei);
			m_ComPort.Close();
			SetDlgItemText(IDC_IMEI,"");
			m_btn.EnableWindow(true);
		return;
		}
		}

	m_clrBkgnd=(RGB(255,0,0));
	m_color=(RGB(0,0,0));
	SetDlgItemText(IDC_COLOK,"写入失败");
	SetDlgItemText(IDC_EDIT2,strimei);
	m_ComPort.Close();
	SetDlgItemText(IDC_IMEI,"");
	m_btn.EnableWindow(true);
}

void CTest3Dlg::OnButton2() 
{
	// TODO: Add your control notification handler code here

	CString strimei,strbaud,strport;
	char cmd[128]="",cmdd[128]="";
	const char *IMEI,*Pfind;
	int i=0,baud=115200,port=1;
	CString strIMEI,imei;
	m_clrBkgnd=(RGB(255,255,0));
	m_color=(RGB(0,0,255));
	SetDlgItemText(IDC_COLOK,"正在写入");
	GetDlgItemText(IDC_Baud,strbaud);
	GetDlgItemText(IDC_COMBO1,strport);
	baud=atoi(strbaud);
	port=atoi(strport);
	m_ComPort.Open(port,baud);
	Sleep(100);
	if (!m_ComPort.Open(port,baud))
	{
		m_ComPort.Close();
		m_btn.EnableWindow(true);
		return;
	}
	Sleep(100);
	imei.Format("ATD112;\n\r");
	IMEI=imei;
		for (i=0; i<10; i++)
		{
		m_ComPort.SendData("AT\n\r",4);
		Sleep(100);
		m_ComPort.ReadData(cmd,128);
		Pfind=strstr(cmd,"OK");
		if(Pfind!=NULL) break;
		Sleep(100);
		}
		if (strstr(cmd, "OK") == NULL){
			for (i=0; i<100; i++)
			{
				m_ComPort.SendData("AT\n\r",4);
				Sleep(15);
				m_ComPort.ReadData(cmd,128);
				Pfind=strstr(cmd,"OK");
				if(Pfind!=NULL) break;
				Sleep(15);
			}
			if (strstr(cmd, "OK") == NULL){
			m_clrBkgnd=(RGB(255,0,0));
			m_color=(RGB(0,0,0));
			SetDlgItemText(IDC_COLOK,"串口不通");
			SetDlgItemText(IDC_IMEI,"");
			SetDlgItemText(IDC_EDIT2,strimei);
			m_ComPort.Close();
			m_btn.EnableWindow(true);
			return;
		}
		}
		for (i=0; i<10; i++)
		{
			m_ComPort.SendData(IMEI,8);
			Sleep(100);
			m_ComPort.ReadData(cmd,64);
//			token = strtok(cmd, "\"");
//			token = strtok(NULL, "\"");
//			strIMEI = token;
			if (strstr(cmd, "OK") != NULL) break;//&&(strIMEI==m_imei))
			Sleep(100);
		}
		if (strstr(cmd, "OK") != NULL){
		
		if (strstr(cmdd, strimei) != NULL){
//			m_clrBkgnd=(RGB(0,255,0));
//			m_color=(RGB(0,0,0));
//			SetDlgItemText(IDC_COLOK,"写入成功");
			SetDlgItemText(IDC_EDIT2,strimei);
			m_ComPort.Close();
			SetDlgItemText(IDC_IMEI,"");
			m_btn.EnableWindow(true);
		return;
		}
		}

	m_clrBkgnd=(RGB(255,0,0));
	m_color=(RGB(0,0,0));
	SetDlgItemText(IDC_COLOK,"写入失败");
	SetDlgItemText(IDC_EDIT2,strimei);
	m_ComPort.Close();
	SetDlgItemText(IDC_IMEI,"");
	m_btn.EnableWindow(true);
}

⌨️ 快捷键说明

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