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

📄 myapp1dlg.cpp

📁 上位机软件 for 自制AT89C51/52编程器
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// MYAPP1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "MYAPP1.h"
#include "MYAPP1Dlg.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 };
  CString   m_aboutedit;
  //}}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)
  m_aboutedit = _T("波特率:9600\r\n数据位:8\r\n校  验:无\r\n停止位:1\r\n数据流控制:Xon/Xoff\r\n");
  //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CAboutDlg)
  DDX_Text(pDX, IDC_EDIT1, m_aboutedit);
  //}}AFX_DATA_MAP
}

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

/////////////////////////////////////////////////////////////////////////////
// CMYAPP1Dlg dialog
void CMYAPP1Dlg::DisplayMessage(CString s,char enter=1) 
{
  UpdateData(TRUE);
  int i;
  while(  (i=s.Find("\n\r"))!=-1)
  {
    s.SetAt(i,'\r');
    s.SetAt(i+1,'\n');
  }

  char ln[10];
  if(enter)
  {
    int len=m_edit1.GetLength();
    if(len>2 && m_edit1.GetAt(len-1)!='\n')m_edit1+="\r\n";
    sprintf(ln,"%04d: ",m_edit1line++);
    m_edit1+=ln+s+"\r\n";
  }
  else m_edit1+=s;
  UpdateData(FALSE);

    CEdit *pDC; 
    pDC=(CEdit *)GetDlgItem(IDC_EDIT1);
    int len=pDC->GetLineCount();
  pDC->LineScroll(len); 
/*
  UpdateData(TRUE);
  sprintf(ln,"%d: ",len);
  m_edit2=ln;
  UpdateData(FALSE);
*/

}
void CMYAPP1Dlg::DisplayMessage(DWORD d)
{
    char s[10];
  if(d==XON) strcpy(s,"XON");
  else if (d==XOFF)strcpy(s,"XOFF");
  else sprintf(s,"%u",d);
  DisplayMessage(s,0);
}

void CMYAPP1Dlg::OnCommRecv(WPARAM wParam, LPARAM lParam)
{
  ISONCOMRECV=TRUE;
}

CMYAPP1Dlg::CMYAPP1Dlg(CWnd* pParent /*=NULL*/)
  : CDialog(CMYAPP1Dlg::IDD, pParent)
{
  //{{AFX_DATA_INIT(CMYAPP1Dlg)
  m_edit1 = _T("");
  m_edit1line=0;
  m_check1 = FALSE;
  m_check2 = FALSE;
  m_check3 = FALSE;
  m_radio1 = -1;
  m_edit2 = _T("");
  m_edit3 = _T("");
  //}}AFX_DATA_INIT
  // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMYAPP1Dlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CMYAPP1Dlg)
  DDX_Control(pDX, IDC_PROGRESS1, m_nProgress);
  DDX_Control(pDX, IDC_OPENS, m_opens);
  DDX_Text(pDX, IDC_EDIT1, m_edit1);
  DDX_Check(pDX, IDC_CHECK1, m_check1);
  DDX_Check(pDX, IDC_CHECK2, m_check2);
  DDX_Check(pDX, IDC_CHECK3, m_check3);
  DDX_Radio(pDX, IDC_RADIO1, m_radio1);
  DDX_Text(pDX, IDC_EDIT2, m_edit2);
  DDX_Text(pDX, IDC_EDIT3, m_edit3);
  //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMYAPP1Dlg, CDialog)
  //{{AFX_MSG_MAP(CMYAPP1Dlg)
  ON_WM_SYSCOMMAND()
  ON_WM_PAINT()
  ON_WM_QUERYDRAGICON()
  ON_BN_CLICKED(IDC_OPENS, OnOpens)
  ON_BN_CLICKED(IDC_CHECK3, OnCheck3)
  ON_NOTIFY(NM_CLICK, IDC_LIST3, OnClickList3)
  ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  ON_MESSAGE(ON_COM_RECEIVE,OnCommRecv)
  ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
  ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
  ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
  ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
  ON_BN_CLICKED(IDC_RADIO4, OnRadio4)
  ON_BN_CLICKED(IDC_RADIO5, OnRadio5)
  ON_WM_TIMER()
  ON_EN_CHANGE(IDC_EDIT3, OnChangeEdit3)
  ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2)
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMYAPP1Dlg message handlers

BOOL CMYAPP1Dlg::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_nProgress.SetPos(0);
  DisplayMessage("MYEZ编程器");

/*
LONG RegOpenKeyEx(
  HKEY hKey,         // handle to open key
  LPCTSTR lpSubKey,  // address of name of subkey to open
  DWORD ulOptions,   // reserved
  REGSAM samDesired, // security access mask
  PHKEY phkResult    // address of handle to open key
);

LONG RegEnumValue(
  HKEY hKey,              // handle to key to query
  DWORD dwIndex,          // index of value to query
  LPTSTR lpValueName,     // address of buffer for value string
  LPDWORD lpcbValueName,  // address for size of value buffer
  LPDWORD lpReserved,     // reserved
  LPDWORD lpType,         // address of buffer for type code
  LPBYTE lpData,          // address of buffer for value data
  LPDWORD lpcbData        // address for size of data buffer
);
LONG RegQueryValueEx(
  HKEY hKey,           // handle to key to query
  LPTSTR lpValueName,  // address of name of value to query
  LPDWORD lpReserved,  // reserved
  LPDWORD lpType,      // address of buffer for value type
  LPBYTE lpData,       // address of data buffer
  LPDWORD lpcbData     // address of data buffer size
);
  
*/
  #define MAXKEYPATH  99
  HKEY    hKey    = HKEY_LOCAL_MACHINE;
  LPCTSTR     lpSubKey  = "HARDWARE\\DEVICEMAP\\SERIALCOMM";
  REGSAM    samDesired  = KEY_READ;

  DWORD   dwIndex = 0;
  LPDWORD   lpType  =new DWORD;   *lpType= REG_SZ;
  LPBYTE  COMName =new BYTE[80];
  LPDWORD lpcbData=new DWORD; *lpcbData=80;
    
  char   COMObject[MAXKEYPATH];   
  LPSTR   lpszDescBuff   =   NULL;   
  DWORD   rc;   
  DWORD   dwBufSize=MAXKEYPATH; 
  CListCtrl* p1;
  rc   =   RegOpenKeyEx(hKey,lpSubKey,0,samDesired,&hKey); 
  
  if   (rc   ==   ERROR_SUCCESS)   
  {   
    p1= (CListCtrl*)GetDlgItem(IDC_LIST3);
    //p1->SetBkColor(RGB(255,255,255));
    //p1->SetTextBkColor(RGB(255,255,255));
    while   (RegEnumValue(hKey,dwIndex++,COMObject,&dwBufSize,NULL,lpType,COMName,lpcbData)==ERROR_SUCCESS)   
    {   
      CString ts=COMName;
      p1->InsertItem(dwIndex,ts,dwIndex);
      if(dwIndex==1)SetDlgItemText(IDC_STATIC_LIST3, ts);
      dwBufSize   =   MAXKEYPATH;
      ts="发现:";
      //ts+=COMObject;
      //ts+="\\";
      ts+=COMName;
      DisplayMessage(ts);
    }   
    RegCloseKey(hKey);   
  }   
  else   
  {
    DisplayMessage("未能检测串口!");
  }
  if(dwIndex==0)m_opens.EnableWindow(FALSE);
  UpdateData(TRUE);
  m_edit3="C:\\Documents and Settings\\All Users\\桌面\\default.bin";
  m_edit2="C:\\Documents and Settings\\All Users\\桌面\\default.bin";
  UpdateData(FALSE);
  return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CMYAPP1Dlg::OnOpens() 
{
  // TODO: Add your control notification handler code here
  CString ss1,ss2;
  CButton*  ReadButton=(CButton*)GetDlgItem(IDC_BUTTON2);
  CButton* WriteButton=(CButton*)GetDlgItem(IDC_BUTTON4);
  CButton*  autocheck=(CButton*)GetDlgItem(IDC_CHECK3);
  UpdateData(TRUE);
  UpdateData(FALSE);
  m_opens.GetWindowText(ss1);
  GetDlgItemText(IDC_STATIC_LIST3,ss2);

  if (ss1=="打开串口")
  {
    if(com.Open(ss2))//打开串口并使用默认设置
    {
      LPCOMMTIMEOUTS co1=com.GetTimeouts();
      co1->ReadIntervalTimeout=MAXDWORD;
      co1->ReadTotalTimeoutMultiplier=MAXDWORD;
      co1->ReadTotalTimeoutConstant=10000;
      com.SetTimeouts(co1);
      ISONCOMRECV=FALSE;
      DisplayMessage("打开"+ss2+"成功!");
        m_opens.SetWindowText("关闭串口");
      CListCtrl* pList = (CListCtrl*) GetDlgItem(IDC_LIST3);
      pList->EnableWindow(FALSE);
      if(0<=m_radio1 && m_radio1<5)
      {
        ReadButton->EnableWindow();
        WriteButton->EnableWindow();
      }
      autocheck->EnableWindow();
      //UpdateData(TRUE);UpdateData(FALSE);
      if(m_check3==TRUE)OnCheck3();
      com.SetWnd(AfxGetMainWnd()->m_hWnd); //设置消息处理窗口
    }
    else 
    {
      DisplayMessage("打开"+ss2+"失败!串口已被占用。");
    }
  }
  else
  {
    com.Close();
    DisplayMessage("关闭"+ss2+"成功!");
      m_opens.SetWindowText("打开串口");
      CListCtrl* pList = (CListCtrl*) GetDlgItem(IDC_LIST3);
      pList->EnableWindow();
    ReadButton->EnableWindow(FALSE);
    WriteButton->EnableWindow(FALSE);
    autocheck->EnableWindow(FALSE);
  }

  
}
//检测 
void CMYAPP1Dlg::OnTimer(UINT nIDEvent) 
{
  // TODO: Add your message handler code here and/or call default
  WAIT_OVER=TRUE;
  if(com.IsOpen())
  {

⌨️ 快捷键说明

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