[C] BalloonTip (2)

Source: Internet
Author: User

I have written an article in the past. I used a self-drawn window in C # To create a Balloon Tip. Here, because we are talking about BalloonTip, it will be followed by the previous article. The difference is that this time a ToolTip window is directly created using the windows sdk API function, and then we get the ToolTip window handle, it is easy to locate the ToolTip to any position on the screen. I used the previous demo of the MSN-style pop-up notification window, but added the ToolTip code. The effect is as follows:

 

The code is relatively simple. For details, see tooltip. h and tooltip. c in the attachment.

CreateWindow is used to create a tooltip window, and the window class name is "Tooltips_Class32 ". The first parameter isBalloon specifies the shape of the ToolTip to be created. If it is TRUE, a "balloon" ToolTip is created, which is similar to a session bubble in a cartoon image, the position coordinates (x, y) when the bubble is displayed are the coordinates of the arrow pointing to the point. Otherwise, a normal rectangular ToolTip is created. The position coordinates (x, y) when displayed are the coordinates of an endpoint of the ToolTip window (such as the upper left corner.

Code_createmediltip
// Create a ToolTip window
HWND CreateToopTip (BOOL isBalloon, int maxWidth)
{
If (! M_hWndToolTip)
{
M_hWndToolTip = CreateWindow (
"Tooltips_Class32", // TOOLTIPS_CLASS,
NULL,
WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP | (isBalloon? TTS_BALLOON: 0 ),
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL,
NULL, // m_hInst,
NULL );

If (m_hWndToolTip)
{
// Set the version so we can have non buggy mouse event forwarding
// SendMessage (m_hWndToolTip, CCM_SETVERSION, COMCTL32_VERSION, 0 );
SendMessage (m_hWndToolTip, TTM_SETMAXTIPWIDTH, 0, (LPARAM) maxWidth );
}
}
Return m_hWndToolTip;
}

 

 

When a ToolTip is displayed, you can set the title, text, and position by sending a message prefixed with "TTM _" of the ToolTip to the window. Then, we use a timer to set the display time of the ToolTip on the screen.

Code_ShowToolTip
// Tooltip is displayed.
Void ShowToolTip (HWND hWnd, char * content, char * title, int x, int y, int icon, int mSeconds)
{
// Obtain the foreground window
HWND m_curForeWnd = GetForegroundWindow ();

If (! M_hWndToolTip)
{
// Balloontip by default
CreateToopTip (TRUE, 300 );
}

If (m_hWndToolTip)
{
TOOLINFO ti = {0 };
Ti. cbSize = sizeof (ti );
Ti. uFlags =/* TTF_IDISHWND | */TTF_TRACK | TTF_TRANSPARENT/* | TTF_CENTERTIP */;
Ti. hwnd = hWnd;
Ti. uId = 0;
// Ti. lpszText = NULL;
SendMessage (m_hWndToolTip, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) & ti );
// Hide tooltip
SendMessage (m_hWndToolTip, TTM_TRACKACTIVATE, (WPARAM) FALSE, (LPARAM) 0 );

// Set text
Ti. lpszText = content;
SendMessage (m_hWndToolTip, TTM_UPDATETIPTEXT, 0, (LPARAM) & ti );

// Set the title and icon
SendMessage (m_hWndToolTip, TTM_SETTITLEA, icon, (LPARAM) title );

// Set the pointing position
SendMessage (m_hWndToolTip, TTM_TRACKPOSITION, 0, MAKELONG (x, y ));

// Set z-order
SetWindowPos (m_hWndToolTip, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE );

// Display tooltip
SendMessage (m_hWndToolTip, TTM_TRACKACTIVATE, (WPARAM) TRUE, (LPARAM) & ti );

// Install the timeout Timer
KillTimer (hWnd, TIMERID_TOOLTIP );
If (mSeconds> 0)
{
SetTimer (hWnd, TIMERID_TOOLTIP, mSeconds, (TIMERPROC) ToolTipTimerProc );
}
}
// Restore the foreground window
SetForegroundWindow (m_curForeWnd );
}

 

 

Finally, the source code download link (consistent with the previous download link ):

Http://files.cnblogs.com/hoodlum1980/NotifyWndDemo2.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.