?? mynetchatdlg.cpp
字號:
// MyNetChatDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MyNetChat.h"
#include "MyNetChatDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WM_NETICON WM_USER+100
///////////////////////////////////////////////////////////////////////////////////
SOCKET m_socket,m_hSocket;//host sock
sockaddr_in m_addr;//host address
sockaddr_in m_raddr;//host recive client address
sockaddr_in m_caddr;
char *message=NULL;
char *name=NULL;
int nItem;
CString changstr;
bool exitmsg=FALSE;
CString strname;
UINT AcceptThread(LPVOID lpvoid);
//CWinThread *pAccept;
SOCKET m_cSocket;//client sock
NOTIFYICONDATA tnd; //add icon
bool ShowApp=FALSE;
/////////////////////////////////////////////////////////////////////////////
// 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)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyNetChatDlg dialog
CMyNetChatDlg::CMyNetChatDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyNetChatDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyNetChatDlg)
m_sendstr = _T("");
m_showstr = _T("");
m_name = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyNetChatDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyNetChatDlg)
DDX_Control(pDX, IDC_EDIT1, m_showname);
DDX_Control(pDX, IDC_USER, m_List);
DDX_Control(pDX, IDC_SHOWSTR, m_show);
DDX_Control(pDX, IDC_SENDSTR, m_send);
DDX_Control(pDX, IDC_SENDED,m_SD);
DDX_Control(pDX, IDC_FINDUSER,m_FU);
DDX_Text(pDX, IDC_SENDSTR, m_sendstr);
DDX_Text(pDX, IDC_SHOWSTR, m_showstr);
DDX_Text(pDX, IDC_EDIT1, m_name);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyNetChatDlg, CDialog)
//{{AFX_MSG_MAP(CMyNetChatDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(ID_CREATHOST, OnCreathost)
ON_COMMAND(ID_EXITCHAT, OnExitchat)
ON_NOTIFY(NM_DBLCLK, IDC_USER, OnDblclkUser)
ON_WM_CREATE()
ON_COMMAND(ID_EXITWL, OnExitwl)
ON_COMMAND(ID_SHOWAPP, OnSetSever)
ON_BN_CLICKED(IDC_SENDED,OnSended)
ON_BN_CLICKED(IDC_FINDUSER, OnFinduser)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_NETICON,AddIcon)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyNetChatDlg message handlers
BOOL CMyNetChatDlg::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
char localname[64];
GetName(localname);
changstr=localname;
//////////////////////////////////
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyNetChatDlg::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 CMyNetChatDlg::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.
UINT AcceptThread(LPVOID lpvoid)
{
int Addrlength=sizeof(m_raddr);
m_socket=accept(m_hSocket,(LPSOCKADDR)&m_raddr,&Addrlength);
if(m_socket==INVALID_SOCKET)
{
closesocket(m_hSocket);
m_socket=NULL;
AfxMessageBox("Accept Error");
return 0;
}
AfxBeginThread(AcceptThread,NULL);
char buff[1024];
int ret=recv(m_socket,buff,1024,0);
if(ret==0||ret==SOCKET_ERROR)
{
closesocket(m_socket);
AfxMessageBox("recv bad");
return 0;
}
if(buff!=NULL)
{
int msgleng=0;
int nameleng=0;
int i;
nameleng=buff[0];
name=new char[nameleng+1];
for(i=0;i<nameleng;i++)
{name[i]=buff[i+1];}
msgleng=buff[nameleng+1];
message=new char[msgleng+1];
for(i=0;i<msgleng;i++)
{
message[i]=buff[i+2+nameleng];
}
name[nameleng]='\0';
message[msgleng]='\0';
CMyNetChatDlg* SM=(CMyNetChatDlg*)(AfxGetApp()->m_pMainWnd);
SM->ShowRecvData();
}
return 0;
}
HCURSOR CMyNetChatDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyNetChatDlg::OnCreathost() //create host
{
if(m_hSocket!=NULL)
{
closesocket(m_hSocket);
m_hSocket=NULL;
}
if(m_hSocket==NULL)
{
m_hSocket=socket(AF_INET,SOCK_STREAM,0);
ASSERT(m_hSocket!=NULL);
}
UpdateData(TRUE);
m_addr.sin_addr.S_un.S_addr=INADDR_ANY;
m_addr.sin_family=AF_INET;
m_addr.sin_port=htons(52428);
int ret=0;
ret=bind(m_hSocket,(LPSOCKADDR)&m_addr,sizeof(m_addr));
if(ret==SOCKET_ERROR)
{
closesocket(m_hSocket);
AfxMessageBox("Bind Error");
return ;
}
ret=listen(m_hSocket,12);
if(ret==SOCKET_ERROR)
{
closesocket(m_hSocket);
AfxMessageBox("Listen Error");
return ;
}
AfxBeginThread(AcceptThread,NULL);
}
void CMyNetChatDlg::OnExitchat()
{
// EXit Application and all thread;
exitmsg=TRUE;
Shell_NotifyIcon(NIM_DELETE,&tnd);
CDialog::OnOK();
}
void CMyNetChatDlg::GetName(char Temp[64])
{
gethostname(Temp,64);
}
CString CMyNetChatDlg::GetIP(CString username)
{
CString hostaddr;
struct hostent *hostname=gethostbyname(username);
for(int i=0;hostname!=NULL&&hostname->h_addr_list[i];i++)
{
hostaddr=inet_ntoa(*(struct in_addr *)hostname->h_addr_list[i]);
}
return hostaddr;
}
void CMyNetChatDlg::OnSended()
{
// send message to host
m_showname.GetWindowText(m_name);
if(m_name.IsEmpty())
{
AfxMessageBox("請選擇發送用戶");
return;
}
strname=GetIP(m_name);
if(m_cSocket!=NULL)
{
closesocket(m_cSocket);
m_cSocket=NULL;
}
if(m_cSocket==NULL)
{
m_cSocket=socket(AF_INET,SOCK_STREAM,0);
ASSERT(m_cSocket!=NULL);
}
UpdateData(TRUE);
m_caddr.sin_addr.S_un.S_addr=inet_addr(strname);
m_caddr.sin_family=AF_INET;
m_caddr.sin_port=htons(52428);
int ret=0;
int caddrlength=sizeof(m_caddr);
ret=connect(m_cSocket,(LPSOCKADDR)&m_caddr,caddrlength);
if(ret==SOCKET_ERROR)
{
connectFlag=FALSE;
int err=WSAGetLastError();
if(err==10061)
AfxMessageBox(_T("服務器未打開"));
return ;
}
connectFlag=TRUE;
if(connectFlag==TRUE)
{
m_send.GetWindowText(m_sendstr);
if(m_sendstr.IsEmpty())
{
AfxMessageBox(_T("不能發空消息!"));
return;
}
CString sendstr;
char *sendbuf=NULL;
sendstr+=char(changstr.GetLength());
sendstr+=changstr;
sendstr+=char(m_sendstr.GetLength());
sendstr+=m_sendstr;
sendbuf=sendstr.GetBuffer(0);
sendstr.ReleaseBuffer(-1);
int slength;
slength=send(m_cSocket,sendbuf,sendstr.GetLength(),0);
if(slength!=sendstr.GetLength()||m_cSocket==SOCKET_ERROR)
{
int errs=WSAGetLastError();
AfxMessageBox(_T("發送消息失敗"));
return ;
}
m_send.SetWindowText("");
return ;
}
AfxMessageBox(_T("請先連接"));
}
void CMyNetChatDlg::OnDblclkUser(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
POSITION pos;
m_showname.SetWindowText("");
nItem=0;
pos=m_List.GetFirstSelectedItemPosition();
if(pos==NULL)
{
return ;
}else
{
while(pos)
{
nItem=m_List.GetNextSelectedItem(pos);
}
m_name=m_List.GetItemText(nItem,0);
m_showname.SetWindowText(m_name);
}
*pResult = 0;
}
void CMyNetChatDlg::ShowRecvData()
{
CString tempstr;
tempstr="";
m_show.SetWindowText("");
if(name!=NULL&&message!=NULL)
{
tempstr=name;
m_showstr=message;
m_show.SetWindowText(tempstr+":"+m_showstr);
message=NULL;
name=NULL;
}
}
void CMyNetChatDlg::OnOK()
{
}
void CMyNetChatDlg::OnCancel()
{
}
int CMyNetChatDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
tnd.cbSize=sizeof(NOTIFYICONDATA);
tnd.hWnd=this->m_hWnd;
tnd.uID=IDR_MAINFRAME;
tnd.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
tnd.uCallbackMessage=WM_NETICON;
tnd.hIcon=LoadIcon(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDR_MAINFRAME));
strcpy(tnd.szTip,"無聊");
Shell_NotifyIcon(NIM_ADD,&tnd);
return 0;
}
void CMyNetChatDlg::AddIcon(WPARAM wParam,LPARAM lParam)
{
CMenu W_menu,*pmenu;
UINT uMouseMsg;
UINT uID;
POINT pos;
uID=(UINT)wParam;
uMouseMsg=(UINT)lParam;
W_menu.LoadMenu(IDR_WULIAO);
pmenu=W_menu.GetSubMenu(0);
CWnd *pMain=AfxGetMainWnd();
if(uMouseMsg==WM_RBUTTONDOWN)
{
switch(uID)
{
case IDR_MAINFRAME:
GetCursorPos(&pos);
pMain->SetForegroundWindow();
::TrackPopupMenu(pmenu->m_hMenu,0,pos.x,pos.y,0,pMain->GetSafeHwnd(),NULL);
break;
}
}
return;
}
void CMyNetChatDlg::OnExitwl()
{
// TODO: Add your command handler code here
OnExitchat();
}
void CMyNetChatDlg::OnSetSever()
{
// TODO: Add your command handler code here
OnCreathost();
}
BOOL CMyNetChatDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message==WM_SYSKEYDOWN)
{
short AltState=::GetKeyState(0x12);
if((AltState)&&(pMsg->wParam==VK_F4))
{
OnExitchat();
}
}
if(pMsg->message==WM_KEYDOWN)
{
if(pMsg->wParam==VK_TAB)
OnSended();
}
return CDialog::PreTranslateMessage(pMsg);
}
void CMyNetChatDlg::GetLanActiveComputer()
{
CString strTemp;
struct hostent *host;
//struct in_addr *ptr; // 檢索IP地址
DWORD dwScope = RESOURCE_CONTEXT;
NETRESOURCE *NetResource = NULL;
HANDLE hEnum;
WNetOpenEnum( dwScope, NULL, NULL,
NULL, &hEnum );
if ( hEnum )
{
DWORD Count = 0xFFFFFFFF;
DWORD BufferSize = 2048;
LPVOID Buffer = new char[2048];
WNetEnumResource( hEnum, &Count,
Buffer, &BufferSize );
NetResource = (NETRESOURCE*)Buffer;
char szHostName[200];
unsigned int i;
for ( i = 0;i < BufferSize/sizeof(NETRESOURCE);i++, NetResource++ )
{
if ( NetResource->dwUsage ==RESOURCEUSAGE_CONTAINER &&NetResource->dwType ==RESOURCETYPE_ANY )
{
if ( NetResource->lpRemoteName )
{
CString strFullName =NetResource->lpRemoteName;
if ( 0 ==strFullName.Left(2).Compare("\\\\") )
strFullName =strFullName.Right(strFullName.GetLength()-2);
gethostname( szHostName,strlen( szHostName ) );
host = gethostbyname(strFullName);
if(host == NULL) continue;
strTemp.Format("%s",strFullName);
m_List.InsertItem(i,strTemp,0);
}
}
}
delete Buffer;
WNetCloseEnum( hEnum );
}
}
void CMyNetChatDlg::OnFinduser()
{
GetLanActiveComputer();
Sleep(1000);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -