How to add a prompt for any control and region: CTIP class packaged

zhaozj2021-02-08  236

There is a ctooltipCtrl class in the MFC. I want everyone to know that I have to add a prompt to specific controls or regions on ActiveX Control in a project. I started using CTooltiPCtrl, but I found that it didn't work at all. Helpless, I have written myself. A class to implement it.

This class is CTIP (including ctip.c and ctip.h)

You only need to add it in your project, you can use it.

Steps for usage:

1. CREATE (CWND * PParentWnd) during initialization.

2. Tune setText (const cstring & rstext) in Mosemove (CPOINT POINT) to set the text you want to display.

3. Tune Show (CPOINT POINT) in Mosemove (CPOINT POINT).

Instructions for use:

1.Create is used to create a window with Createex. The size of this window is 0, the title is empty, the style is WS_POPUP & BrVBARWS_CHILD & BrVBARWS_CLIPSIBLINGS

2. There is a trick here, you have to know which range when your mouse is located, so you can have two options: one, you specify the range directly (set up a create) or add a member Variables are dynamically recorded; two, you can call as follows:

CRECT RECT;

CWND * PWND = getdlgitem (id_xxx);

PWND-> GetWindowRect (& Rect);

ScreenToClient (& Re);

This can also get the current RECT. Then you can call as follows:

IF (Rect.ptinRect (Point))

{

m_tip.settext (m_varname);

m_tip.show (point);

}

Else

{

m_tip.close ();

}

3.SHOW and Close are used to display and hide prompts, mainly using CDCs in SHOW to draw programs.

Several major functions specific code:

Bool CTooltip2 :: Create (CWND * PParentWnd)

{

Assert (this! = Null);

Assert (pParentWnd! = NULL);

m_pparentwnd = pparentwnd;

// Create Font

CRECT RectinitialSize (0,0,0,0); // Initial window size. Will Be Dynamically

Return Createex (NULL, NULL, NULL, WS_POPUP | WS_CHILD | WS_CLIPSIBLINGS,

RectInitialSize, PparentWnd, Null, NULL

}

BOOL CTooltip2 :: Show (const cpoint & rurrentpoint)

{

Assert (this! = Null);

Assert (m_hwnd! = Null);

// Is Text Empty or Tool Tip already displayed?

IF (m_sztext.isempty () || m_bshowstatus)

Return False;

m_ptcurrent = rcurrentpoint;

M_BSHOWSTATUS = True;

// Show Tool TIP

DisplayTooltip (rcurrentpoint);

Return True;

}

Void CTooltip2 :: DisplayTooltip (Const Cpoint & RcurrentPoint)

{

CDC * PDC = Getdc ();

CBRUSH * POLDBRUSH;

CFont * PoldFont;

PoldFont = PDC-> SelectObject (& m_font);

CSIZE SIZE = PDC-> GetTextExtent; PDC-> LPTODP (& size);

// Form TooltIP Rectangle

CRECT RectTooltip (rcurrentpoint.x, rcurrentpoint.y,

RcurrentPoint.x size.cx 7, rcurrentpoint.y size.cy 2);

// Draw TooltIP Rect and Text

PDC-> setbkmode (transparent);

Cbrush Brushtooltip (GetsysColor (Color_Infobk);

Poldbrush = PDC-> SelectObject (& Brushtooltip);

// Create and SELECT Thick Black Pen

CPEN Penblack (PS_SOLID, 0, ColorRef (RGB (0, 0, 0));

CPEN * POLDPEN = PDC-> SELECTOBJECT (& Penblack);

// Draw Rectangle Filled with Color_Infobk

PDC-> Rectangle (0, 0, RectTooltip.width (), RectTooltip.Height ());

// Draw Tooltip Text

PDC-> setTextColor (getsyscolor (color_infotext)); // Tool Tip Color Set in

// Control Panel Settings

PDC-> setTextAlign (TA_LEFT);

PDC-> Textout (3, 1, m_sztext);

CRECT RECTWND = RectTooltip;

m_pparentWnd-> ClientToscreen (RectWnd); // Convert from Client To Screen

Cpoint pttooltipleft = RectWnd.Topleft ();

// Now Display Tooltip

SetWindowPos (& Wndtop, PtTooltiPLEFT.X 1, PTTooltiPLEFT.Y 1, RectWnd.Width (),

RectWnd.Height (), SWP_SHOWINDOW & Brvbarswp_noownerzorder & brvbarswp_noactivate;

// Put Back Old Objects

PDC-> SELECTOBJECT (POLDBRUSH);

PDC-> SELECTOBJECT (POLDPEN);

PDC-> SELECTOBJECT (POLDFONT);

ReleaseDC (PDC);

}

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

New Post(0)