注册 登录
编程论坛 VC++/MFC

静态文本框运行时输入数据老是自动清空

chen3bing 发布于 2023-01-10 00:01, 1013 次点击
我贴出有关所有代码,大神看看,谢谢!

// DTU测试软件Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "DTU测试软件.h"
#include "DTU测试软件Dlg.h"
#include "Message.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()

/////////////////////////////////////////////////////////////////////////////
// CDTUDlg dialog

CDTUDlg::CDTUDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CDTUDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CDTUDlg)
    m_EditRec = _T("");
    m_EditSend = _T("");
    m_EditLoIP = _T("");
    m_EditReIP = _T("");
    m_EditRePort = 0;
    m_EditLoPort = 0;
    m_EditByteCnt = 0;
    m_EditFrCnt = 0;
    m_EditFrLen = 0;
    m_EditHearFrCnt = 0;
    m_EditInErrCnt = 0;
    m_EditLastData = _T("");
    m_EditOtherFrCnt = 0;
    m_EditThisData = _T("");
    m_EditWorkFrCnt = 0;
    m_EditOutErrCnt = 0;
    m_EditRepeatFrCnt = 0;
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDTUDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CDTUDlg)
    DDX_Control(pDX, IDC_EditRec, m_control);
    DDX_Text(pDX, IDC_EditRec, m_EditRec);
    DDX_Text(pDX, IDC_EditSend, m_EditSend);
    DDX_Text(pDX, IDC_EditLoIP, m_EditLoIP);
    DDX_Text(pDX, IDC_EditReIP, m_EditReIP);
    DDX_Text(pDX, IDC_EditRePort, m_EditRePort);
    DDX_Text(pDX, IDC_EditLoPort, m_EditLoPort);
    DDX_Text(pDX, IDC_EditByteCnt, m_EditByteCnt);
    DDX_Text(pDX, IDC_EditFrCnt, m_EditFrCnt);
    DDX_Text(pDX, IDC_EditFrLen, m_EditFrLen);
    DDX_Text(pDX, IDC_EditHeartFrCnt, m_EditHearFrCnt);
    DDX_Text(pDX, IDC_EditInErrCnt, m_EditInErrCnt);
    DDX_Text(pDX, IDC_EditLastData, m_EditLastData);
    DDX_Text(pDX, IDC_EditOhterFrCnt, m_EditOtherFrCnt);
    DDX_Text(pDX, IDC_EditThisData, m_EditThisData);
    DDX_Text(pDX, IDC_EditWorkFrCnt, m_EditWorkFrCnt);
    DDX_Text(pDX, IDC_EditOutErrCnt, m_EditOutErrCnt);
    DDX_Text(pDX, IDC_EditRepeatFrCnt, m_EditRepeatFrCnt);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDTUDlg, CDialog)
    //{{AFX_MSG_MAP(CDTUDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_ButtonLink, OnButtonLink)
    ON_BN_CLICKED(IDC_ButtonSend, OnButtonSend)
    ON_BN_CLICKED(IDC_ButtonUnlink, OnButtonUnlink)
    ON_BN_CLICKED(IDC_ButtonRClr, OnButtonRClr)
    ON_BN_CLICKED(IDC_ButtonSClr, OnButtonSClr)
    ON_BN_CLICKED(IDC_ButtonMsgClr, OnButtonMsgClr)
    ON_BN_CLICKED(IDC_ButtonWClr, OnButtonWClr)
    //}}AFX_MSG_MAP
    ON_MESSAGE(WM_RECDATA, OnReadData)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDTUDlg message handlers

BOOL CDTUDlg::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
    char name[50];
    int err;

    err = gethostname(name, sizeof(name));
    if (err == SOCKET_ERROR)
    {
        m_EditRec = "Get host name is failure!";
    }
    struct hostent *hn;
    hn = gethostbyname(name);
    m_EditLoIP = inet_ntoa(*(struct in_addr *)hn->h_addr_list[0]);

    m_EditLoPort = 50000;
    m_EditReIP = "222.18.168.253";
    m_EditRePort = 40000;

    m_EditByteCnt = 0;
    m_EditFrCnt = 0;
    m_EditFrLen = 0;
    m_EditHearFrCnt = 0;
    m_EditInErrCnt = 0;
    m_EditOutErrCnt = 0;
    m_EditOtherFrCnt = 0;
    m_EditWorkFrCnt = 0;
    m_EditRepeatFrCnt = 0;
    m_EditThisData = "";
    m_EditLastData = "";
    m_EditRec = "";
    m_EditSend = "";
    CheckRadioButton(IDC_RadioAck0, IDC_RadioAck1, IDC_RadioAck0);
    b_AckType = FALSE;
    UpdateData(FALSE);

    return TRUE;  // return TRUE  unless you set the focus to a control
}

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

/**
** Initialization UDP
**/
void CDTUDlg::OnButtonLink()
{
    // TODO: Add your control notification handler code here
    WSADATA wsadata;
    WORD wVersionRequested;
    int err;
    char buffer[60], acktype[15];

    wVersionRequested = MAKEWORD(2, 2);
    err = WSAStartup( wVersionRequested, &wsadata);
    if (err != 0)
    {
        m_EditRec = "we could not find a usable";
        UpdateData(FALSE);
        return ;
    }
    if (LOBYTE(wsadata.wVersion) != 2
        || HIBYTE(wsadata.wVersion) != 2)
    {
        WSACleanup();
        m_EditRec = "we could not find a usable";
        UpdateData(FALSE);
        return ;
    }
    UpdateData(TRUE);
    /* 本地IP地址及其端口号 */
    s_LocalAddr.sin_family = AF_INET;
    s_LocalAddr.sin_addr.S_un.S_addr = inet_addr(m_EditLoIP);
    s_LocalAddr.sin_port = htons(m_EditLoPort);
    /* 远程IP地址及其端口号 */
    s_RemoteAddr.sin_family = AF_INET;
    s_RemoteAddr.sin_addr.S_un.S_addr = inet_addr(m_EditReIP);
    s_RemoteAddr.sin_port = htons(m_EditRePort);
   
    UDPSocket = socket(AF_INET, SOCK_DGRAM, 0);
    if (UDPSocket == INVALID_SOCKET)
    {
        WSACleanup();
        m_EditRec = "we could not socket";
        UpdateData(FALSE);
        return ;
    }

    err = bind(UDPSocket, (sockaddr *)&s_LocalAddr, sizeof(s_LocalAddr));
    if (err == SOCKET_ERROR)
    {
        err = closesocket(UDPSocket);
        WSACleanup();
        m_EditRec = "bind is failure!";
        UpdateData(FALSE);
        return ;
    }

    err = WSAAsyncSelect(UDPSocket, m_hWnd, WM_RECDATA, FD_READ);
    if (err == SOCKET_ERROR)
    {
        err = closesocket(UDPSocket);
        WSACleanup();
        m_EditRec = "set WSAAsyncSelect is failure!";
        UpdateData(FALSE);
        return ;
    }

    b_firstflg = FALSE;
    err = GetCheckedRadioButton(IDC_RadioAck0, IDC_RadioAck1);
    if (err == IDC_RadioAck0)
    {
        b_AckType = FALSE;
        strcpy(acktype, "DTU Device");
    }   
    else
    {
        b_AckType = TRUE;
        strcpy(acktype, "Server");
    }
    sprintf(
            buffer,
            "%s link is successful!\r\nLocal Port is %d\r\nRemote Port is %d.",
            acktype,
            m_EditLoPort,
            m_EditRePort
            );
    m_EditRec = buffer;
    UpdateData(FALSE);
}

/**
** Send data to internet by UDP
**/
LRESULT CDTUDlg::SendTo(void)
{
    int err;

    err = sendto(   
                UDPSocket,
                SendMsg.msg,
                SendMsg.len,
                0,
                (const sockaddr *)&s_RemoteAddr,
                sizeof(s_RemoteAddr)
                );
    if (err == SOCKET_ERROR)
    {
        m_EditRec = m_EditRec + "Send data is failure!";
        UpdateData(FALSE);
        return err;
    }
    return err;
}

/**
** Click "发送" button to send data by UDP
**/
void CDTUDlg::OnButtonSend()
{
    // TODO: Add your control notification handler code here
    int err;

    UpdateData(TRUE);
    SendMsg.len = m_EditSend.GetLength();
    if (SendMsg.len == 0)
    {
        m_EditRec = "Don't send empty string!";
        UpdateData(FALSE);
        return ;
    }
    strcpy(SendMsg.msg, m_EditSend);
    err = SendTo();

    return ;
}

/**
** To accept receive data response
**/
LRESULT CDTUDlg::RecAck(void)
{
    int err;
    SendMsg.msg[0] = 0x00;
    SendMsg.msg[1] = 0x0A;
    SendMsg.msg[2] = RecMsg.msg[2];
    SendMsg.msg[3] = RecMsg.msg[3];
    SendMsg.msg[4] = RecMsg.msg[4];
    SendMsg.msg[5] = RecMsg.msg[5];
    SendMsg.msg[6] = RecMsg.msg[6];
    SendMsg.msg[7] = RecMsg.msg[7];
    SendMsg.msg[8] = RecMsg.msg[8];
    if (b_AckType == FALSE)
        SendMsg.msg[9] = 0x51;
    else
        SendMsg.msg[9] = 0x50;
    SendMsg.len = 10;
    err = SendTo();
    return err;
}

/**
** Convert hex characters
**/
CString CDTUDlg::Hex(char ch)
{
    CString result;
    char buf[5] = {'\0'};

    if ((unsigned char)ch < 16)
        sprintf(buf, "0%x ", (unsigned char)ch);
    else
        sprintf(buf, "%2x ", (unsigned char)ch);
    result = buf;

    return result;
}

/**
** Check whether the data received correctly
**/
LRESULT CDTUDlg::RecDataCheck(void)
{
    unsigned char temp;
    static unsigned lastchar;
    int i;

    temp = (unsigned char)RecMsg.msg[10];
    for (i = 11; i < (RecMsg.len - 1); i++)
    {
        if (temp == 0xFF)
            temp = 0x00;
        else
            temp++;
        if (temp != (unsigned char)RecMsg.msg[i])
        {
            m_EditInErrCnt++;
            m_EditThisData = Hex(RecMsg.msg[i]);
            m_EditLastData = Hex((char)temp);
        }
        temp = (unsigned char)RecMsg.msg[i];
    }

    if (b_firstflg == FALSE)
        b_firstflg = TRUE;
    else
    {
        if (lastchar != (unsigned char)RecMsg.msg[10])
        {
            m_EditOutErrCnt++;
            m_EditThisData = Hex(RecMsg.msg[10]);
            m_EditLastData = Hex((char)lastchar);
        }
    }
    lastchar = (unsigned char)RecMsg.msg[RecMsg.len - 1];
    if (lastchar == 0xFF)
        lastchar = 0x00;
    else
        lastchar++;

    return 0;
}

/**
** Handle receive data
** 下面的函数是我们在做一个项目测试时用的,你可以不要
**/
LRESULT CDTUDlg::RecHandle(void)
{   
    int err;
    int i;

    m_EditRec = "";
    for (i = 0; i < RecMsg.len; i++)
    {
        //m_EditRec = m_EditRec + Hex(RecMsg.msg[i]);
        m_EditRec = m_EditRec + RecMsg.msg[i];
    }

    m_EditByteCnt += RecMsg.len;
    m_EditFrCnt++;
    m_EditFrLen = RecMsg.len;
    if (RecMsg.len >= 10)   
    {
        switch (RecMsg.msg[9])
        {
        case 0x5A :
            m_EditWorkFrCnt++;
            if (((unsigned char)RecMsg.msg[0] & 0x80) == 0x80)
                m_EditRepeatFrCnt++;
            err = RecAck();
            err = RecDataCheck();
            break;
        case 0x55 :
            m_EditHearFrCnt++;
            break;
        default :
            m_EditOtherFrCnt++;
            break;
        }
    }
    UpdateData(FALSE);
    return 0;
}

/**
** Receive data by UDP protocol, and the function is triggered by winsock event
**/
LRESULT CDTUDlg::OnReadData(WPARAM wParam, LPARAM lParam)
{
    int err = 0;
    int sockaddrlen = 0;

    sockaddrlen = sizeof(s_RemoteAddr);
    switch (WSAGETSELECTEVENT(lParam))                        /* get winsock event        */
    {
    case FD_READ :
        err = recvfrom(   
                        UDPSocket,                            /* receive UDP data            */                                         
                        RecMsg.msg,
                        sizeof(RecMsg.msg),
                        0,
                        (sockaddr *)&s_RemoteAddr,
                        (int *)&sockaddrlen
                        );
        if (err == SOCKET_ERROR)                            /* Is receive data right    */
        {
            m_EditRec = m_EditRec +
                        "receive data is failure!";
            UpdateData(FALSE);
            return 0;
        }
        RecMsg.len = err;
        err = RecHandle();
        break;
    default :
        break;
    }
    return 0;
}

BOOL CDTUDlg::DestroyWindow()
{
    // TODO: Add your specialized code here and/or call the base class
    int err;

    err = closesocket(UDPSocket);
    WSACleanup();

    return CDialog::DestroyWindow();
}

void CDTUDlg::OnButtonUnlink()
{
    // TODO: Add your control notification handler code here
    int err;
   
    err = closesocket(UDPSocket);
    WSACleanup();
    m_EditRec = "Unlink the DTU device!";
    UpdateData(FALSE);
    return ;
}

void CDTUDlg::OnButtonRClr()
{
    // TODO: Add your control notification handler code here
    m_EditRec = "";
    UpdateData(FALSE);
    return ;
}

void CDTUDlg::OnButtonSClr()
{
    // TODO: Add your control notification handler code here
    m_EditSend = "";
    UpdateData(FALSE);
    return ;
}

void CDTUDlg::OnButtonMsgClr()
{
    // TODO: Add your control notification handler code here
    m_EditByteCnt = 0;
    m_EditFrCnt = 0;
    m_EditFrLen = 0;
    UpdateData(FALSE);
    return ;
}

void CDTUDlg::OnButtonWClr()
{
    // TODO: Add your control notification handler code here
    m_EditHearFrCnt = 0;
    m_EditInErrCnt = 0;
    m_EditOutErrCnt = 0;
    m_EditOtherFrCnt = 0;
    m_EditWorkFrCnt = 0;
    m_EditRepeatFrCnt = 0;
    m_EditThisData = "";
    m_EditLastData = "";
    UpdateData(FALSE);
    return ;
}
那个静态文本框ID是IDC_EditSend,关联一个变量m_EditSend.
0 回复
1