第二十八篇 -- 写一个简陋的WIFI服务器界面
效果图:
Dlg.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// WIFIWMITestDlg.cpp : implementation file // #include "stdafx.h" #include "WIFIWMITest.h" #include "WIFIWMITestDlg.h" #include "afxdialogex.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CAboutDlg dialog used for App About class CAboutDlg : public CDialogEx { public: CAboutDlg(); // Dialog Data #ifdef AFX_DESIGN_TIME enum { IDD = IDD_ABOUTBOX }; #endif protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // Implementation protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) END_MESSAGE_MAP() // CWIFIWMITestDlg dialog CWIFIWMITestDlg::CWIFIWMITestDlg(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_WIFIWMITEST_DIALOG, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CWIFIWMITestDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_STATIC8, Icon_Static); DDX_Control(pDX, IDC_STATIC9, Icon_Static_Green); DDX_Control(pDX, IDC_STATIC1, Server_Status); } BEGIN_MESSAGE_MAP(CWIFIWMITestDlg, CDialogEx) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_COMMAND(ID_SERVER_START, &CWIFIWMITestDlg::OnServerStart) ON_COMMAND(ID_SERVER_STOP, &CWIFIWMITestDlg::OnServerStop) ON_UPDATE_COMMAND_UI(ID_SERVER_STOP, &CWIFIWMITestDlg::OnUpdateServerStop) END_MESSAGE_MAP() // CWIFIWMITestDlg message handlers BOOL CWIFIWMITestDlg::OnInitDialog() { CDialogEx::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 != nullptr) { BOOL bNameValid; CString strAboutMenu; bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); ASSERT(bNameValid); 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 //添加menu菜单 m_Menu.LoadMenu(IDR_MENU1); // IDR_MENU1 SetMenu(&m_Menu); //使菜单变灰 m_Menu.GetSubMenu(0)->EnableMenuItem(ID_SERVER_STOP, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED); //添加红绿灰三种图标 DWORD dwStyle = WS_VISIBLE | WS_CHILD | SS_ICON; ::SetWindowLong(Icon_Static.m_hWnd, GWL_STYLE, dwStyle); ::SetWindowLong(Icon_Static_Green.m_hWnd, GWL_STYLE, dwStyle); //HICON hIcon = ::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_ICON1)); //((CStatic *)GetDlgItem(IDC_STATIC5))->SetIcon(hIcon); HICON hIcon_red = (HICON)::LoadImage(AfxGetInstanceHandle(), 1 ? MAKEINTRESOURCE(IDI_ICON1) : MAKEINTRESOURCE(IDI_ICON2), IMAGE_ICON, 16, 16, LR_SHARED); HICON hIcon_gray = (HICON)::LoadImage(AfxGetInstanceHandle(), 0 ? MAKEINTRESOURCE(IDI_ICON1) : MAKEINTRESOURCE(IDI_ICON3), IMAGE_ICON, 16, 16, LR_SHARED); ((CStatic *)GetDlgItem(IDC_STATIC8))->SetIcon(hIcon_red); ((CStatic *)GetDlgItem(IDC_STATIC9))->SetIcon(hIcon_gray); return TRUE; // return TRUE unless you set the focus to a control } void CWIFIWMITestDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialogEx::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 CWIFIWMITestDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 { CDialogEx::OnPaint(); } } // The system calls this function to obtain the cursor to display while the user drags // the minimized window. HCURSOR CWIFIWMITestDlg::OnQueryDragIcon() { return static_cast<HCURSOR>(m_hIcon); } void CWIFIWMITestDlg::OnServerStart() { // TODO: Add your command handler code here CWnd* pWnd = GetDlgItem(IDC_STATIC1); pWnd->SetWindowText(_T(" Server is online!")); HICON hIcon_green = (HICON)::LoadImage(AfxGetInstanceHandle(), 0 ? MAKEINTRESOURCE(IDI_ICON1) : MAKEINTRESOURCE(IDI_ICON2), IMAGE_ICON, 16, 16, LR_SHARED); ((CStatic *)GetDlgItem(IDC_STATIC9))->SetIcon(hIcon_green); HICON hIcon_gray = (HICON)::LoadImage(AfxGetInstanceHandle(), 0 ? MAKEINTRESOURCE(IDI_ICON1) : MAKEINTRESOURCE(IDI_ICON3), IMAGE_ICON, 16, 16, LR_SHARED); ((CStatic *)GetDlgItem(IDC_STATIC8))->SetIcon(hIcon_gray); //恢复菜单变亮 m_Menu.GetSubMenu(0)->EnableMenuItem(ID_SERVER_STOP, MF_BYCOMMAND | MF_ENABLED); //使菜单变灰 m_Menu.GetSubMenu(0)->EnableMenuItem(ID_SERVER_START, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED); //MessageBox((LPCTSTR)"Server Ready!"); } void CWIFIWMITestDlg::OnServerStop() { // TODO: Add your command handler code here CWnd* pWnd = GetDlgItem(IDC_STATIC1); pWnd->SetWindowText(_T(" Server is offline!")); HICON hIcon_red = (HICON)::LoadImage(AfxGetInstanceHandle(), 1 ? MAKEINTRESOURCE(IDI_ICON1) : MAKEINTRESOURCE(IDI_ICON2), IMAGE_ICON, 16, 16, LR_SHARED); ((CStatic *)GetDlgItem(IDC_STATIC8))->SetIcon(hIcon_red); HICON hIcon_gray = (HICON)::LoadImage(AfxGetInstanceHandle(), 0 ? MAKEINTRESOURCE(IDI_ICON1) : MAKEINTRESOURCE(IDI_ICON3), IMAGE_ICON, 16, 16, LR_SHARED); ((CStatic *)GetDlgItem(IDC_STATIC9))->SetIcon(hIcon_gray); //恢复菜单变亮 m_Menu.GetSubMenu(0)->EnableMenuItem(ID_SERVER_START, MF_BYCOMMAND | MF_ENABLED); //使菜单变灰 m_Menu.GetSubMenu(0)->EnableMenuItem(ID_SERVER_STOP, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED); } void CWIFIWMITestDlg::OnUpdateServerStop(CCmdUI *pCmdUI) { // TODO: Add your command update UI handler code here }
Dlg.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// WIFIWMITestDlg.h : header file // #pragma once // CWIFIWMITestDlg dialog class CWIFIWMITestDlg : public CDialogEx { // Construction public: CWIFIWMITestDlg(CWnd* pParent = nullptr); // standard constructor // Dialog Data #ifdef AFX_DESIGN_TIME enum { IDD = IDD_WIFIWMITEST_DIALOG }; #endif protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // Implementation protected: HICON m_hIcon; // Generated message map functions virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); DECLARE_MESSAGE_MAP() public: CMenu m_Menu; afx_msg void OnServerStart(); CStatic Icon_Static; CStatic Icon_Static_Green; CStatic Server_Status; afx_msg void OnServerStop(); afx_msg void OnUpdateServerStop(CCmdUI *pCmdUI); };
菜单的模样
分隔符这个问题,看下面
将下拉菜单要添加分隔符的地方,把Separator设置为True就行了。