ServerDlg.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:8k
- // ServerDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Server.h"
- #include "ServerDlg.h"
- #include "netaddress.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()
- /////////////////////////////////////////////////////////////////////////////
- // CServerDlg dialog
- CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CServerDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CServerDlg)
- m_slocalip = _T("");
- m_nlistenport = 0;
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CServerDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CServerDlg)
- DDX_Control(pDX, ID_CLOSEDIRSVR, m_btnCloseSvr);
- DDX_Control(pDX, ID_OPENDIRSVR, m_btnOpenSvr);
- DDX_Text(pDX, IDC_EDTLOCALIP, m_slocalip);
- DDV_MaxChars(pDX, m_slocalip, 32);
- DDX_Text(pDX, IDC_EDTSVRPORT, m_nlistenport);
- DDV_MinMaxUInt(pDX, m_nlistenport, 5555, 10000);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
- //{{AFX_MSG_MAP(CServerDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_WM_DESTROY()
- ON_BN_CLICKED(ID_OPENDIRSVR, OnOpendirsvr)
- ON_BN_CLICKED(ID_CLOSEDIRSVR, OnClosedirsvr)
- ON_BN_CLICKED(IDC_LOCALSETTING, OnLocalsetting)
- ON_WM_TIMER()
- ON_COMMAND(IDM_TISHOWWND, OnTishowwnd)
- ON_COMMAND(IDM_TIEXIT, OnTiexit)
- ON_COMMAND(IDM_TIABOUT, OnTiabout)
- ON_COMMAND(IDM_TISTARTSVR, OnTistartsvr)
- ON_COMMAND(IDM_TICLOSESVR, OnTiclosesvr)
- //}}AFX_MSG_MAP
- ON_MESSAGE(UWM_TRAYICON, OnTrayNotification)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CServerDlg message handlers
- BOOL CServerDlg::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
- if(!this->m_otrayicon.Create(this->GetSafeHwnd(), GLOBAL_TRAYICON_ID,
- AfxGetApp()->LoadIcon(IDI_SVRSTOP), "trfServer服务器", UWM_TRAYICON))
- {
- return FALSE;
- }
- this->m_otrayicon.SetMenu(IDR_TRAYICON);
- EnableMenuItem(this->m_otrayicon.GetPopMenu(), IDM_TISTARTSVR, FALSE | MF_BYCOMMAND);
- EnableMenuItem(this->m_otrayicon.GetPopMenu(), IDM_TICLOSESVR, TRUE | MF_BYCOMMAND);
- m_btnOpenSvr.EnableWindow(TRUE);
- m_btnCloseSvr.EnableWindow(FALSE);
- this->m_slocalip.Format("%s / %s",
- CXNetAddress::GetLocalHostName().data(),
- CXNetAddress::GetLocalHostIp().data());
- this->m_nlistenport = SVR_DIRPORT;
- theApp.m_syslog.Print(LL_INFO, "CServerDlg::OnInitDialog : 本机标识[%s].n", this->m_slocalip.GetBuffer(0));
-
- UpdateData(FALSE);
- this->SetTimer(100, 60*1000, NULL);
- this->SetTimer(110, 500, NULL);
- this->SetTimer(111, 3000, NULL);
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CServerDlg::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 CServerDlg::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 CServerDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CServerDlg::OnDestroy()
- {
- m_service.Stop();
- CDialog::OnDestroy();
- }
- void CServerDlg::OnOpendirsvr()
- {
- UpdateData(TRUE);
- if(m_service.Start(this->m_nlistenport))
- {
- m_btnOpenSvr.EnableWindow(FALSE);
- m_btnCloseSvr.EnableWindow(TRUE);
- CWnd *pwnd = this->GetDlgItem(IDC_EDTSVRPORT);
- pwnd->EnableWindow(FALSE);
- this->m_otrayicon.ModifyIcon(IDR_MAINFRAME);
- EnableMenuItem(this->m_otrayicon.GetPopMenu(), IDM_TISTARTSVR, TRUE | MF_BYCOMMAND);
- EnableMenuItem(this->m_otrayicon.GetPopMenu(), IDM_TICLOSESVR, FALSE| MF_BYCOMMAND);
- }
- }
- void CServerDlg::OnClosedirsvr()
- {
- m_service.Stop();
- {
- m_btnOpenSvr.EnableWindow(TRUE);
- m_btnCloseSvr.EnableWindow(FALSE);
- CWnd *pwnd = this->GetDlgItem(IDC_EDTSVRPORT);
- pwnd->EnableWindow(TRUE);
- this->m_otrayicon.ModifyIcon(IDI_SVRSTOP);
- EnableMenuItem(this->m_otrayicon.GetPopMenu(), IDM_TISTARTSVR, FALSE| MF_BYCOMMAND);
- EnableMenuItem(this->m_otrayicon.GetPopMenu(), IDM_TICLOSESVR, TRUE | MF_BYCOMMAND);
- }
- }
- void CServerDlg::OnLocalsetting()
- {
- this->m_otrayicon.SetActive(TRUE);
- }
- void CServerDlg::OnTimer(UINT nIDEvent)
- {
- if(100 == nIDEvent)
- {
- string strtip = CXNetAddress::GetLocalHostName() + " / ";
- strtip = strtip + CXNetAddress::GetLocalHostIp();
- this->m_slocalip = strtip.data();
- this->m_otrayicon.ModifyTipInfo(this->m_slocalip.GetBuffer(0));
- UpdateData(FALSE);
- }
- else if(110 == nIDEvent)
- {
- this->OnLocalsetting();
- this->OnOpendirsvr();
- this->KillTimer(110);
- }
- else if(111 == nIDEvent)
- {
- this->m_otrayicon.Refresh();
- }
- CDialog::OnTimer(nIDEvent);
- }
- LRESULT CServerDlg::OnTrayNotification(WPARAM wparam, LPARAM lparam)
- {
- if ( wparam == GLOBAL_TRAYICON_ID )
- {
- if ( lparam == WM_RBUTTONUP )
- {
- m_otrayicon.PopupMenu();
- }
- else
- {
- if ( lparam == WM_LBUTTONDBLCLK )
- {
- if ( ::IsWindowVisible(m_otrayicon.GetOwnerWnd()) )
- {
- m_otrayicon.SetActive();
- ::ShowWindow(m_otrayicon.GetOwnerWnd(), SW_HIDE);
- }
- else
- {
- m_otrayicon.SetActive(FALSE);
- ::ShowWindow(m_otrayicon.GetOwnerWnd(), SW_SHOW);
- }
- }
- }
- }
- return 1;
- }
- void CServerDlg::OnTishowwnd()
- {
- this->m_otrayicon.SetActive(FALSE);
- }
- void CServerDlg::OnTiexit()
- {
- this->OnClosedirsvr();
- PostMessage(WM_CLOSE, 0, 0);
- }
- void CServerDlg::OnTiabout()
- {
- CAboutDlg about;
- about.DoModal();
- }
- void CServerDlg::OnTistartsvr()
- {
- this->OnOpendirsvr();
- }
- void CServerDlg::OnTiclosesvr()
- {
- this->OnClosedirsvr();
- }