📄 msgclientdlg.cpp
字号:
// MsgClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MsgClient.h"
#include "MsgClientDlg.h"
#include "ClientSocket.h"
#include "ShowMsgDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CMsgClientDlg dialog
CMsgClientDlg::CMsgClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMsgClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMsgClientDlg)
m_strSysMsg = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMsgClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMsgClientDlg)
DDX_Control(pDX, IDC_LIST_MSG, m_ListMsg);
DDX_Text(pDX, IDC_STATIC_MSG, m_strSysMsg);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMsgClientDlg, CDialog)
ON_MESSAGE(WM_USER_RECEIVE_MSG, OnReceiveMsg)
//{{AFX_MSG_MAP(CMsgClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_WM_CLOSE()
ON_NOTIFY(NM_DBLCLK, IDC_LIST_MSG, OnDblclkListMsg)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_MESSAGE(WM_HOTKEY,OnHotKey)
ON_COMMAND(IDM_TASK_OPEN, OnTaskOpen)
ON_COMMAND(IDM_TASK_QUIT, OnTaskQuit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMsgClientDlg message handlers
BOOL CMsgClientDlg::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
//----------------------列表初始化--------------------------
RECT rect;
m_ListMsg.ModifyStyle(LVS_TYPEMASK, LVS_REPORT);
ListView_SetExtendedListViewStyle(m_ListMsg.m_hWnd, LVS_EX_GRIDLINES |LVS_EX_FULLROWSELECT );
m_ListMsg.DeleteAllItems();
m_ListMsg.InsertColumn(0, "时间");
m_ListMsg.InsertColumn(1, "信息");
m_ListMsg.GetWindowRect(&rect);
int nTotalX = rect.right - rect.left;
m_ListMsg.SetColumnWidth(0, (nTotalX-4)*3/10);
m_ListMsg.SetColumnWidth(1, (nTotalX-4)*7/10);
//----------------------列表初始化结束----------------------
m_pSocket=NULL;
OnHide();
ReadIniFile();
return TRUE; // return TRUE unless you set the focus to a control
}
void CMsgClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else if(nID==SC_MINIMIZE)
{
ShowWindow(SW_HIDE);
}
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 CMsgClientDlg::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 CMsgClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
CClientSocket* CMsgClientDlg::ConnectServer()
{
CClientSocket *pTempSocket=new CClientSocket(this);
//生成客户端的Socket
if(!(pTempSocket->Create()))
{
delete pTempSocket;
AfxMessageBox("生成Socket出错");
return NULL;
}
//进行服务器的连接
if(!pTempSocket->Connect(m_strSerVerIP ,m_nPort))
{
delete pTempSocket;
//AfxMessageBox("和服务器进行连接时出错");
m_strSysMsg="和服务器进行连接时出错";
UpdateData(FALSE);
return NULL;
}
return pTempSocket;
}
void CMsgClientDlg::Connect()
{
if(!m_pSocket)
{
m_pSocket=new CClientSocket(this);
if(!(m_pSocket=ConnectServer()))
{
delete m_pSocket; //虽然此时的m_pSocket是NULL,但仍然可以进行delete操作
m_pSocket=NULL;
return;
}
}
CString strCmdMsg="GET_MSG";
int iSend = m_pSocket->Send(strCmdMsg,strlen(strCmdMsg));
}
void CMsgClientDlg::DisConnect()
{
if(m_pSocket)
{
delete m_pSocket;//先要进行销毁对象指针的操作,
m_pSocket=NULL;//然后使指针为NULL
}
}
//-------------------------------------------------------
void CMsgClientDlg::OnTimer(UINT nIDEvent)
{
Connect();
CDialog::OnTimer(nIDEvent);
}
void CMsgClientDlg::OnClose()
{
KillTimer(WM_GET_MSG_TIMER);
CDialog::OnClose();
}
//------------------------------------------------------
LRESULT CMsgClientDlg::OnReceiveMsg(WPARAM wParam, LPARAM lParam)
{
m_ListMsg.DeleteAllItems();
if ( wParam ==0 )
{
m_strSysMsg = "目前没有信息!";
UpdateData(FALSE);
return 0;
}
CString strTime;
CTime ctTime;
CString strMsg;
int iSubItem;
pReceiveInfo = (struct SentInfo*) tempMsg;
CString temp;
for( int i=0;i<pReceiveInfo->iCurrentMsgCount;i++)
{
ctTime = pReceiveInfo->MsgList[i].ctAddTime;
strTime = ctTime.Format("%Y-%m-%d %H:%M:%S");
strMsg.Format("%s",pReceiveInfo->MsgList[i].strMsg);
strMsg=strMsg.Left(pReceiveInfo->MsgList[i].iMsgSize);
iSubItem = m_ListMsg.InsertItem(0, strTime);
m_ListMsg.SetItemText (iSubItem,1,strMsg);
}
m_strSysMsg.Format("有%d条信息,共%dBYTE.",pReceiveInfo->iCurrentMsgCount,wParam);
UpdateData(FALSE);
CShowMsgDlg ShowDlg;
ShowDlg.DoModal();
return 0;
}
void CMsgClientDlg::OnDblclkListMsg(NMHDR* pNMHDR, LRESULT* pResult)
{
CString strTime = m_ListMsg.GetItemText(m_ListMsg.GetNextItem(-1,LVNI_ALL | LVNI_SELECTED),0);
CString strMsg = m_ListMsg.GetItemText(m_ListMsg.GetNextItem(-1,LVNI_ALL | LVNI_SELECTED),1);
if (strTime.IsEmpty())
{
return;
}
CString temp;
temp.Format("%s\n%s",strTime,strMsg);
MessageBox(temp,"北斗星信息服务");
*pResult = 0;
}
/********************************************************
* 读取初始数据
* 2004/9/20
********************************************************/
void CMsgClientDlg::ReadIniFile()
{
char szParameter[1024] = {NULL};
char *szSection;
char *szFileName = "ParaSet.ini";
char szDir[1024];
char szTemp[1024];
szSection = "MsgClient";
GetCurrentDirectory(1024,szDir);
SetCurrentDirectory( szDir );
wsprintf(szTemp,"%s\\%s",szDir,szFileName);
szFileName = szTemp;
//服务器IP
lstrcpy( szParameter, "127.0.0.1" );
::GetPrivateProfileString( szSection, "ServerIP", "127.0.0.1", szParameter, 1024, szFileName);
m_strSerVerIP.Format("%s",szParameter);
//服务器端口
m_nPort = ::GetPrivateProfileInt(szSection,"Port",5200,szFileName);
//链接间隔
int iTimer = ::GetPrivateProfileInt(szSection,"Timer",60,szFileName);
iTimer = iTimer*1000;
SetTimer(WM_GET_MSG_TIMER,iTimer,NULL);
}
/*****************************************************************
* 功能:实现托盘图标
* 函数:OnHide() WindowProc() OnTray() DeleteIcon()
******************************************************************/
void CMsgClientDlg::OnHide()
{
NOTIFYICONDATA tnd;
tnd.cbSize=sizeof(NOTIFYICONDATA);
tnd.hWnd=this->m_hWnd;
tnd.uID=IDR_MAINFRAME;
tnd.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
tnd.uCallbackMessage=WM_TRAY;
tnd.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
strcpy(tnd.szTip,"北斗星信息服务");
::Shell_NotifyIcon(NIM_ADD,&tnd);
this->ShowWindow(SW_HIDE);
}
LRESULT CMsgClientDlg::OnTray(WPARAM wParam,LPARAM lParam)
{
if(wParam!=IDR_MAINFRAME)
{
return 0L;
}
CMenu menu,*pSubMenu;
CWnd *pTarget=AfxGetMainWnd();
if(LOWORD(lParam)==WM_RBUTTONUP)
{
if(!menu.LoadMenu(IDR_MENU_TASK))
{
return 0;
}
if(!(pSubMenu=menu.GetSubMenu(0)))
{
return 0;
}
::SetMenuDefaultItem(pSubMenu->m_hMenu,IDR_MENU_TASK,0);
CPoint pos;
GetCursorPos(&pos);
::TrackPopupMenu(pSubMenu->m_hMenu,0,pos.x,pos.y,0,pTarget->GetSafeHwnd(),NULL);
pTarget->PostMessage(WM_NULL,0,0);
menu.DestroyMenu();
}
else if(LOWORD(lParam)==WM_LBUTTONDBLCLK)
{
this->ShowWindow(SW_SHOW);
}
return 0;
}
int CMsgClientDlg::DeleteIcon()
{
BOOL res;
NOTIFYICONDATA tnd;
tnd.cbSize=sizeof(NOTIFYICONDATA);
tnd.hWnd=this->m_hWnd;
tnd.uID=IDR_MAINFRAME;
res=Shell_NotifyIcon(NIM_DELETE,&tnd);
return res;
}
LRESULT CMsgClientDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if(message == WM_TRAY)
{
return OnTray(wParam,lParam);
}
return CDialog::WindowProc(message, wParam, lParam);
}
void CMsgClientDlg::OnTaskOpen()
{
this->ShowWindow(SW_SHOW);
}
void CMsgClientDlg::OnTaskQuit()
{
::PostQuitMessage(0);
}
/*****************************************************************
* 功能:启动及退出系统
******************************************************************/
void CMsgClientDlg::OnDestroy()
{
CDialog::OnDestroy();
DeleteIcon();
UnregisterHotKey(m_hWnd,1001);
UnregisterHotKey(m_hWnd,1002);
UnregisterHotKey(m_hWnd,1003);
UnregisterHotKey(m_hWnd,1004);
}
int CMsgClientDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
RegisterHotKey(m_hWnd,1001,MOD_ALT,'q');
RegisterHotKey(m_hWnd,1002,MOD_ALT,'Q');
RegisterHotKey(m_hWnd,1003,MOD_ALT,'w');
RegisterHotKey(m_hWnd,1004,MOD_ALT,'W');
return 0;
}
/**********************************************************
* 名称:OnHotKey
* 功能:系统热键的操作
**********************************************************/
LRESULT CMsgClientDlg::OnHotKey(WPARAM wParam,LPARAM lParam)
{
if(wParam==1001 || wParam==1002)
{
this->ShowWindow(SW_SHOW);
}
if(wParam==1003 || wParam==1004)
{
this->ShowWindow(SW_HIDE);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -