Add a bubble prompt on the menu bar

xiaoxiao2021-03-06  37

This function is derived from an example "Tooltips for Menu Item and Popup MenuItem", in fact, the steps of the example are very simple, but I don't plan to translate it. Mr. Lin Yutang is not said: "I only use the same thing, no Understand the truth, it is really not high. " The core of this feature is that the author has a MenutooltiP class, 182 line code implements the function of the menu prompt. Here I explain it, and there is a specific explanation after each line code. #ifndef _MENU_TOOLTIP // conditional compilation statements to determine whether the definition of the _MENU_TOOLTI the following macro #define _MENU_TOOLTIP // define some constants identifier #ifndef TTS_NOANIMATE #define TTS_NOANIMATE 0x10 #define TTS_NOFADE 0x20 #define TTS_BALLOON 0x40 #define TTS_CLOSE 0x80

#define ttm_settitlea (WM_USER 32) // wparam = tti_ *, lparam = char * sztitle #define TTM_SETTILEW (WM_USER 33) // wparam = tti_ *, lparam = wchar * sztitle

#ifdef unicode #define TTM_SETTILE TTM_SETTITLEW #Else #define TTM_SETTILE TTM_SETTITLEA # Endif # ENDIF

Class cmenutooltip {

Public: cmenutooltip (): m_htooltip (0), m_hparent (0) {}

// Create a menu item related tips and void Create (HWND hParent, LPCTSTR sczTipText, HINSTANCE hInstance = NULL, DWORD dwStyle = 0, LPCTSTR sczTipTitle = NULL); This function is called void OnMenuSelect (UINT nItemID // trigger when WM_MENUSELECT news, UINT NFLAGS, HMENU HSUBMENU;

/ / Display or hide the function void showtooltip (bool bshow) {ToolInfo Ti; // ToolInfo is a structure / / / / below to initialize some of the variables in this structure Ti.cbsize = Sizeof (ToolInfo); // Set the size of the structure Ti.uflags = TTF_IDishWnd; // Indicates that the UID member is the handle Ti.hWnd = m_hparent; / / contains the handle of the prompt Ti.uID = (uint) m_hparent; // application definition identification Message // Message Send the prompt :: SendMessage (m_htooltip, ttm_trackactivate, (wparam) BSHOW, (LPARAM) & Ti);}

// Setting the position of the prompt Void SetTooltipPosition (HMENU HMENU, UINT NITEMID) {Rect RT = {0, 0, 0, 0}; // Find item Rectangle and position // Based on the number of menu items complete the following loop for (int NItem = 0; NITEM <:: getMenuItemcount (HMENU); NITEM ) {uint cmd = :: getMenuItemID (HMENU, NITEM); // Put the identifier of the current menu item in a variable // If the current menu item is selected Set menu item, get the area IF (cmd == nitemid) {:: getMenuItemRect (M_HParent, HMENU, NITEM, & RT);}} // sends a message to set the location of the display: SendMessage (M_HTooltip) , Ttm_trackposition, 0, (lparam) makelparam (rt.right, rt.bottom 2)); // Tips to display at the top floor, otherwise the prompt arrow will appear under the menu, try seeing: -> :: setwindowpos M_HTooltip, hwnd_topmost, 0,0,0,0, swp_nosize | swp_noactivate | swp_nomove;} // Update prompt information void UpdateTooltiptext (lpctstr szbuff, hinstance hinstance = 0) {

TOOLINFO ti; ti.cbSize = sizeof (TOOLINFO); ti.uFlags = TTF_IDISHWND; ti.hwnd = m_hParent; ti.uId = (UINT) m_hParent; ti.hinst = hInstance; ti.lpszText = const_cast (szBuff) ; // Update the content // Send Update message: SendMessage (m_htooltip, ttm_updatetiptext, (wparam) 0, (lparam) & ti);} // Overload Handle Operator HWnd () {Return M_HTOOLTIP;}

Private: hwnd m_htooltip; hwnd m_hparent; tchar m_szdefault; // stores the string Hinstance M_HINSTANCE (}) of default prompt;

The following inline functions inline // Create a prompt control void CMenuToolTip :: Create (HWND hParent, LPCTSTR sczTipText, HINSTANCE hInstance, DWORD dwStyle, LPCTSTR sczTipTitle) {INITCOMMONCONTROLSEX icex; // registered public control class in the dynamic link library TOOLINFO common controls in Ti; // load the tooltip class from the dll. Icex.dwsize = sizeof (cex); ICEX.DWICC = ICC_BAR_CLASS; // Returns if (! INITCOMMONTROLSEX (& Icex)) return;

m_hParent = hParent; m_hInstance = hInstance; // create a prompt control m_hToolTip = :: CreateWindow (TOOLTIPS_CLASS, TEXT ( ""), WS_POPUP | dwStyle, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, (HMENU) NULL, hInstance, NULL) ; // Some of the initial parameters of the control Ti.cbsize = sizeof (ToolInfo); Ti.uflags = TTF_IDishWnd | TTF_TRANSPARENT | TTF_TRACK | TTF_ABSOLUTE; // TTF_CENTIP TI.HWND = HParent; Ti.uID = (uint) hParent; TI. Hinst = hinstance; ti.lpsztext = const_cast (scztiptext); if (scztiptext! = lpstr_textCallback) {// The following setting default prompt text if (scztiptext) {// If there is a prompt text if the current menu is prompting, Store to m_szdefault _tcscpy (m_szdefault, scztiptext);} else {// If there is no prompt text, display the following string _tcscpy (m_szdefault, _t ("no text associated");}}

Ti.Rect.Left = Ti.Rect.top = Ti.Rect.Bottom = Ti.Rect.Right = 0;

// Add the prompt to the control :: SendMessage (m_htooltip, ttm_addtool, 0, (lparam) & ti);

:: SendMessage (M_HTooltip, TTM_SETMAXTIPWIDTH, 0, 300); if (scztitle) {:: SendMessage (m_htooltip, ttm_settitle, 1, (lparam) scztiPtitle);}}

Inline // The following operation of the inline function is set to the operation void cfertooltip :: OnMenuSelect (uint nitemid, uint nflags, hmenu hsubmenu) {if (nflags & mf_popup || (nflags == 0xfffff && hSubMenu == NULL)) {// Do not prompt showtooltip (false) when the menu is closed;}

IF (! (NFLAGS & MF_POPUP)) {// Setting the location settooltipPosition (HSubMenu, NItemID);

/ / Update the prompt text TCHAR SZBUFF [256]; / / Defines the character array SZBUFF [0] = 0 of the storage prompt text; // store the specified prompt text into a preset buffer in an int NRET = :: LoadString (M_HINSTANCE, NITEMID, SZBUFF, 256); # if 0 for (INT i = 0; i

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

New Post(0)