📄 imdlg.cpp
字号:
// IMDlg.cpp : implementation file
//
#include "stdafx.h"
#include "IM.h"
#include "IMDlg.h"
#include "IM1.h"
#include "RegDlg.h"
extern int m_uid,total;
extern CString CRdir;
extern HWND IMhWnd;
extern SOCKET TCPsocket;
#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()
/////////////////////////////////////////////////////////////////////////////
// CIMDlg dialog
CIMDlg::CIMDlg(CWnd* pParent /*=NULL*/)
: CDialog(CIMDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CIMDlg)
m_uPW = 0;
m_nuid = 0;
m_nPort = 0;
// m_nCheck = FALSE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON);
}
void CIMDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CIMDlg)
DDX_Text(pDX, IDC_EDIT2, m_uPW);
DDX_Text(pDX, IDC_EDIT1, m_nuid);
DDX_Text(pDX, IDC_EDIT3, m_nPort);
// DDX_Check(pDX, IDC_CHECK2, m_nCheck);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CIMDlg, CDialog)
//{{AFX_MSG_MAP(CIMDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_BTN_REG, OnBtnReg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CIMDlg message handlers
BOOL CIMDlg::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, IDI_ICON, 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
//初始化TCP套接字
InitTCPSocket();
// TODO: Add extra initialization here
m_uLogin=FALSE;
SetDlgItemText(IDC_EDIT1,NULL);
SetDlgItemText(IDC_EDIT2,NULL);
SetDlgItemText(IDC_EDIT3,NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CIMDlg::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 CIMDlg::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 CIMDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CIMDlg::OnOK()
{
// TODO: Add extra validation here
UpdateData();//获取对话框数据内容
CString str;
char buffer[4];
//标志a,代表客户端登陆请求 所属服务器A类服务
str.Format("a%d#%d#%d",m_nuid,m_uPW,m_nPort);
::send(TCPsocket,str,str.GetLength()+1,0);
//判断是否登陆成功 等待服务器回复
if(::recv(TCPsocket,buffer,4,0)==-1)
{
MessageBox("登陆失败!");
exit(1);
}
else
{
str.Format("%s",buffer);
total=atoi(str);
}
EndDialog(1);
m_uid=m_nuid;
char sdir[100];
//获取当前路径名
GetCurrentDirectory(100,sdir);
CRdir.Format("%s\\ChatRecord%d",sdir,m_nuid);
str.Format("%s\\fenzu.ini",CRdir);
FILE *R=fopen(str,"r+");
//判断纪录文件是否存在
if(R==NULL)
{
//不存在?则新创建
R=fopen(str,"w+");
if(R==NULL)
{
if (!SetCurrentDirectory(CRdir))
CreateDirectory(CRdir, NULL);
R=fopen(CRdir+"\\fenzu.ini","w+");
}
CRdir.Format(".\\ChatRecord%d",m_nuid);
//在新建分组文件里写入基本组 我的好友
WritePrivateProfileString("FriendGroup","Groupnum",
"1",CRdir+"\\fenzu.ini");
WritePrivateProfileString("FriendGroup","Groupname1",
"我的好友",CRdir+"\\fenzu.ini");
}
fclose(R);
IM dlg_IM;
dlg_IM.m_nPort=m_nPort;
dlg_IM.DoModal();
IMhWnd=NULL;
CDialog::OnOK();
}
void CIMDlg::OnBtnReg()
{
// TODO: Add your control notification handler code here
RegDlg rd;
rd.DoModal();
}
BOOL CIMDlg::InitTCPSocket()
{
TCPsocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==TCPsocket)
{
AfxMessageBox("TCP套接字创建失败!");
exit(1);
}
SOCKADDR_IN servAddr;
servAddr.sin_family=AF_INET;
servAddr.sin_port=htons(700);
servAddr.sin_addr.S_un.S_addr=inet_addr("59.72.27.169");
/*注意 此处填服务器IP */;
//连接服务器端
if(::connect(TCPsocket, (SOCKADDR*)&servAddr, sizeof(servAddr)) == -1)
{
MessageBox("无法和服务器建立连接!");
exit(0);
}
return TRUE;
}
void CIMDlg::OnCancel()
{
// TODO: Add extra cleanup here
if(!TCPsocket)closesocket(TCPsocket);
exit(0);
CDialog::OnCancel();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -