I've been working on a modal dialog using the Windows Template Library where I need to dynamically create buttons and add tool-tips to them. I can't seem to find the correct way to do this:
// CMyDialog.h
#pragma once
#include "stdafx.h"
class CMyDialog : public CDialogImpl<CIdleDialog> {
public:
enum { IDD = IDD_IDLEDIALOG };
protected:
BEGIN_MSG_MAP(CMyDialog)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
END_MSG_MAP()
LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam,
BOOL &bHandled)
{
CRect dialog_rect(0, 0, 600, 400);
MoveWindow(&dialog_rect);
CenterWindow();
CButton btn;
CRect btn_rect(10, 10, 200, 30);
btn.Create(this->m_hWnd, btn_rect, L"Test Button",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_FLAT, NULL,
BTN_ID_OFFSET + 1);
CToolTipCtrl tooltip;
tooltip.Create(this->m_hWnd, rcDefault, NULL,
TTS_BALLOON | TTS_NOPREFIX);
TOOLINFO ti = {sizeof(ti)};
ti.hwnd = this->m_hWnd;
ti.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
ti.uId = (UINT_PTR)btn.m_hWnd;
ti.lpszText = (LPWSTR)L"THIS IS A TOOLTIP";
if (!tooltip.AddTool(&ti)) {
ocdbg("Could not add tooltip to button.\n");
}
tooltip.Activate(TRUE);
return TRUE;
}
};
For how I run this dialog:
// main.cpp
#include "stdafx.h"
#include "CMyDialog.h"
CAppModule _Module;
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
_In_ PWSTR pCmdLine, _In_ int nCmdShow)
{
AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES);
HRESULT h_res = _Module.Init(NULL, hInstance);
{
CMyDialog dialog;
dialog.DoModal();
}
_Module.Term();
return 0;
}
The call to tooltip.AddTool(&ti) returns 0 which notes a failure. I'm assuming this is because the button is not in my resource.h file. Any ideas?
I have solved this by following this article https://learn.microsoft.com/en-us/windows/win32/controls/cookbook-overview and adding this to the top of my dialog's header.
Everything is working fine now!