⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wmsheet.cpp

📁 本人买的<<VC++项目开发实例>>源代码配套光盘.
💻 CPP
字号:
// WMSheet.cpp : implementation file
//

#include "stdafx.h"
#include "WMVC.h"
#include "WMSheet.h"
#include "Configration.h"
#include "HttpClient.h"		//for CHttpClient
#include "DownloadDlg.h"	//for CDownloadDlg
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CWMSheet

IMPLEMENT_DYNAMIC(CWMSheet, CPropertySheetEx)

CWMSheet::CWMSheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
	:CPropertySheetEx(nIDCaption, pParentWnd, iSelectPage)
{
	//给属性框加入图标
	m_hIcon = AfxGetApp()->LoadIcon(IDI_MAINICON);
	m_psh.dwFlags |= PSH_USEHICON | PSH_USEHBMHEADER;
	//注意必须要PropertySheet和PropertyPage中同时去除PSP_HASHELP才可以。
	m_psh.dwFlags &= ~PSH_HASHELP;
	//去掉Apply按钮
	m_psh.dwFlags |= PSH_NOAPPLYNOW;
	m_psh.hIcon = m_hIcon;	
	//通知区图标标志设置为 false
	//m_bTrayIconOn = false;
}

CWMSheet::CWMSheet(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
	:CPropertySheetEx(pszCaption, pParentWnd, iSelectPage)
{
}

CWMSheet::~CWMSheet()
{
}


BEGIN_MESSAGE_MAP(CWMSheet, CPropertySheetEx)
	//{{AFX_MSG_MAP(CWMSheet)
	ON_WM_CREATE()
	ON_COMMAND(ID_MENU_CHECKVERSION, OnMenuCheckVersion)
	ON_COMMAND(ID_MENU_ABOUT, OnMenuAbout)
	ON_WM_CHAR()
	ON_WM_SYSCOMMAND()
	ON_COMMAND(ID_TRAY_OPEN, OnTrayOpen)
	ON_COMMAND(ID_TRAY_EXIT, OnTrayExit)
	ON_COMMAND(ID_TRAY_ABOUT, OnTrayAbout)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_TRAYNOTIFY, OnTrayNotify)
	ON_MESSAGE(WM_HOTKEY, OnHotKey)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWMSheet message handlers

BOOL CWMSheet::OnInitDialog() 
{
	BOOL bResult = false;
	do
	{
		bResult = CPropertySheetEx::OnInitDialog();
	}while(false);	
	
	//加载菜单
	CMenu menuMain;
	menuMain.LoadMenu(IDR_MAINMENU);
	SetMenu(&menuMain);
	menuMain.Detach();
	
	//由于增加了菜单,所以必须要扩大属性框的大小
	CRect rectWnd;
	GetWindowRect(&rectWnd);
	//得到菜单栏的大小
	CSize sizeInflate(0
		, GetSystemMetrics(SM_CYMENUSIZE));
	rectWnd.right += sizeInflate.cx;
	rectWnd.bottom += sizeInflate.cy;
	MoveWindow(&rectWnd);

	//去除帮助按钮
	CWnd *pbtnHelp = GetDlgItem(IDHELP);
	pbtnHelp->ShowWindow(SW_HIDE);
	//隐藏确定按钮
	GetDlgItem(IDOK)->ShowWindow(SW_HIDE);
	//修改取消按钮的提示
	SetDlgItemText(IDCANCEL, "退出(&X)");	


	//改变Tab Control的大小
	//取得Tab Control的大小,然后再进行调整。
	CTabCtrl *pTabCtrl = this->GetTabControl();
	CRect rect;
	pTabCtrl->GetWindowRect(rect);
	rect.right -= 10;
	ScreenToClient(rect);
	//int nID = pTabCtrl->GetDlgCtrlID();

	//加载通知区菜单
	m_menuTray.LoadMenu(IDR_TRAYMENU);
	//将打开菜单项作为default的菜单项
	m_menuTray.GetSubMenu(0)->SetDefaultItem(ID_TRAY_OPEN, FALSE);

	//注册快捷键
	extern CConfigration config;
	BOOL bRst = RegisterHotKey(AfxGetMainWnd()->m_hWnd, IDHOTKEY, HIWORD(config.m_dwHotKey), LOWORD(config.m_dwHotKey));
	if (!bRst)
	{
		AfxMessageBox(" 注册快捷键失败!");
	}

	return bResult;
}

int CWMSheet::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CPropertySheetEx::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	//增加系统菜单和最小化对话框
	//注意:修改窗口的风格只能在OnCreate中,不能放置到
	//对话框的OnInitDialog()中。
	this->ModifyStyle(0, WS_MINIMIZEBOX);
	//禁止关闭
	//SetClassLong(this->m_hWnd, GCL_STYLE, CS_NOCLOSE);

	extern CConfigration config;
	//初始化通知区图标的数据
	ZeroMemory(&m_nid, sizeof(m_nid));
	m_nid.cbSize = sizeof(m_nid);
	m_nid.hIcon = AfxGetApp()->LoadIcon(IDI_MAINICON);
	m_nid.hWnd = m_hWnd;
	CString strTip = "五笔:快捷键";
	strTip += HIWORD(config.m_dwHotKey) & MOD_ALT ? "ALT + " : "";
	strTip += HIWORD(config.m_dwHotKey) & MOD_CONTROL ? "Ctrl + " : "";
	strTip += HIWORD(config.m_dwHotKey) & MOD_SHIFT ? "Shift + " : "";
	strTip += (char)LOWORD(config.m_dwHotKey);
	strcpy(m_nid.szTip, (const char *)strTip);
	m_nid.uCallbackMessage = WM_TRAYNOTIFY;
	m_nid.uFlags = NIF_TIP | NIF_ICON | NIF_MESSAGE;
	m_nid.uID = 1;	
	return 0;
}
#pragma message("在Tools->Options->Directories->Include Files加入UpgradeAtv.dll的路径。")
#import "UpgradeAtv.dll"		//for Client ActiveX
void CWMSheet::OnMenuCheckVersion() 
{
	extern CConfigration config;
	CString strVerOnServer;

	//初始化COM环境,为控件作准备
	::CoInitialize(NULL);
	CHttpClient httpClient;
	std::string *pstrResp = httpClient.GET("localhost", "80", "/version.asp", "");
	try
	{
		if ("" == *pstrResp)
		{
			throw new CString("请求连接出现问题,返回为\"\"。");
		}
		else
		{
			std::string strFlag = "Current version is:";
			std::string::size_type pos = pstrResp->find(strFlag);
			if (std::string::npos == pos)
			{
				throw new CString("服务器内部错误。");
			}
			//取得服务器上的版本号
			strVerOnServer = &pstrResp->c_str()[pos + strFlag.length()];
			if (strVerOnServer != config.m_strVersion)
			{
				CDownloadDlg dlg;
				dlg.DoModal();
			}
		}
	}
	catch(CString *e)
	{
		AfxMessageBox(*e);
		delete e;
	}
	catch(...)
	{
		AfxMessageBox("未知异常发生。");
		throw;
	}
	delete pstrResp;
	::CoUninitialize();
}

void CWMSheet::OnMenuAbout() 
{
	CString strCaption;
	strCaption.LoadString(AFX_IDS_APP_TITLE);
	CString strPrompt;
	strPrompt.LoadString(IDS_ABOUT_PROMPT);

	MSGBOXPARAMS paramMsg;
	ZeroMemory(&paramMsg, sizeof(paramMsg));
	paramMsg.cbSize = sizeof(paramMsg);
	paramMsg.dwContextHelpId = 0;
	paramMsg.dwLanguageId = MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED);
	paramMsg.dwStyle = MB_USERICON | MB_OK;
	paramMsg.hInstance = AfxGetInstanceHandle();
	paramMsg.hwndOwner = this->m_hWnd;
	paramMsg.lpfnMsgBoxCallback = NULL;
	paramMsg.lpszCaption = (const char *)strCaption;
	paramMsg.lpszIcon = MAKEINTRESOURCE(IDI_MAINICON);
	paramMsg.lpszText = (const char *)strPrompt;
	MessageBoxIndirect(&paramMsg);

}

void CWMSheet::OnOK()
{
	//PropertySheet的OnOK不被执行,而是直接调用
	//各个Page的OnOK
}

BOOL CWMSheet::PreTranslateMessage(MSG* pMsg) 
{
	//拦截 回车键,防止窗口关闭
	if (WM_KEYDOWN == pMsg->message &&  VK_RETURN == pMsg->wParam)
	{
		::TranslateMessage(pMsg);
		::DispatchMessage(pMsg);
		return TRUE;
	}
	//拦截 ESC键,防止窗口关闭
	//
	if (WM_KEYDOWN == pMsg->message && VK_ESCAPE == pMsg->wParam)
	{
		//ESC隐藏窗口。
		PostMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
		::TranslateMessage(pMsg);
		::DispatchMessage(pMsg);
		return TRUE;
	}
	return CPropertySheetEx::PreTranslateMessage(pMsg);
}

void CWMSheet::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	TRACE("Char\n");
	CPropertySheetEx::OnChar(nChar, nRepCnt, nFlags);
}

void CWMSheet::OnSysCommand(UINT nID, LPARAM lParam) 
{
	extern CConfigration config;
	if (SC_MINIMIZE == nID )
	{
		//加入通知区图标
		Shell_NotifyIcon(NIM_ADD, &m_nid);
		//隐藏窗口
		ShowWindow(SW_HIDE);
	}
	else if (SC_CLOSE == nID)
	{
		if (!config.m_dwStopPromptMin)
		{
			int nRsp = AfxMessageBox("关闭动作使得窗口最小化到通知区。\r\n"
				"要退出程序,"
				"请使用退出按钮或者通知区菜单的退出项。\r\n"
				"下次继续提示吗?", MB_YESNO | MB_ICONQUESTION);
			if (IDNO == nRsp)
			{
				config.m_dwStopPromptMin = 1;
			}
		}
		PostMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
	}
	else
	{
		CPropertySheetEx::OnSysCommand(nID, lParam);
	}
}

LRESULT CWMSheet::OnTrayNotify(WPARAM wParam, LPARAM lParam)
{
	if (WM_CONTEXTMENU == lParam || WM_RBUTTONDOWN == lParam)
	{
		//Microsoft's bug, so must call SetForegroundWindow()
		CPoint pt;
		::GetCursorPos(&pt);
		SetForegroundWindow();
		m_menuTray.GetSubMenu(0)->TrackPopupMenu(0, pt.x, pt.y, this);
	}
	else if(WM_LBUTTONDBLCLK == lParam)
	{
		ShowWindow(SW_SHOW);
		SetForegroundWindow();
		//删除图标
		Shell_NotifyIcon(NIM_DELETE, &m_nid);
	}
	return 0;
}

void CWMSheet::OnTrayOpen() 
{
	PostMessage(WM_TRAYNOTIFY, 0, WM_LBUTTONDBLCLK);	
}

void CWMSheet::OnTrayExit() 
{
	SendMessage(WM_TRAYNOTIFY, 0, WM_LBUTTONDBLCLK);
	CPropertySheetEx::EndDialog(IDCANCEL);
}

void CWMSheet::OnTrayAbout() 
{
	PostMessage(WM_COMMAND, ID_MENU_ABOUT, 0);	
}

LRESULT CWMSheet::OnHotKey(WPARAM wParam, LPARAM lParam)
{
	if (IDHOTKEY == wParam)
	{
		if (!IsWindowVisible())
		{
			//模拟通知区双击,唤醒窗口
			SendMessage(WM_TRAYNOTIFY, 0, WM_LBUTTONDBLCLK);
		}
		else
		{
			ShowWindow(SW_SHOW);
			SetForegroundWindow();
		}		
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -