
1 BOOL CFyToolTip::Create(CWnd* pWnd, BYTE bAlpha, DWORD dwStyle)
2 {
3 LPCTSTR lpszClassName = AfxRegisterWndClass(NULL);
4 BOOL bRet = CreateEx(WS_EX_TOOLWINDOW | WS_EX_TOPMOST, lpszClassName, _T(""), //WS_EX_TOOLWINDOW WS_EX_TOPMOST
5 dwStyle, 0, 0, 1, 1, pWnd->GetSafeHwnd(), NULL); // WS_POPUP WS_CHILD
6 if (bRet)
7 {
8 CSize sz(30,20);
9 MoveWindow(CRect(m_ptOrg, sz));
10 m_TipSize = sz;
11 }
12
13 LONG para = GetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE);
14 para |= WS_EX_LAYERED;
15 SetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE, para);
16 SetLayeredWindowAttributes(RGB(0, 0, 0), bAlpha, LWA_ALPHA); //LWA_ALPHA
17
18 m_parent = pWnd;
19 return bRet;
20 }
21
22 void CFyToolTip::OnPaint()
23 {
24 CPaintDC dc(this); // device context for painting
25
26 // TODO: Add your message handler code here
27 CRect rtWin;
28 GetClientRect(rtWin);
29 dc.FillSolidRect(rtWin, m_clrBack);
30 dc.SetBkMode(TRANSPARENT);
31 dc.Draw3dRect(rtWin, m_clrFrameColor, m_clrFrameColor);
32
33 CFont font;
34 font.CreatePointFont(m_fontSize * 10, m_fontName, NULL);
35 CFont* pOldFont = dc.SelectObject(&font);
36
37 CRect curLineRect;
38 if (!m_bMultLineTip)
39 {
40 CSize txtSize = dc.GetTextExtent(m_strTips); // 字串的长度像素
41 curLineRect.SetRect(rtWin.left + 0, 1, rtWin.right, txtSize.cy);
42 dc.SetTextColor(m_clrText);
43 dc.DrawText(m_strTips, rtWin, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
44 }
45 else
46 {
47 int offsetY = 1;
48 for (int i = 0; i < m_lineCount; i++)
49 {
50 CSize txtSize = dc.GetTextExtent(m_values[i]); // 字串的长度像素
51
52 dc.SetTextColor(m_colors[i]);
53 curLineRect.SetRect(rtWin.left + 4, offsetY, rtWin.right, offsetY + txtSize.cy);
54 dc.DrawText(m_values[i], curLineRect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
55
56 offsetY += txtSize.cy + 1;
57 }
58 }
59 dc.SelectObject(pOldFont);
60 }
61
62
63
64 void CFyToolTip::ShowTips(int nX, int nY, CString strTipsContent)
65 {
66 m_bMultLineTip = false;
67
68
69 m_strTips = strTipsContent;
70
71 //计算字符串长宽
72 HDC hDC = ::GetDC(this->m_hWnd);
73 CDC dc;
74 dc.Attach(hDC);
75
76 CFont font;
77 font.CreatePointFont(m_fontSize * 10, m_fontName, NULL);
78 CFont* pOldFont = dc.SelectObject(&font);
79 CSize txtSize = dc.GetTextExtent(m_strTips); // 字串的长度像素
80 dc.SelectObject(pOldFont);
81
82 //设置窗口位置大小
83 m_ptOrg = CPoint(nX, nY);
84 m_TipSize= txtSize;
85 MoveWindow(nX, nY, m_TipSize.cx, m_TipSize.cy, TRUE);
86 if (!IsWindowVisible())
87 {
88 ShowWindow(SW_SHOW);
89 }
90 Invalidate(TRUE);
91 }
92
93 void CFyToolTip::ShowXTips(int xCenter, int yTop, CString strTipsContent)
94 {
95 m_bMultLineTip = false;
96
97 m_strTips = strTipsContent;
98
99 //计算字符串长宽
100 HDC hDC = ::GetDC(this->m_hWnd);
101 CDC dc;
102 dc.Attach(hDC);
103
104 CFont font;
105 font.CreatePointFont(m_fontSize * 10, m_fontName, NULL);
106 CFont* pOldFont = dc.SelectObject(&font);
107 CSize txtSize = dc.GetTextExtent(m_strTips); // 字串的长度像素
108 dc.SelectObject(pOldFont);
109
110 //设置窗口位置大小
111 int nX = xCenter - txtSize.cx / 2;
112 m_ptOrg = CPoint(nX, yTop);
113 m_TipSize = txtSize;
114 MoveWindow(nX, yTop, m_TipSize.cx + 2, m_TipSize.cy, TRUE);
115 if (!IsWindowVisible())
116 {
117 ShowWindow(SW_SHOW);
118 }
119 Invalidate(TRUE);
120 }
121
122 void CFyToolTip::ShowY1Tips(int xRight, int yCenter, CString strTipsContent)
123 {
124 ......
125 }
126
127 void CFyToolTip::ShowY2Tips(int xLeft, int yCenter, CString strTipsContent)
128 {
129 ......
130 }
131
132 void CFyToolTip::ShowTips(CPoint& point, CRect& parentRect, CString* vals, int* colors, int count)
133 {
134 ......
135 }
136
137 void CFyToolTip::HideTips()
138 {
139 if (IsWindow(m_hWnd))
140 {
141 ShowWindow(SW_HIDE);
142 }
143 }
144
145
146 void CFyToolTip::SetBkColor(COLORREF clrBack)
147 {
148 m_clrBack = clrBack;
149 }
150
151 void CFyToolTip::SetFrameColor(COLORREF clrFrame)
152 {
153 m_clrFrameColor = clrFrame;
154 }
155
156 void CFyToolTip::SetTipTextColor(COLORREF clrText)
157 {
158 m_clrText = clrText;
159 }
160 .....

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用