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

📄 hiddenwnd.cpp

📁 一个提示软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		m_pSearchDlg->ShowWindow(SW_SHOW);
		return 0;
	}

	// If a search dialog does not exist then create a new one
	m_pSearchDlg = new CSearchDlg(this);

	// Check if 'new' succeeded and we have a valid pointer to a dialog object
	if (m_pSearchDlg != NULL)
	{
		if (m_pSearchDlg->Create(m_hWnd) == NULL)	// create failed
		{
			ATLTRACE(_T("Search dialog creation failed!\n"));
			return 0;
		}
		m_pSearchDlg->ShowWindow(SW_SHOW);
	}
	else
		ATLTRACE(_T("Search dialog object creation failed!\n"));

	return 0;
}

// Handles selecting 'About' item from the menu
LRESULT CHiddenWindow::OnAbout(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	ATLTRACE(_T("CHiddenWindow::OnAbout()\n"));

	CAboutDlg dlg;
	dlg.DoModal();
	return 0;
}

// Handles selecting 'Exit' item from the menu
LRESULT CHiddenWindow::OnExit(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	ATLTRACE(_T("CHiddenWindow::OnExit()\n"));

	// Destroy the hidden window
	DestroyWindow();
	return 0;
}

// Reads the file and populates the vector which'll contain the list of all saved notes
bool CHiddenWindow::LoadNotes()
{
	ATLTRACE(_T("CHiddenWindow::LoadNotesFromFile()\n"));

	// Lets check if the file name is valid
	if (m_strFileName.length() == 0)
	{
		MessageBox(_T("Could not retrieve the file name.\nCould not load notes!"),
					_T("Sticky Notes - Error"), MB_OK|MB_ICONEXCLAMATION);
		return false;
	}

	// Open a file which contain all saved notes
	CNotesArchive NotesArchive(m_strFileName.c_str(), ios::in | ios::out | ios::app);
	if (!NotesArchive.IsOpen())
	{
		MessageBox(_T("The file to load notes from was not opened.\nCould not load notes!"),
					_T("Sticky Notes - Error"), MB_OK|MB_ICONEXCLAMATION);
		return false;
	}

	NotesArchive.LoadNotesFromFile(&m_vecNotes);

	return true;
}

// Writes vector containing list of saved notes to the file
bool CHiddenWindow::SaveNotes()
{
	ATLTRACE(_T("CHiddenWindow::SaveNotesToFile()\n"));

	// Lets check if the file name is valid
	if (m_strFileName.length() == 0)
	{
		MessageBox(_T("Could not retrieve the file name.\nCould not save notes!"),
					_T("Sticky Notes - Error"), MB_OK|MB_ICONEXCLAMATION);
		return false;
	}

	// Open a file and delete the old one (if it already exists)
	CNotesArchive NotesArchive(m_strFileName.c_str(), ios::in | ios::out | ios::trunc);
	if (!NotesArchive.IsOpen())
	{
		MessageBox(_T("The file to save notes was not opened.\nCould not save notes!"),
					_T("Sticky Notes - Error"), MB_OK|MB_ICONEXCLAMATION);
		return false;
	}

	NotesArchive.SaveNotesToFile(m_vecNotes);

	return true;
}

// Handler for WMU_DISPLAYNOTE message.
// WPARAM contains ID of the note which was selected in the search dialog.
// When the hidden window receives this message it'll search for this note and display it.
LRESULT CHiddenWindow::OnDisplayNote(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	ATLTRACE(_T("CHiddenWindow::OnDisplayNote()\n"));

	DWORD dwID = (DWORD)wParam;		// note's id

	vector<CNote> vecResult;		// vector to hold the search matches

	// Create the function object
	CFindNoteById findNotes(dwID, &vecResult);

	// Iterate over the contents of the vector.
	// Instance of CFindNoteById class is passed to the 'for_each()' algorithm as its
	// third argument and its operator() is called for each element in the vector.
	// Each element's m_nID value is searched for the specified integer.
	for_each(m_vecNotes.begin(), m_vecNotes.end(), findNotes);

	// Since I'm searching by unique id it can be only one match (I hope)
	if (vecResult.size() != 0)
	{
		// Create a new Note dialog
		CNoteDlg * pDlgNote = NULL;
		pDlgNote = new CNoteDlg(this, vecResult[0]);

		// 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 0;
			}
			pDlgNote->ShowWindow(SW_SHOW);
			pDlgNote->UnselectText();
		}
		else
			ATLTRACE(_T("Dialog object creation failed!\n"));
	}
	else
		MessageBox(_T("This note was deleted!"), APP_NAME, MB_OK);

	return 0;
}

// Creates a full path of the file where the application keeps saved notes.
string CHiddenWindow::GetFileName()
{
	ATLTRACE(_T("CHiddenWindow::GetFileName()\n"));

	string strFile = DEFAULT_FILE_NAME;
	HANDLE hToken = NULL;
	TCHAR szPath[_MAX_PATH];
	DWORD dwPathSize = sizeof(szPath)/sizeof(TCHAR);
	TCHAR szUserName[UNLEN + 1];
	DWORD dwUserNameSize = sizeof(szUserName)/sizeof(TCHAR);

	typedef BOOL (__stdcall *PFUNCOPENPROCESSTOKEN)(HANDLE, DWORD, HANDLE*); 
	PFUNCOPENPROCESSTOKEN pfOpenProcessToken;

	typedef BOOL (__stdcall *PFUNCGETUSERPROFILEDIR)(HANDLE, LPTSTR, DWORD*); 
	PFUNCGETUSERPROFILEDIR pfGetUserProfileDir;  

	// If Win95, 98 or Me is running the file will be created in the same 
	// directory where executable is located since only one user can use
	// the system.
	// If WinNT, 2000, XP or higher is running the file will be created
	// in the root directory of the specified user's profile, i.e.
	// C:\Documents and Settings\administrator on Win2000

	// Load the Advapi32.dll
	HMODULE hAdvapi = ::LoadLibrary("Advapi32.dll");
    if (hAdvapi == NULL) 
	{
		ATLTRACE(_T("LoadLibrary(Advapi32.dll) failed!\n"));
		return strFile;
	}

	// Try to get the OpenProcessToken function address.
	pfOpenProcessToken = 
		(PFUNCOPENPROCESSTOKEN)::GetProcAddress(hAdvapi, "OpenProcessToken"); 
	if (pfOpenProcessToken == NULL)
	{
		ATLTRACE(_T("GetProcAddress(hAdvapi, OpenProcessToken) failed!\n"));
		// Free the Advapi32.dll.
		::FreeLibrary(hAdvapi); 
		return strFile;
	}

	// Load the Userenv.dll
	HMODULE hUserenv = ::LoadLibrary("Userenv.dll");
    if (hUserenv == NULL) 
	{
		ATLTRACE(_T("LoadLibrary(Userenv.dll) failed!\n"));
		// Free the Advapi32.dll
		::FreeLibrary(hAdvapi); 
		return strFile;
	}

	// Try to get the GetUserProfileDirectory function address
#ifdef _UNICODE
	pfGetUserProfileDir = 
		(PFUNCGETUSERPROFILEDIR)::GetProcAddress(hUserenv, "GetUserProfileDirectoryW");
#else 
	pfGetUserProfileDir = 
		(PFUNCGETUSERPROFILEDIR)::GetProcAddress(hUserenv, "GetUserProfileDirectoryA");
#endif /* _UNICODE */

	if (pfGetUserProfileDir == NULL)
	{
		ATLTRACE(_T("GetProcAddress(hUserenv, GetUserProfileDirectory) failed!\n"));
		// Free the Advapi32.dll.
		::FreeLibrary(hAdvapi);
		// Free the Userenv.dll. 
		::FreeLibrary(hUserenv);
		return strFile;
	}

	// Open the access token associated with a process
	if (!(pfOpenProcessToken)(GetCurrentProcess(), TOKEN_QUERY, &hToken))
	{
		ATLTRACE(_T("OpenProcessToken() failed!\n"));
		// Free the Advapi32.dll.
		::FreeLibrary(hAdvapi); 
		// Free the Userenv.dll.
		::FreeLibrary(hUserenv); 
		return strFile;
	}

	// Retrieve the path to the root directory of the specified user's profile
	if (!(pfGetUserProfileDir)(hToken, szPath, &dwPathSize))
	{
		ATLTRACE(_T("GetUserProfileDirectory() failed!\n"));
		if (hToken)
			::CloseHandle(hToken);
		// Free the Advapi32.dll.
		::FreeLibrary(hAdvapi); 
		// Free the Userenv.dll.
		::FreeLibrary(hUserenv); 
		return strFile;
	}

	// Retrieve the user name of the current thread. 
	// This is the name of the user currently logged onto the system. 
	if (!::GetUserName(szUserName, &dwUserNameSize))
	{
		ATLTRACE(_T("GetUserName() failed!\n"));
		if (hToken)
			::CloseHandle(hToken);
		// Free the Advapi32.dll.
		::FreeLibrary(hAdvapi); 
		// Free the Userenv.dll.
		::FreeLibrary(hUserenv); 
		return strFile;
	}

	// Construct a fully qualified file name.
	// The file name is created by concatenating the user name and the default file name
	strFile = string(szPath) + string(_T("\\")) + string(szUserName) + 
			string(_T("_")) + string(DEFAULT_FILE_NAME);

	if (hToken)
		::CloseHandle(hToken);

	// Free the Advapi32.dll.
    ::FreeLibrary(hAdvapi); 
		
	// Free the Userenv.dll.
    ::FreeLibrary(hUserenv); 

	return strFile;
}

⌨️ 快捷键说明

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