📄 hiddenwnd.cpp
字号:
// HiddenWnd.cpp : implementation of the CHiddenWindow class
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"
#include "HiddenWnd.h"
#include "FindNote.h"
// Standard shift-operator overload, which takes CNote as a parameter
ostream& operator<<(ostream& os, CNote n)
{
ATLTRACE(_T("operator<<()\n"));
os << "[NOTE]" << endl;
os << "[NOTETEXT]:" << n.m_strNoteText << endl;
os << "[FONTNAME]:" << n.m_strFontName << endl;
os << "[FONTHEIGHT]:" << n.m_lFontHeight << endl;
os << "[FONTWEIGHT]:" << n.m_lWeight << endl;
os << "[FONTITALIC]:" << n.m_bItalic << endl;
os << "[TIMESTAMP]:" << n.m_strTimestamp << endl;
os << "[BKGNDCOLOR]:" << n.m_clrBkgnd << endl;
os << "[NOTEID]:" << n.m_nID << endl;
os << "[ENDNOTE]" << endl;
return os;
}
CHiddenWindow::CHiddenWindow()
{
ATLTRACE(_T("CHiddenWindow::CHiddenWindow()\n"));
m_pSearchDlg = NULL;
}
CHiddenWindow::~CHiddenWindow()
{
ATLTRACE(_T("CHiddenWindow::~CHiddenWindow()\n"));
m_pSearchDlg = NULL;
// Destroy the menu
if (m_PopupMenu.m_hMenu)
m_PopupMenu.DestroyMenu();
}
LRESULT CHiddenWindow::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
ATLTRACE(_T("CHiddenWindow::OnCreate()\n"));
// Load the 'Notes' icon from the application's resources
m_hIconNotes = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDI_TRAYNOTE),
IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
ATLASSERT(m_hIconNotes);
memset(&m_nid, 0, sizeof(NOTIFYICONDATA));
m_nid.cbSize = sizeof(NOTIFYICONDATA);
// Create a name of a file where the application keeps saved notes.
m_strFileName = GetFileName();
// Get all saved notes from the file
LoadNotes();
return 0;
}
// This message is sent by the system when the user chooses to end the session or
// when any application calls the ExitWindows function.
// Note: If any application returns zero, the session is not ended.
// The system stops sending WM_QUERYENDSESSION messages as soon as one application returns zero.
LRESULT CHiddenWindow::OnEndSession(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
ATLTRACE(_T("CHiddenWindow::OnEndSession()\n"));
// I do not care if the user is logging off or shutting down the system
// Persist saved notes to the file
SaveNotes();
// Delete the icon from the taskbar status area
if (!RemoveIconFromTaskBar(m_hWnd, IDI_TRAYNOTE))
{
ATLTRACE(_T("Icon was not removed from the taskbar!\n"));
// Even in case of an error it's OK to end the Windows session
return TRUE;
}
// It's OK to end the Windows session
return TRUE;
}
LRESULT CHiddenWindow::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
ATLTRACE(_T("CHiddenWindow::OnDestroy()\n"));
// Persist saved notes to the file
SaveNotes();
// Delete the icon from the taskbar status area
if (!RemoveIconFromTaskBar(m_hWnd, IDI_TRAYNOTE))
{
ATLTRACE(_T("Icon was not removed from the taskbar!\n"));
return 0;
}
::PostQuitMessage(0);
return 0;
}
// Adds an icon to the taskbar status area.
// Returns TRUE if successful, or FALSE otherwise.
// hwnd - handle to the window to receive callback messages.
// uID - identifier of the icon.
// hicon - handle to the icon to add.
// lpszTip - ToolTip text.
BOOL CHiddenWindow::AddIconToTaskBar(HWND hwnd, UINT uID, HICON hicon, LPSTR lpszTip)
{
ATLTRACE(_T("CHiddenWindow::AddIconToTaskBar()\n"));
BOOL bRes;
m_nid.hWnd = hwnd;
m_nid.uID = uID;
m_nid.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
m_nid.uCallbackMessage = WMU_NOTIFYICON;
m_nid.hIcon = hicon;
if (lpszTip)
lstrcpyn(m_nid.szTip, lpszTip, sizeof(m_nid.szTip));
else
m_nid.szTip[0] = _T('\0');
bRes = Shell_NotifyIcon(NIM_ADD, &m_nid);
if (hicon)
DestroyIcon(hicon);
return bRes;
}
// Deletes an icon from the taskbar status area.
// Returns TRUE if successful, or FALSE otherwise.
// hwnd - handle to the window that added the icon.
// uID - identifier of the icon to delete.
BOOL CHiddenWindow::RemoveIconFromTaskBar(HWND hwnd, UINT uID)
{
ATLTRACE(_T("CHiddenWindow::RemoveIconFromTaskBar()\n"));
BOOL bRes;
m_nid.hWnd = hwnd;
m_nid.uID = uID;
bRes = Shell_NotifyIcon(NIM_DELETE, &m_nid);
return bRes;
}
// Handles tray notification message
LRESULT CHiddenWindow::OnNotifyIcon(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
ATLTRACE(_T("CHiddenWindow::OnNotifyIcon()\n"));
// Determine the tray message
switch (lParam)
{
// Right mouse button click - display a popup menu
case WM_RBUTTONUP:
DisplayShortcutMenu();
break;
// Left mouse button double-click - create a new note
case WM_LBUTTONDBLCLK:
CreateNewNote();
break;
default:
break;
}
return 0;
}
// Displays a notify icon's shortcut menu
LRESULT CHiddenWindow::DisplayShortcutMenu()
{
ATLTRACE(_T("CHiddenWindow::DisplayShortcutMenu()\n"));
// Hide the search dialog if it exists
if (m_pSearchDlg && ::IsWindow(m_pSearchDlg->m_hWnd))
m_pSearchDlg->ShowWindow(SW_HIDE);
// Load the menu resource
if (!m_PopupMenu.LoadMenu(IDR_TRAYMENU))
{
ATLTRACE(_T("Menu was not loaded!\n"));
return 0;
}
// Get cursor's position
POINT pt;
::GetCursorPos(&pt);
// Display a shortcut menu at the specified location
// See "PRB: Menus for Notification Icons Don't Work Correctly" in MSDN
::SetForegroundWindow(m_nid.hWnd);
// TrackPopupMenu cannot display the menu bar so get
// a handle to the first shortcut menu
CMenuHandle menuTrackPopup = m_PopupMenu.GetSubMenu(0);
// Display the shortcut menu. Track the right mouse button
if (!menuTrackPopup.TrackPopupMenu(TPM_RIGHTALIGN|TPM_RIGHTBUTTON, pt.x, pt.y, m_hWnd, NULL))
{
ATLTRACE(_T("Shortcut menu was not displayed!\n"));
return 0;
}
::PostMessage(m_nid.hWnd, WM_NULL, 0, 0);
// Destroy the menu and free any memory that the menu occupies
menuTrackPopup.DestroyMenu();
m_PopupMenu.DestroyMenu();
m_PopupMenu.m_hMenu = NULL;
return 0;
}
// Creates a brand new note
LRESULT CHiddenWindow::CreateNewNote()
{
ATLTRACE(_T("CHiddenWindow::CreateNewNote()\n"));
// Create a new Note dialog
CNoteDlg * pDlgNote = NULL;
pDlgNote = new CNoteDlg(this);
// Check if 'new' succeeded and we have a valid pointer to a dialog object
if (pDlgNote != NULL)
{
if (pDlgNote->Create(m_hWnd) == NULL) // create failed
{
ATLTRACE(_T("Note dialog creation failed!\n"));
return -1;
}
pDlgNote->ShowWindow(SW_SHOW);
}
else
{
ATLTRACE(_T("Dialog object creation failed!\n"));
return -1;
}
return 0;
}
// Handles selecting 'New Note' item from the menu
LRESULT CHiddenWindow::OnNewNote(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
ATLTRACE(_T("CHiddenWindow::OnNewNote()\n"));
if (CreateNewNote() == -1)
ATLTRACE(_T("New Note creation failed!\n"));
return 0;
}
// Handles selecting 'Search' item from the menu
LRESULT CHiddenWindow::OnSearch(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
ATLTRACE(_T("CHiddenWindow::OnSearch()\n"));
// If a search dialog already exists then show it, do not create a new one
if (m_pSearchDlg && ::IsWindow(m_pSearchDlg->m_hWnd))
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -