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

📄 notify.cpp

📁 C# Notify MFC C # Notify MFC
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*

  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  PARTICULAR PURPOSE.

  This is "Sample Code" and is distributable subject to the terms of the end user license agreement.

*/

#include "stdafx.h"
#include "notify.h"


//#define SetDlgItemText SetDlgItemTextW
//#define GetDlgItemText GetDlgItemTextW

// GUID for the app {85EE47B2-57D7-EEFE-8E7A-36480443D062}
static const GUID guidNotifyApp = 
{ 0x85ee47b2, 0x57d7, 0xEEFE, { 0x8e, 0x7a, 0x36, 0x48, 0x4, 0x43, 0xd0, 0x62 } };

// Global Variables:	
HINSTANCE			g_hInst;									// The current instance
HWND				g_hWnd;										// The main window handle
UINT				g_uiNotifyIDCount = 12540;					// id base for notify events
UINT				g_iIDCounter = 10000;
TCHAR				g_szCustomSecs[MAX_STRINGLEN];
TCHAR				g_szCustomTitle[MAX_STRINGLEN];
TCHAR				g_szCustomHTMLSampleText[MAX_MESSAGELEN];
SHNP				g_shnpNotifyType;
DWORD				g_dwCustomFlags;
int					g_iConvertedTime;
HWND				g_hDlg;
CPtrList*			g_pList;

static SHACTIVATEINFO s_sai;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK NotifyMain(
	HWND, 
	UINT, 
	WPARAM,
	LPARAM
	);

LRESULT CALLBACK CustomOptions(
	HWND, 
	UINT, 
	WPARAM, 
	LPARAM
	);

// Linked List Wrapper Functions
SHNOTIFICATIONDATA* CreateAndAddNotification();

BOOL RemoveNotification(
	DWORD dwID
	);

void RemoveNotificationAll();

int WINAPI WinMain(
	HINSTANCE hInstance, 
	HINSTANCE hPrevInstance, 
	LPTSTR lpCmdLine,	
	int nCmdShow
	)
{
	g_hInst = hInstance;

	SHInitExtraControls();

	// just call a dialog box, system will handle messaging, painting, etc...
	DialogBox(hInstance,(LPCTSTR)IDD_NOTIFYMAIN, NULL,(DLGPROC)NotifyMain); 
	return 0;
}

LRESULT CALLBACK NotifyMain(
	HWND hDlg, 
	UINT message,
	WPARAM wParam,
	LPARAM lParam
	)
{
	
	SHINITDLGINFO shidi = {0};
	SHMENUBARINFO shmbi = {0};
	SHNOTIFICATIONDATA shnd = {0};
	HICON hIcon = NULL;
	HWND hPriorityListCtrl = NULL;
	HWND hRemoveListCtrl = NULL;
	int iItemNumber = 0;
	int iRemoveID = 0;
	SHNP shnpNotifyType;
	SHNOTIFICATIONDATA* pCurrentNotification = NULL;
	int iCurrentItem = 0;
	TCHAR* pszID =(TCHAR*)malloc(MAX_STRINGLEN);
	
	hIcon =(HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_MESSAGE),
		IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	
	
	g_hDlg = hDlg;
	
	switch (message)
	{
	case WM_INITDIALOG:
		// Create a Done button and size it.  
		shidi.dwMask = SHIDIM_FLAGS;
		shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIZEDLGFULLSCREEN;
		shidi.hDlg = hDlg;
		SHInitDialog(&shidi);

		shmbi.cbSize = sizeof(shmbi);
		shmbi.hwndParent = hDlg;
		shmbi.nToolBarId = IDR_MENU_DATA;
		shmbi.hInstRes = g_hInst;
		SHCreateMenuBar(&shmbi);

		// get the combo box handle and add the priority items to it
		hPriorityListCtrl = GetDlgItem(hDlg, IDC_NOTIFICATIONPRIORITY);
		
		iCurrentItem = SendMessage(hPriorityListCtrl, CB_ADDSTRING, NULL,(LPARAM)(LPCTSTR)IDS_SHNP_INFORM);
		SendMessage(hPriorityListCtrl, CB_SETITEMDATA,(WPARAM)iCurrentItem,(LPARAM)(DWORD)(SHNP)SHNP_INFORM);  
		
		iCurrentItem = SendMessage(hPriorityListCtrl, CB_ADDSTRING, NULL,(LPARAM)(LPCTSTR)IDS_SHNP_ICONIC);
		SendMessage(hPriorityListCtrl, CB_SETITEMDATA,(WPARAM)iCurrentItem,(LPARAM)(DWORD)(SHNP)SHNP_ICONIC);  
		
		iCurrentItem = SendMessage(hPriorityListCtrl, CB_ADDSTRING, NULL,(LPARAM)(LPCTSTR)IDS_UPDATED);
		SendMessage(hPriorityListCtrl, CB_SETITEMDATA,(WPARAM)iCurrentItem,(LPARAM)(DWORD)(SHNP)UPDATE_NOTIFICATION);  
		
		iCurrentItem = SendMessage(hPriorityListCtrl, CB_ADDSTRING, NULL,(LPARAM)(LPCTSTR)IDS_CUSTOM);
		SendMessage(hPriorityListCtrl, CB_SETITEMDATA,(WPARAM)iCurrentItem,(LPARAM)(DWORD)(SHNP)CUSTOM_NOTIFICATION);  
		
		// set the inform item as the default
		SendMessage(hPriorityListCtrl, CB_SETCURSEL, 0, 0);
		
		// populate the text information box about the type of priority
		//LoadString(g_hInst, IDS_INFORM_MESSAGE, pszPriorityText, MAX_MESSAGELEN);
		SetDlgItemText(hDlg, IDC_INFOTEXT, IDS_INFORM_MESSAGE);
		
		// initilize the CPtrList object 
		g_pList = new CPtrList;
		
		break;
		
	case WM_MESSAGEBOX:
		MessageBox(NULL, TEXT("User Clicked OK."), TEXT("Notification Callback"), NULL);
		break;
		
	case WM_TIMER:
		// wParam is the pointer to the SHNOTIFICAITONDATA structure to update - using Update Notification to update the event
		SHNOTIFICATIONDATA* pUpdateNotification;
		pUpdateNotification =(SHNOTIFICATIONDATA*)wParam;
		pUpdateNotification->pszTitle = TEXT("Updated Title!");
		SHNotificationUpdate(SHNUM_TITLE, pUpdateNotification);
		break;

	case WM_DESTROY:
		free(pszID);
		RemoveNotificationAll();
		break;
		
	case WM_COMMAND:
			// when the user finishes selecting a priority from the combo box
			if (HIWORD(wParam)== CBN_SELENDOK)
			{
				// we update the text when the user finishes the selection of the priority of the message
				hPriorityListCtrl = GetDlgItem(hDlg, IDC_NOTIFICATIONPRIORITY);
				iItemNumber = SendMessage(hPriorityListCtrl, CB_GETCURSEL, 0, 0);
				shnpNotifyType =(SHNP)SendMessage(hPriorityListCtrl, CB_GETITEMDATA,(WPARAM)iItemNumber, 0);
				switch (shnpNotifyType)
				{
				case SHNP_ICONIC:
					SetDlgItemText(hDlg, IDC_INFOTEXT, IDS_ICONIC_MESSAGE);
					break;
				
				case UPDATE_NOTIFICATION:
					SetDlgItemText(hDlg, IDC_INFOTEXT, IDS_UPDATED_MESSAGE);
					break;
				
				case CUSTOM_NOTIFICATION:
					SetDlgItemText(hDlg, IDC_INFOTEXT, IDS_CUSTOM_MESSAGE);
					break;
				
				default:
					SetDlgItemText(hDlg, IDC_INFOTEXT, IDS_INFORM_MESSAGE);
					break;
				}
			}
			else
			{
				switch (LOWORD(wParam))
				{
					// if the user clicks add
				case IDC_ADD:
					// Get the Selected Priority for the Notification
					hPriorityListCtrl = GetDlgItem(hDlg, IDC_NOTIFICATIONPRIORITY);
					iItemNumber = SendMessage(hPriorityListCtrl, CB_GETCURSEL, 0, 0);
					shnpNotifyType =(SHNP)SendMessage(hPriorityListCtrl, CB_GETITEMDATA,(WPARAM)iItemNumber, 0);
					
					// create a node in the linked list for the notification
					pCurrentNotification = CreateAndAddNotification();
					// add the node to the listbox to show up in the display to remove(we know g_iIDCounter is the number)
					// we do not iconic notificaitons since they expire at preset times
					if (shnpNotifyType != SHNP_ICONIC)
					{
						wsprintf(pszID, TEXT("%i"), g_iIDCounter);
						hRemoveListCtrl = GetDlgItem(hDlg, IDC_REMOVELIST);
						iCurrentItem = SendMessage(hRemoveListCtrl, LB_ADDSTRING, NULL,(LPARAM)(LPCTSTR)pszID);
						SendMessage(hRemoveListCtrl, LB_SETITEMDATA,(WPARAM)iCurrentItem,(LPARAM)(DWORD)(UINT)g_iIDCounter); 
					}
					
					// This code is implemented this way to easily facilitate copying to other apps
					switch (shnpNotifyType)
					{
					case SHNP_ICONIC:
						shnd.dwID = g_iIDCounter;
						shnd.clsid = guidNotifyApp;
						shnd.npPriority = SHNP_ICONIC;
						shnd.csDuration = 10;
						shnd.hwndSink = hDlg;
						shnd.pszHTML = IDS_DEFAULTHTMLMESSAGE;
						shnd.hicon = hIcon;
						shnd.cbStruct = sizeof(SHNOTIFICATIONDATA);
						shnd.pszTitle = IDS_DEFAULTTITLE;
						shnd.grfFlags = NULL;
						
						g_iIDCounter++;
						SHNotificationAdd(&shnd);
						break;
					case UPDATE_NOTIFICATION:
						pCurrentNotification->dwID = g_iIDCounter;
						pCurrentNotification->clsid = guidNotifyApp;
						pCurrentNotification->npPriority = SHNP_INFORM;
						pCurrentNotification->csDuration = 20;
						pCurrentNotification->hwndSink = hDlg;
						pCurrentNotification->pszHTML = IDS_DEFAULTHTMLMESSAGE;
						pCurrentNotification->hicon = hIcon;
						pCurrentNotification->cbStruct = sizeof(SHNOTIFICATIONDATA);
						pCurrentNotification->pszTitle = IDS_DEFAULTTITLE;
						pCurrentNotification->grfFlags = NULL;
						
						g_iIDCounter++;
						SHNotificationAdd(pCurrentNotification);
						
						// start the timer for the updated notification in 20 secs
						// this will send a WM_TIMER message to the message queue after 20 seconds
						SetTimer(g_hDlg,(UINT)pCurrentNotification, 20000, NULL);

⌨️ 快捷键说明

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