📄 infodlg.cpp
字号:
// InfoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Info.h"
#include "InfoDlg.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 void OnLButtonDown(UINT nFlags, CPoint point);
//}}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)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CInfoDlg dialog
CInfoDlg::CInfoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CInfoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CInfoDlg)
m_strMsg = _T("");
m_strName = _T("");
m_nPort = 0;
m_nType = -1;
m_strState = _T("");
m_hisMsg = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bShow=true;
}
void CInfoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CInfoDlg)
DDX_Control(pDX, IDM_EXIT, m_exit);
DDX_Control(pDX, IDC_EDIT_PORT, m_edit_port);
DDX_Control(pDX, IDC_EDIT_NAME, m_edit_name);
DDX_Control(pDX, IDC_EDIT_MSG, m_edit_msg);
DDX_Control(pDX, IDC_FRESH, m_fresh);
DDX_Control(pDX, IDC_SHOW, m_shou);
DDX_Control(pDX, IDC_ABOUT, m_about);
DDX_Control(pDX, IDC_BUTTON_SEND, m_button_send);
DDX_Control(pDX, IDC_BUTTON_CLOSE, m_button_close);
DDX_Control(pDX, IDC_LIST_SEND, m_list_send);
DDX_Control(pDX, IDC_BUTTON, m_button);
DDX_Text(pDX, IDC_EDIT_MSG, m_strMsg);
DDX_Text(pDX, IDC_EDIT_NAME, m_strName);
DDX_Text(pDX, IDC_EDIT_PORT, m_nPort);
DDX_Radio(pDX, IDC_RADIO1, m_nType);
DDX_Text(pDX, IDC_STATE, m_strState);
DDX_Text(pDX, IDC_HIS_MSG, m_hisMsg);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CInfoDlg, CDialog)
//{{AFX_MSG_MAP(CInfoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
ON_BN_CLICKED(IDC_BUTTON, OnButton)
ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
ON_COMMAND(IDM_EXIT, OnExit)
ON_COMMAND(IDC_ABOUT, OnAbout)
ON_COMMAND(ID_MENUITEM32771, OnMenuitem32771)
ON_BN_CLICKED(IDC_SHOW, OnShow)
ON_BN_CLICKED(IDC_FRESH, OnFresh)
ON_WM_LBUTTONDOWN()
ON_WM_CREATE()
ON_WM_LBUTTONDBLCLK()
ON_WM_CONTEXTMENU()
ON_COMMAND(ID_MENUITEM32772, OnMenuitem32772)
ON_MESSAGE(MYWM_NOTIFYICON,OnMyIconNotify)
ON_MESSAGE(WM_HOTKEY,OnHotKey)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CInfoDlg message handlers
BOOL CInfoDlg::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
CInfoApp* app=(CInfoApp*)AfxGetApp();
if(app->m)
{
m_nType=0; UpdateData(false);
GetDlgItem(IDC_BUTTON)->SetWindowText("开始连接");
}
else
{
m_nType=1; UpdateData(false);
GetDlgItem(IDC_BUTTON)->SetWindowText("开始侦听");
}
char buf[200];
DWORD dd=200;
::GetComputerName(buf,&dd);
m_nPort=4000;
m_listenSocket.SetParenet(this);
m_connectSocket.SetParenet(this);
UpdateData(false);
GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(false);
GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(FALSE);
ExpandDialog(IDC_LIST_SEND,true);
m_menu.LoadMenu(IDR_MENU1);
TaskBarAddIcon(GetSafeHwnd(),100,AfxGetApp()->LoadIcon(IDR_MAINFRAME),_T("聊天软件--三峡大学"));
RegisterHotKey(m_hWnd,m_hotShow,MOD_WIN,'Q');
SetWindowPos(&CWnd::wndTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CInfoDlg::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 CInfoDlg::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 CInfoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CInfoDlg::OnRadio1()
{
UpdateData(true);
if(m_nType==0)
m_button.SetWindowText("开始连接");
else
m_button.SetWindowText("开始侦听");
}
void CInfoDlg::OnRadio2()
{
UpdateData(true);
if(m_nType==0)
m_button.SetWindowText("开始连接");
else
m_button.SetWindowText("开始侦听");
}
void CInfoDlg::OnAccept()
{
m_listenSocket.Accept(m_connectSocket);
m_strState="已经和 客户端 建立了连接";
UpdateData(false);
GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(TRUE);
}
void CInfoDlg::OnButton()
{
UpdateData(true);
GetDlgItem(IDC_RADIO1)->EnableWindow(FALSE);
GetDlgItem(IDC_RADIO2)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT_NAME)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT_PORT)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(TRUE);
if(m_nType==0)
{
m_connectSocket.Create();
m_connectSocket.Connect(m_strName,m_nPort);
m_strState="◆正在连接到服务器(如果长时间无响应,请确认服务端程序是否处于侦听状态)...";
UpdateData(false);
}
else
{
m_listenSocket.Create(m_nPort);
m_listenSocket.Listen();
m_strState="◆正在侦听客户端连接...";
UpdateData(false);
}
}
void CInfoDlg::OnConnect()
{
m_strState="已经和 服务器 建立了连接";
UpdateData(false);
GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(true);
}
void CInfoDlg::OnButtonSend()
{
OnSend();
}
void CInfoDlg::OnSend()
{
UpdateData(true);
if(m_strMsg !="")
{
if( m_connectSocket.Send (LPCTSTR(m_strMsg), m_strMsg.GetLength()) ==SOCKET_ERROR )
{
m_strState="◆发送消息失败, 请确认已经正确连接!!";
UpdateData(false);
}
else
{
m_list_send.AddString("●你说:"+m_strMsg);
m_list_send.AddString("-------------------------------------------------------------");
UpdateData(false);
}
}
}
void CInfoDlg::OnReceive()
{
char *pbuffer=new char[1025];
int bufferSize=1024;
CString str;
int iReceived=m_connectSocket.Receive(pbuffer,bufferSize);
if( iReceived==SOCKET_ERROR)
{
m_strState="接受消息失败 !!";
UpdateData(false);
}
else
{
pbuffer[iReceived]=NULL;
str=pbuffer;
m_list_send.AddString("■对方说:"+str);
m_list_send.AddString("-------------------------------------------------------------");
m_hisMsg=str;
UpdateData(false);
}
}
void CInfoDlg::OnButtonClose()
{
OnClose();
}
void CInfoDlg::OnClose()
{
m_connectSocket.Close();
m_listenSocket.Close();
GetDlgItem(IDC_RADIO1)->EnableWindow(true);
GetDlgItem(IDC_RADIO2)->EnableWindow(true);
GetDlgItem(IDC_EDIT_NAME)->EnableWindow(true);
GetDlgItem(IDC_EDIT_PORT)->EnableWindow(true);
GetDlgItem(IDC_BUTTON)->EnableWindow(true);
GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(false);
if(m_nType==0)
{
m_strState="已经和 服务器 断开了连接 ";
UpdateData(false);
}
else
{
m_strState="已经 客户端 断开了连接 ";
UpdateData(false);
}
}
void CInfoDlg::OnExit()
{
CDialog::OnOK();
}
void CInfoDlg::OnAbout()
{
CAboutDlg dlg;
dlg.DoModal();
}
void CInfoDlg::OnMenuitem32771()
{
MessageBox("本软件目前还是免费版本,你可以自由复制使用。","说明",MB_OK | MB_ICONINFORMATION);
}
void CInfoDlg::OnShow()
{
static BOOL bExpand=false;//记录扩展或收缩状态
ExpandDialog(IDC_LIST_SEND,bExpand);//扩展或收缩对话框
bExpand=!bExpand;
}
void CInfoDlg::ExpandDialog(int nResourceID, BOOL bExpand)
{
static CRect rcLarge; //记录扩展后对话框的位置和大小
static CRect rcSmall; //记录收缩后对话框的位置和大小
CString sExpand;
if(rcLarge.IsRectNull())
{
CRect rcLandmark; //记录分界线的位置和大小
CWnd* pWndLandmark=GetDlgItem(nResourceID);
ASSERT(pWndLandmark);
GetWindowRect(rcLarge);
pWndLandmark->GetWindowRect(rcLandmark);
rcSmall=rcLarge;
rcSmall.bottom=rcLandmark.top;// 如果将扩展部分放在对话框的右边,只需将此句改为rcSmall.right=rcLandmark.left;即可
}
if(bExpand)
{
SetWindowPos(NULL,0,0,rcLarge.Width(),rcLarge.Height(),
SWP_NOMOVE|SWP_NOZORDER);
sExpand="隐藏记录";
EnableVisibleChildren();
}
else
{
SetWindowPos(NULL,0,0,rcSmall.Width(),rcSmall.Height(),SWP_NOMOVE|SWP_NOZORDER);
sExpand="显示记录";
EnableVisibleChildren();
}
SetDlgItemText(IDC_SHOW,sExpand);
}
void CInfoDlg::EnableVisibleChildren()
{
CWnd *pWndCtl=GetWindow(GW_CHILD);
CRect rcTest;
CRect rcControl; //记录各控件的位置和大小
CRect rcShow; //记录目前显示对话框的位置和大小
GetWindowRect(rcShow);
while(pWndCtl!=NULL)
{
pWndCtl->GetWindowRect(rcControl);
if(rcTest.IntersectRect(rcShow,rcControl))
pWndCtl->EnableWindow(TRUE);
else
pWndCtl->EnableWindow(FALSE);
pWndCtl=pWndCtl->GetWindow(GW_HWNDNEXT);
}
}
void CInfoDlg::OnFresh()
{
m_list_send.ResetContent();
}
void CInfoDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
ReleaseCapture();
SendMessage(WM_NCLBUTTONDOWN,HTCAPTION,0);
CDialog::OnLButtonDown(nFlags, point);
}
int CInfoDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
void CInfoDlg::OnLButtonDblClk(UINT nFlags, CPoint point)
{
CAboutDlg dlg;
dlg.DoModal();
CDialog::OnLButtonDblClk(nFlags, point);
}
void CAboutDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
CDialog::OnOK();
CDialog::OnLButtonDown(nFlags, point);
}
void CInfoDlg::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu *pmenu;
pmenu=m_menu.GetSubMenu(0);
pmenu->TrackPopupMenu( TPM_RIGHTBUTTON,point.x,point.y,this,NULL);
}
void CInfoDlg::OnMenuitem32772()
{
MessageBox("其实很简单,自己看一下就知道了!","如何使用",MB_OK | MB_ICONINFORMATION);
}
BOOL CInfoDlg::TaskBarAddIcon(HWND hwnd, UINT uID, HICON hicon, LPSTR lpszTip)
{
NOTIFYICONDATA d;
d.cbSize=sizeof(NOTIFYICONDATA);
d.hWnd=hwnd;
d.uID=uID;
d.uFlags=NIF_ICON | NIF_TIP | NIF_MESSAGE;
d.uCallbackMessage=MYWM_NOTIFYICON;
d.hIcon=hicon;
if(lpszTip)
lstrcpy(d.szTip,lpszTip);
else
d.szTip[0]='\0';
return Shell_NotifyIcon(NIM_ADD,&d);
}
BOOL CInfoDlg::TaskBarDeleteIcon(HWND hwnd, UINT uID)
{
NOTIFYICONDATA d;
d.cbSize=sizeof(NOTIFYICONDATA);
d.hWnd=hwnd;
d.uID=uID;
return Shell_NotifyIcon(NIM_DELETE,&d);
}
void CInfoDlg::OnMyIconNotify(WPARAM wParam,LPARAM lParam)
{
UINT uMouseMsg=LOWORD(lParam);
switch(uMouseMsg)
{
case WM_LBUTTONDOWN:
{
if(m_bShow)
{
ShowWindow(SW_HIDE);
m_bShow=false;
}
else
{
ShowWindow(SW_SHOWNORMAL);
m_bShow=true;
}
}break;
case WM_RBUTTONDOWN:
{
CPoint pt;
GetCursorPos(&pt);
AfxGetMainWnd()->SetForegroundWindow();
CMenu* psub=(CMenu*)m_menu.GetSubMenu(0);
psub->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,pt.x,pt.y,this);
AfxGetMainWnd()->PostMessage(WM_NULL,0,0);
}
break;
}
}
BOOL CInfoDlg::DestroyWindow()
{
TaskBarDeleteIcon(GetSafeHwnd(),100);
UnregisterHotKey(m_hWnd,m_hotShow);
return CDialog::DestroyWindow();
}
LRESULT CInfoDlg::OnHotKey(WPARAM wp,LPARAM lp)
{
if(wp==m_hotShow && !IsWindowVisible())
ShowWindow(SW_SHOWNORMAL);
else
ShowWindow(SW_HIDE);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -