MFC学习碎片



(1)MFC程序中弹出框:        

	 MessageBox(str,_T("程序执行结果"),MB_OK);

         AfxMessageBox("Hello");

(2)MFC获取Edit Control的值:

         CEdit *edit1, *edit2;     //注意获取两个控件的值时,要分别加上*
         edit1 = (CEdit*)GetDlgItem(IDC_EDIT_USERNAME);
         edit2 = (CEdit*)GetDlgItem(IDC_EDIT_PWD);

         //获取Edit Control的值:

         edit1->GetWindowText(m_csName);
         edit2->GetWindowText(m_csPwd);
         m_csName.ReleaseBuffer();
         m_csPwd.ReleaseBuffer();

         //设置Edit Control的值:

         edit1->SetWindowText("Hello!");

(3)Socket传递结构体

//定义消息的宏。主要定义结构体类型
#define MSG_TYPE_LOGIN  1
#define MSG_TYPE_LOGIN_RST 2

struct StrMsg
{
    int m_nMsgType;             //用来标识结构体类型
    union                                //不同结构体定义在一个联合中
    { 
        struct Strlogin m_strLogin;
        struct StrloginRst m_strLoginRst;
    }
};

struct StrLogin
{
    char m_szUID[20];
    char m_szPWD[20];
};

struct StrLoginRst
{
    int m_nLoginRst;
};

//发送结构体
CString csName = "HELLO";
CString csPwd = "123456";

StrMag msgToSend;
msgToSend.m_nMagType = MAG_TYPE_LOGIN;      //设置消息类型
memset(msgToSend.m_strLogin.m_szUID, '', 20);    //初始化字符数组
strcpy(msgToSend.m_strLogin.m_szUID, csName);     //给字符数组赋值
memset(msgToSend.m_strLogin.m_szPWD, '', 20);
strcpy(msgToSend.m_strLogin.m_szPWD, csPwd);

pSocket->Send(&msgToSend, sizeof(StrMsg));    //套接字发送结构体


//接收结构体
StrMsg* pMsg = (StrMsg*)pBuf;     //这里用到强制类型转换
switch(pMsg->m_nMsgType)
{
     case MSG_TYPE_LOGIN:
    {
        StrLogin login = pMsg->m_strLogin;              //发送过来的结构体在这里就能够使用了

         /*********在这里能够加入对接受到结构体StrLogin的处理程序************/
        StrMsg msgResult;                                        //收到消息之后能够回复消息给对方
        msgResult.m_nMsgType = MSG_TYPE_LOGINRST;
        msgResult.m_strLoginRst.m_nLoginRst = 0;

        pChatSocket->Send(&msgResult, sizeof(StrMsg));               //回复消息
    }
    break;
case MSG_TYPE_REG:
    break;
}

【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/zhchoutai/p/8465841.html