当前位置:网站首页>Chat software project development 2
Chat software project development 2
2022-07-26 20:31:00 【Domineering Xiao Ming】
Catalog
2.4 Button add fault tolerance
2.7 The dialog interface inserts a background image
2.1 Nickname settings
ultimate objective

Configure nickname principle
1. Start client , There should be an operation to read the configuration file , Read the nickname from the configuration file to the control
2. Customer configuration nickname , Need to write to the configuration file
In the nickname save control function
void CMFCChatClientDlg::OnBnClickedSavenameBtn()
{
// Get the contents of the text box
CString strName;
GetDlgItemText(IDC_NAME_EDIT, strName);
// Determine whether the nickname box is empty
if (strName.GetLength() <= 0) {
MessageBox(_T(" The nickname cannot be empty !"));
return;
}
//MB_OKCANCEL Indicates that the message box is OK/CANCEL key , User choice OK entering if sentence
if (IDOK == AfxMessageBox(_T(" Whether to modify the nickname "), MB_OKCANCEL)) {
// Save nicknames
// About the definition of path array , Built in 260 size
WCHAR strPath[MAX_PATH] = { 0 };
// Get the current path
GetCurrentDirectoryW(MAX_PATH, strPath);
//L"%s" It also represents wide bytes , If it is "%ls" It is found that all contents cannot be output
TRACE(L"#####strPath= %s", strPath);
CString strFilePath;
// Capitalization L Represents wide bytes ,.ini Represents a configuration file
strFilePath.Format(L"%ls//Test.ini", strPath);
// Write a string in the configuration file
WritePrivateProfileStringW(_T("CLIENT"), _T("NAME"), strName, strFilePath);
//WritePrivateProfileStringW(
// _In_opt_ LPCWSTR lpAppName, Item name
// _In_opt_ LPCWSTR lpKeyName, Key name
// _In_opt_ LPCWSTR lpString,
// _In_opt_ LPCWSTR lpFileName
//);
}
}In the initialization frame function OnInitDlg
// Get nickname from configuration file
WCHAR strPath[MAX_PATH] = { 0 };
GetCurrentDirectoryW(MAX_PATH, strPath);
CString strFilePath;
strFilePath.Format(L"%ls//Test.ini", strPath);
// Get a nickname
WCHAR wszName[MAX_PATH] = { 0 };
DWORD dwNum = GetPrivateProfileStringW(_T("CLIENT"), _T("NAME"), NULL, wszName, MAX_PATH, strFilePath);
if (dwNum > 0) {
// I read
// Displayed in the nickname box
GetDlgItem(IDC_NAME_EDIT)->SetWindowText(wszName);
}
else {
// Not read or the file is damaged
// First, write a casually in the file , Then display in the nickname box
WritePrivateProfileStringW(_T("CLIENT"), _T("NAME"), _T(" client "), strFilePath);
GetDlgItem(IDC_NAME_EDIT)->SetWindowText(_T(" client "));
}
//GetPrivateProfileStringW(
// _In_opt_ LPCWSTR lpAppName, Item name
// _In_opt_ LPCWSTR lpKeyName, Key name
// _In_opt_ LPCWSTR lpDefault, The default value is
// _Out_writes_to_opt_(nSize, return +1) LPWSTR lpReturnedString, One Buff, Get the content to buff in
// _In_ DWORD nSize,
// _In_opt_ LPCWSTR lpFileName
//);2.2 Automatic recovery
In the receiving function of the client , If the user chooses to send messages automatically , Then send the content in the message box to the server , Then display this message in the history message box

On the client side OnReceive Supplementary code in function
// Automatic recovery
// If checked,
if (((CButton*)dlg->GetDlgItem(IDC_AUTOSEND_RADIO))->GetCheck()) {
//1. Read the contents of the edit box
CString strAutoSendMsg;
dlg->GetDlgItemText(IDC_AUTOSEND_RADIO, strAutoSendMsg);
//2. Packet + Group format
CString strName;
dlg->GetDlgItemTextW(IDC_NAME_EDIT, strName);
// Time + nickname +[ Automatic recovery ]+ Content
CString strMsg = strName + _T(":") + _T("[ Automatic recovery ]") + strAutoSendMsg;
char* szSendBuf = T2A(strMsg);
// Send it to the server
dlg->m_client->Send(szSendBuf, SEND_MAX_BUF, 0);
// Displayed in the list box
CString strShow;
strShow = dlg->CatShowString(_T(""), strMsg);
dlg->m_list.AddString(strShow);
}Add the control function of automatic reply
// Automatic recovery
void CMFCChatClientDlg::OnBnClickedAutosendRadio()
{
if (((CButton*)GetDlgItem(IDC_AUTOSEND_RADIO))->GetCheck()) {
TRACE("#### Choose ");
((CButton*)GetDlgItem(IDC_AUTOSEND_RADIO))->SetCheck(FALSE);
}
else {
TRACE("##### For the selection ");
((CButton*)GetDlgItem(IDC_AUTOSEND_RADIO))->SetCheck(TRUE);
}
}2.3 Clear the screen

Only need to use under the corresponding control function ResetControl Function
2.4 Button add fault tolerance
When the client or server starts , Some buttons except the connection control should be grayed out , Prevent the program from crashing
GetDlgItem(IDC_SEND_BTN)->EnableWindow(FALSE);
// The disconnect button is grayed out
GetDlgItem(IDC_DISCONNECT_BTN)->EnableWindow(FALSE);
// Automatically reset the gray
GetDlgItem(IDC_AUTOSEND_CHECK)->EnableWindow(FALSE);
// The connect button is available
GetDlgItem(IDC_CONNECT_BTN)->EnableWindow(TRUE);2.5 disconnect

client
In the disconnect control function
void CMFCChatClientDlg::OnBnClickedDisconnectBtn()
{
//1. Control controls
GetDlgItem(IDC_SEND_BTN)->EnableWindow(FALSE);
// The disconnect button is grayed out
GetDlgItem(IDC_DISCONNECT_BTN)->EnableWindow(FALSE);
// Automatically reset the gray
GetDlgItem(IDC_AUTOSEND_CHECK)->EnableWindow(FALSE);
// The connect button is available
GetDlgItem(IDC_CONNECT_BTN)->EnableWindow(TRUE);
//2. Recycle resources
m_client->Close();
if (m_client != NULL) {
delete m_client;
m_client = NULL;
}
//3. Display to the list box
CString strShow;
strShow = CatShowString(_T(""), _T(" Disconnect from the server "));
m_list.AddString(strShow);
UpdateData(FALSE);
}Server side
In the disconnect control function
void CMFCChartServerDlg::OnBnClickedStopBtn()
{
//1. Control controls
GetDlgItem(IDC_START_BTN)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP_BTN)->EnableWindow(FALSE);
GetDlgItem(IDC_SEND_BTN)->EnableWindow(FALSE);
//2. Recycle resources
m_server->Close();
if (m_server != NULL) {
delete m_server;
m_server = NULL;
}
if (m_chat != NULL) {
delete m_chat;
m_chat = NULL;
}
//3. Displayed in the list box
CString strShow;
strShow = CatShowString(_T(""), _T(" The server stops "));
m_list.AddString(strShow);
UpdateData(FALSE);
}2.6 Change font color

Bind member variables to the control , Add colors to the initialization function and set the default options
m_WordColorCombo.AddString(_T(" black "));
m_WordColorCombo.AddString(_T(" Red "));
m_WordColorCombo.AddString(_T(" Blue "));
m_WordColorCombo.AddString(_T(" green "));
// Set the current selection to 0 individual
m_WordColorCombo.SetCurSel(0);
SetDlgItemText(IDC_COLOUR_COMBO, _T(" black "));Set the control color response function
HBRUSH CMFCChatClientDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
CString strColor;
m_WordColorCombo.GetWindowTextW(strColor);
//GetDlgCtrlID Returns a pointer to a given control or child window , There is no return NULL
if (IDC_LIST1 == pWnd->GetDlgCtrlID() || IDC_SENDMSG_EDIT == pWnd->GetDlgCtrlID()) {
if (strColor == L" black ") {
pDC->SetTextColor(RGB(0, 0, 0));
}
else if (strColor == L" Red ") {
pDC->SetTextColor(RGB(255, 0, 0));
}
else if (strColor == L" green ") {
pDC->SetTextColor(RGB(0, 255, 255));
}
else if (strColor == L" Blue ") {
pDC->SetTextColor(RGB(0, 0, 255));
}
}
return hbr;
}2.7 The dialog interface inserts a background image

stay OnPaint Function
// Determine the target area → Load the resource image and convert it to a memory device → mapping
//1. Definition dc
CPaintDC dc(this); // Device context for drawing
//2. Determine the drawing area
CRect rect;
GetClientRect(&rect); // Get the dialog area
//3. Create a memory device environment Create compatibility dc
CDC dcBmp;
dcBmp.CreateCompatibleDC(&dcBmp);
//4. Load resource pictures
CBitmap bmpBackGround;
bmpBackGround.LoadBitmap(IDB_BITMAP);
//5. Load the picture Ziyun into the bitmap bBitMap Bitmap
BITMAP bBitmap;
bmpBackGround.GetBitmap(&bBitmap);
//6. Select the bitmap into the temporary memory device environment
CBitmap* pbmpOld = dcBmp.SelectObject(&bmpBackGround);
//7. Began to draw
dc.StretchBlt(0, 0, rect.Width(), rect.Height(), &dcBmp, 0, 0, bBitmap.bmWidth, bBitmap.bmHeight, SRCCOPY);
边栏推荐
- 【Delphi】FMX Form的BorderStyles不同平台说明
- 谷歌的新编程语言被称为 Carbon
- YGG cooperates with my pet hooligan, AMGI's flagship NFT project, to enter the rabbit hole
- 小场景带来大提升!百度飞桨EasyDL助力制造业流水线AI升级
- How to implement an asynchronous task queue system that can handle massive data (supreme Collection Edition)
- SQL injection
- Cookies and sessions
- 解决IBGP的水平分割和BGP选路原则
- Intranet penetration learning (II) information collection
- Task 2 kaggle diabetes detection
猜你喜欢

游戏合作伙伴专题:BreederDAO 与 Ultiverse 建立了一个新的元宇宙

Read the four service types of kubernetes!

How to build a super interface collaboration platform: count the six weapons of apifox

打字比赛圆满结束!

TinUI发展历程

分组卷积(Group Converlution)

YGG 与 AMGI 的旗舰 NFT 项目 My Pet Hooligan 合作进入 The Rabbit Hole

AI 技术,让复杂世界简单化 | TeaTalk·Online 应用实战系列第 2 期

如何实现一个能处理海量数据的异步任务队列系统(至尊典藏版)

Pandonia spirit voxedit creation competition
随机推荐
SQL注入
After being fined "paid leave" for one month, Google fired him from AI on "love"
Kotlin - 协程上下文 CoroutineContext
BUU刷题记2
Read the four service types of kubernetes!
谷歌的新编程语言被称为 Carbon
一文读懂 Kubernetes的四种服务类型!
vs如何读取mysql中的数据(顺便通过代码解决了中文乱码问题)
为什么 ThreadLocal 可以做到线程隔离?
tkinter使用wpf控件
QT信号与槽连接(松耦合)
numpy.zeros_ like
App uploader download and installation
解决IBGP的水平分割和BGP选路原则
Kotlin - 协程构建器 CoroutineBuilder
arpspoof 安装和使用
Vite configuration eslint specification code
本机号码一键登录原理与应用(荣耀典藏版)
Pandonia spirit voxedit creation competition
81.(cesium之家)cesium修改灰色背景(默认蓝色)