Also talk about the production system tray program

xiaoxiao2021-04-10  514

1. Description of class CSYSTEMTRAY: (The final article of the article has the specific implementation code)

Csystemtray is a COBJECT extension class that implements the following features:

1. Display the tray icon at the task bar.

2, set the prompt TIP

3, set the shape of the icon

Main function description:

Create (CWND * PWND, UINT UCALLBACKMESSAGE, LPCTSTSTSZTIP, HICON ICON, UINT UID);

Features:

Generate an icon.

Parameter Description:

PWND: The main window of the program,

UcallbackMessage: Corresponding message mapping,

Sztip: Tips when the mouse is staying,

Icon: Displayed icon,

UID: Menu ID with the corresponding

· BOOL CSYSTEMTRAY :: seticon (HiCon Hicon)

Bool Csystemtray :: seticon (lpctstr lpsziconname)

Bool Csystemtray :: seticon (uint nidresource)

Bool Csystemtray :: setStandardon (LPCTSTSTANDONNAME)

Bool Csystemtray :: setStandardon (uint nidresource)

Function: Change the icon on the tray.

Void csystemtray :: showicon ()

Function: Display icon.

· BOOL CSYSTEMTRAY :: SetTooltiptext (LPCTSTR PSZTIP)

Bool Csystemtray :: setTooltiptext (uint nid)

Function: Tip shows text.

2. The steps to use this class are as follows:

Step 1: In the VC becoming an environment, create an engineering, dialog, or single document (or multi-document) you casually, all options are default.

Step 2: Define variables Csystemtray M_Trayicon in Mainfrm.h; and add a function declaration:

AFX_MSG LRESULT ONTRAYNOTIFICATION (WPARAM WPARAM, LPARAM LPARAM);

AFX_MSG Void OnSysCommand (Uint Nid, LParam Lparam);

Step 3: Define messages in Staafx.h:

#define WM_ICON_NOTIFY WM_USER 1

Step 4: Custom menu IDR_POPUPMENU, where least includes one: id: id_view_main_window, caption is "Show Main Window" and add message mapping functions, Command, and Update_command_ui to this item in class CMAINFRAME.

Step 5: Add the following in mainframe.cpp:

1. Begin_MESSAGE_MAP (CMAINFRAME, CFRAMEWND)

ON_COMMAND (ID_View_main_window, onviewmainwindow)

ON_UPDATE_COMMAND_UI (ID_View_main_window, onupdateviewmainwindow)

ON_MESSAGE (WM_ICON_NOTIFY, ONTRAYNOTIFICATION)

ON_WM_SYSCOMMAND ()

//}} AFX_MSG_MAP

END_MESSAGE_MAP ()

2. Void CMAINFRAME :: OnViewMainWindow () {

IF (iswindowvisible ())

{

ShowWindow (sw_showminimized);

ShowWindow (SW_HIDE);

m_trayicon.seticon (IDI_ICON1);

}

Else

{

ShowWindow;

ShowWindow (sw_restore);

m_trayicon.seticon (IDI_ICON2);

}

}

Void CMAINFRAME :: OnUpdateViewMainWindow (ccmdui * pcmdui)

{

PCMDUI-> SetCheck (iswindowvisible ());

}

3. Int cmainframe :: oncreate (lpcreatestruct lpcreatestruct)

{

IF (cframewnd :: oncreate (lpcreatestruct) == -1)

Return -1;

// Create a tray icon

IF (! m_trayicon.create (this, wm_icon_notify, "big screen real-time display", null, idR_popupmenu))

Return -1;

m_trayicon.seticon (IDI_ICON1);

SetMenu (NULL);

Return 0;

}

4. LRESULT CMAINFRAME :: OntrayNotification (WPARAM WPARAM, LPARAM LPARAM)

{

IF (WPARAM! = IDR_POPUPMENU)

Return 0L;

Cmenu Menu, * psubmenu;

IF (Loword (LPARAM) == WM_RBUTTONUP)

{

Cpoint Pos;

GetCursorpos (& POS);

IF (! menu.loadmenu) Return 0;

IF (! (psubmenu = menu.getsubmenu (0))) Return 0;

:: SetmenudeFaultItem (psubmenu-> m_hmenu, 3, true);

SetForegroundWindow ();

Psubmenu-> TrackPopupmenu (TPM_RightAlign | TPM_BOTTOMALIGN | TPM_RightButton, Pos.x, Pos.y,

THIS);

Menu.destroymenu ();

}

Else

IF (Loword (LPARAM) == WM_LBUTTONDBLCLK)

{

IF (! menu.loadmenu) Return 0;

IF (! (psubmenu = menu.getsubmenu (0))) Return 0;

SetForegroundWindow ();

/ / Activate the second menu item

SendMessage (wm_command, psubmenu-> getmenuitemid (1), 0);

Menu.destroymenu ();

}

Return 0;

}

Void CMAINFRAME :: OnSysCommand (uint nid, lparam lparam)

{

IF (NID == SC_MINIMIZE)

{

ShowWindow (sw_showminimized);

ShowWindow (SW_HIDE);

}

Else

CframeWnd :: OnSysCommand (NID, LPARAM);

}

3. Appendix: Class implementation code

Csystemtray header file

#ifndef _included_systemtray_h_

#define _included_systemtray_h_ / /

// Csystemtray Window

Class Csystemtray: Public COBJECT

{

// construction / destruction

PUBLIC:

Csystemtray ();

Csystemtray (CWND * PWND, UINT UCALLBACKMESSAGE, LPCTSTSTSZTIP, HICON ICON, UINT UID);

Virtual ~ csystemtray ();

// Operations

PUBLIC:

CframeWnd * m_pframe;

Bool enabled () {return m_benabled;}

Bool visible () {return! M_bhidden;

// Create the TRY icon

Create (CWND * PWND, UINT UCALLBACKMESSAGE, LPCTSTSTSZTIP, HICON ICON, UINT UID);

// Change or Retrieve the Tooltip Text

Bool SetTooltiptext (lpctstr psztooltiptext);

Bool SetTooltiptext (Uint Nid);

CSTRING GETTOOLTIPTEXT () Const;

// Change or Retrieve the icon displayed

Bool seticon (HiCon Hicon);

Bool Seticon (LPCTSTR LPICONNAME);

Bool seticon (uint nidResource);

Bool setstandardicon (LPCTSTR LPICONNAME);

Bool setstandardicon (uint nidResource);

HiCon geticon () const;

Void hideicon ();

Void showicon ();

Void Removeicon ();

Void movetoright ();

// Change or Retrieve The Window to send Notification Messages to

Bool setNotificationWnd (CWND * PNOTIFYWND);

CWND * getNotificationWnd () const;

// default handler for tray notification message

// Overrides

// ClassWizard Generated Virtual Function Overrides

// {{AFX_VIRTUAL (CSYSTEMTRAY)

//}} AFX_VIRTUAL

// Implementation

protected:

BOOL M_BENABLED; / / DOES O / S Support TRAY icon?

BOOL M_BHIDDEN; // Has The icon been hidden?

Notifyicondata M_TND;

Declare_dynamic (Csystemtray)

}

#ENDIF

///

Csystemtray implementation file

#include "stdafx.h"

#include "systemtray.h"

#ifdef _Debug

#define new debug_new

#undef this_file

Static char this_file [] = __file__;

#ENDIF

Implement_dynamic (Csystemtray, CObject)

/

// csystemtray construction / code / destruction

Csystemtray :: csystemtray () {

MEMSET (& M_TND, 0, SIZEOF (M_TND));

m_benabled = false;

m_bhidden = false;

}

Csystemtray :: Csystemtray (CWND * PWND, UINT UCALLBACKMESSAGE, LPCTSTSTSTOOLTIP, HICON ICON, UINT UID)

{

Create (PWND, UcallbackMessage, Sztooltip, Icon, UID);

m_bhidden = false;

}

Bool Csystemtray :: Create (CWND * PWND, UINT UCALLBACKMESSAGE, LPCTSTSTSTOOLTIP, HICON ICON, UINT UID)

{

// this is only for Windows 95 (or Higher)

Verify (m_benabled = (getversion () & 0xff)> = 4);

IF (! m_benabled) Return False;

// Make Sure Notification Window Is Valid

Verify (m_benabled = (PWND && :: Iswindow (pWnd-> getsafehwnd ()))))));

IF (! m_benabled) Return False;

// make Sure We Avoid Conflict with Other Messages

Assert (UcallbackMessage> = WM_USER);

// TRAY ONLY Supports Tooltip Text Up To 64 Characters

Assert (_TCSLEN (SZTooltip <= 64);

// load up the Notifyicondata Structure

m_tnd.cbsize = sizeof (notifyicondata);

m_tnd.hwnd = pWnd-> getsafehwnd ();

m_tnd.uid = uid;

m_tnd.hicon = icon;

M_TND.uflags = Nif_Message | NIF_ICON | NIF_TIP;

m_tnd.ucallbackMessage = ucallbackmessage;

STRCPY (M_TND.SZTIP, SZTooltip);

// set the TRAY icon

m_pframe = (cframewnd *) PWND;

Verify (m_benabled = shell_notifyicon (nim_add, & m_tnd));

Return M_Benabled;

}

Csystemtray :: ~ csystemtray ()

{

REMOVECICON ();

}

/

// csystemtray icon manipulation

Void csystemtray :: movetorage ()

{

Hideicon ();

SHOWICON ();

}

Void Csystemtray :: removeicon ()

{

IF (! m_benabled) return;

m_tnd.uflags = 0;

Shell_notifyicon (Nim_Delete, & M_TND);

m_benabled = false;

}

Void csystemtray :: hideicon ()

{

IF (m_benabled &&! m_bhidden) {

m_tnd.uflags = nif_icon; shell_notifyicon (nim_delete, & m_tnd);

m_bhidden = true;

}

}

Void csystemtray :: showicon ()

{

IF (m_benabled && m_bhidden) {

M_TND.uflags = Nif_Message | NIF_ICON | NIF_TIP;

Shell_notifyicon (NIM_ADD, & M_TND);

m_bhidden = false;

}

}

Bool Csystemtray :: seticon (HiCon Hicon)

{

IF (! m_benabled) Return False;

m_tnd.uflags = nif_icon;

m_tnd.hicon = hic;

Return shell_notifyicon (nim_modify, & m_tnd);

}

Bool Csystemtray :: seticon (lpctstr lpsziconname)

{

Hicon Hicon = AFXGetApp () -> Loadicon (LPSZICONNAME);

Return Seticon (HICON);

}

Bool Csystemtray :: seticon (uint nidresource)

{

Hicon Hicon = AFXGetApp () -> Loadicon (NidResource);

Return Seticon (HICON);

}

Bool Csystemtray :: setStandardon (LPCTSTSTANDONNAME)

{

Hicon Hicon = Loadicon (NULL, LPICONNAME);

Return Seticon (HICON);

}

Bool Csystemtray :: setStandardon (uint nidresource)

{

Hicon Hicon = :: loadicon (AfxgetInstanceHandle (), makeintResource (NidResource));

Return Seticon (HICON);

}

Hicon csystemtray :: geticon () const

{

Hicon Hicon = NULL;

IF (m_benable)

Hicon = m_tnd.hicon;

Return Hicon;

}

//

// Csystemtray TooltiP Text Manipulation

Bool Csystemtray :: SetTooltiptext (LPCTSTR PSZTIP)

{

IF (! m_benabled) Return False;

m_tnd.uflags = nif_tip;

_TCSCPY (m_tnd.sztip, psztip);

Return shell_notifyicon (nim_modify, & m_tnd);

}

Bool Csystemtray :: setTooltiptext (uint nid)

{

CString strText;

Verify (STRTEXT.LOADSTRING (NID));

Return SetTooltiptext (strtext);

}

CString Csystemtray :: gettooltiptext () const () const

{

CString strText;

IF (m_benable)

strText = m_tnd.sztip;

Return strText;

}

// csystemtray notification window stuff

Bool Csystemtray :: setNotificationWnd (CWND * PWND) {

IF (! m_benabled) Return False;

// Make Sure Notification Window Is Valid

Assert (PWND && :: Iswindow (pwnd-> getsafehwnd ()));

m_tnd.hwnd = pWnd-> getsafehwnd ();

m_tnd.uflags = 0;

Return shell_notifyicon (nim_modify, & m_tnd);

}

CWnd * csystemtray :: getNotificationWnd () const

{

Return CWnd :: fromHandle (m_tnd.hwnd);

}

转载请注明原文地址:https://www.9cbs.com/read-133354.html

New Post(0)