One of my application is ported from windows Xp to WIN7. For this application tool tip control is not working in win7 while it is working for XP.
Code logic:
We are setting tooltip in a const string. aToolTipText =anImageTypeStr;
and passing in a function SetToolTipText(LPCTSTR tooltiptext, long Id) which is calling ActivateToolTipText(int Id, bool activateFlag) which is actually handling Tooltip based on id.
bool ActivateToolTipText(int Id, bool activateFlag)
{
CSA_TRY
{
// struct specifying info about tool in ToolTip control
TOOLINFO ti;
unsigned int uid = Id; // for ti initialization
LPTSTR lptstr = (LPTSTR)(LPCTSTR)m_strToolTipText[Id];
// CREATE A TOOLTIP WINDOW
if(activateFlag)
{
m_ToolTipHWND[Id] = CreateWindowEx(WS_EX_TOPMOST,
TOOLTIPS_CLASS,
NULL,
WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
m_hWnd,
NULL,
0,
NULL
);
}
// INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_SUBCLASS ;
ti.hwnd = m_hWnd;
ti.hinst = 0;
ti.uId = uid;
ti.lpszText = lptstr;
// ToolTip control will cover the rect of Id
CRect rect_out;
GetIdArea(Id, &rect_out);
ti.rect = rect_out;
if( activateFlag )
::SendMessage(m_ToolTipHWND[Id], TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO)&ti);
}
else
{
::SendMessage(m_ToolTipHWND[Id], TTM_DELTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
}
return TRUE;
}
}
The problem is with common controls version 6. for xp+, the header file CommCtrl.h assume we will use comctl version 6, but if we dont enable it explictly with manifest file, we'll still use the old comctl version 5.x. problem starts here, the size of TOOLINFO of version 5.x is different to version 6.x. So if you need to use comctl version 5 under windows xp+, you should init TOOLINFO with follwing code, TOOLINFO ti; ti.cbSize = sizeof(TOOLINFO) - 4;
Related
I am trying to find a way to show tooltip even if we disable window like this:
EnableWindow(hWnd, false);
Here is how I am creating tooltip:
void CreateTooltip(HWND hparent)
{
HWND hwndTT = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP, 0, 0, 0, 0, hparent, NULL, hInstance, NULL);
TTTOOLINFO ti = { 0 };
//ti.cbSize = sizeof(TTTOOLINFO);
//*********************************************************
// Specific settings for specific compiler options (Unicode/VC2013)
//*********************************************************
ti.cbSize = TTTOOLINFOW_V2_SIZE;
ti.uFlags = TTF_SUBCLASS;
ti.hwnd = hparent;
ti.lpszText = (LPWSTR)(L"Tooltip string");
GetClientRect(hparent, &ti.rect);
if (!SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM)&ti))
MessageBox(0, TEXT("Failed: TTM_ADDTOOL"), 0, 0);
}
I want to have two tooltips, one for enabled state and one for disabled.
Maybe there is a way to avoid this limitation? Thanks in advance.
CreateWindowEx API really posts WM_SIZE message?
When I create a window via CreateWindowEx as full screen mode,
CreateWindowEx posts WM_SIZE but window mode doesn't.
My code sets the window style like this :
if(bFullscr)
{
//When the window is in full screen mode.
nStyle = WS_POPUP;
nExtraStyle = WS_EX_APPWINDOW;
}
else
{
//Otherwise.
nStyle = WS_OVERLAPPEDWINDOW;
nExtraStyle = (WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
}
And changes display settings like this (full screen mode only) :
if(bFullscr)
{
DEVMODE sScrSet;
memset(&sScrSet, 0, sizeof(DEVMODE));
sScrSet.dmSize = sizeof(DEVMODE);
sScrSet.dmPelsWidth = nWidth;
sScrSet.dmPelsHeight = nHeight;
sScrSet.dmBitsPerPel = nColorBit;
sScrSet.dmFields = (DM_BITSPERPEL | DM_PELSHEIGHT | DM_PELSWIDTH);
if(ChangeDisplaySettings(&sScrSet, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
//Error routine.
}
}
I'm really wonder why CreateWindowEx posts WM_SIZE message selectively.
If you simply want to resize the window, somewhere in your code you should have ShowWindow(hWnd, nCmdShow); change it as follows:
ShowWindow(hWnd, SW_SHOWDEFAULT);//show normal
ShowWindow(hWnd, SW_SHOWMAXIMIZED);//show maximized (full screen)
SetWindowPos(hWnd, NULL, 10, 10, 300, 300, SWP_SHOWWINDOW);//show at specific position
Also you could use WS_MAXIMIZE in CreateWindow, but that could complicate things. Window usually has WS_OVERLAPPEDWINDOW or WS_POPUP|WS_CAPTION|WS_SYSMENU. You should pick one and keep it simple.
When Window size changes, it receives WM_SIZE, you can catch that and examine it.
I am registering my Class in the following method:
BOOL CNDSClientDlg::InitInstance()
{
//Register Window Updated on 16th Nov 2010, #Subhen
// Register our unique class name that we wish to use
WNDCLASS wndcls;
memset(&wndcls, 0, sizeof(WNDCLASS));
wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.hInstance = AfxGetInstanceHandle();
wndcls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndcls.lpszMenuName = NULL;
//Class name for using FindWindow later
wndcls.lpszClassName = _T("CNDSClientDlg");
// Register new class and exit if it fails
if(!AfxRegisterClass(&wndcls)) // [C]
{
return FALSE;
}
}
and then calling the InitInstance method and creating the window in constructor of the Class:
CNDSClientDlg::CNDSClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CNDSClientDlg::IDD, pParent)
{
InitInstance();
HWND hWnd;
hInst = AfxGetInstanceHandle(); // Store instance handle in our global variable
hWnd = CreateWindow(_T("CNDSClientDlg"), "NDS", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInst, NULL);
}
Now in my other application I am finding the window and trying to bring to top:
Edit
Able to bring newlyCreated Windows with below code
CWnd *pWndPrev = NULL;
CWnd *FirstChildhWnd = NULL;
pWndPrev = CWnd::FindWindow(_T("CNDSClientDlg"),NULL);
if(pWndPrev != NULL)
{
//pWndPrev->BringWindowToTop();
WINDOWPLACEMENT wndplacement;
pWndPrev->GetWindowPlacement(&wndplacement);
wndplacement.showCmd = SW_RESTORE;
pWndPrev->SetWindowPlacement(&wndplacement);
pWndPrev->SetForegroundWindow();
FirstChildhWnd = pWndPrev->GetLastActivePopup();
if (pWndPrev != FirstChildhWnd)
{
// a pop-up window is active, bring it to the top too
FirstChildhWnd->GetWindowPlacement(&wndplacement);
wndplacement.showCmd = SW_RESTORE;
FirstChildhWnd->SetWindowPlacement(&wndplacement);
FirstChildhWnd->SetForegroundWindow();
}
I am able to find the window as pWndPrev is not NULL , but It is not bringing up my application to front. Do I need to register any other class Instead of CNDSClientDlg. I want to bring my MFC application to top.
A few things to look at...
1) Try SetForegroundWindow() instead of BringWindowToTop(). It's been awhile since I've done Win32 programming, but I seem to recall that BringWindowToTop() has some limitations (especially when working with windows in different processes).
2) There are some rules that Microsoft put in place regarding SetForegroundWindow() starting with Windows 2000. The short version is that only the front-most application can change the foreground window. The idea is that an application that is not front-most cannot "jump in front of" the active application. If a background application calls SetForegroundWindow(), Windows will flash the taskbar button for the app, but will not actually bring the app to the front. The user must do that. I'm oversimplifying the rules, but this may be something to look at depending on your specific scenario.
BringWindowToTop() only works if the calling process is the foreground process or if it received the last input event.
Call CWnd::SetForegroundWindow() instead.
You may need to call AllowSetForegroundWindow in your "other" application before calling SetForegroundWindow.
That is assuming your other application is the foreground app and is trying to pass on its foreground status to the application with the window.
If neither app is the foreground app then you're not supposed to be able to bring a window to the front, although there are ways to do it (both accidentally and on purpose).
SetWindowPos(&wndTopMost, -1, -1, -1, -1, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
SetForegroundWindow();
I am trying to display unicode tooltips in my application window, however they do not seem to display. Non-unicode text shows up correctly but as soon as I try doing unicode no tooltip shows up. The following is what I am currently doing, any help is appreciated thank you.
HWND parentHwnd = pickInfo->getViewer().getCachedHwnd();
CWnd *pWnd = CWnd::FromHandlePermanent(parentHwnd);
HINSTANCE hInstance = GetModuleHandle(NULL);
if (isUnicode)
m_toolInfoW.lpszText = L"This tooltip does not show up at all.";
else
m_toolInfoA.lpszText = "Non unicode text";
if (!m_bTooltipInitialized){
::SendMessage(m_tooltipHwnd, WM_DESTROY, 0,0);
if(isUnicode)
m_tooltipHwnd = CreateWindowExW(WS_EX_TOPMOST,
TOOLTIPS_CLASSW, NULL,
WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
parentHwnd, NULL, hInstance, NULL);
else
m_tooltipHwnd = CreateWindowEx(WS_EX_TOPMOST,
TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
parentHwnd, NULL, hInstance, NULL);
if (GetLastError() != 0)
return;
::SetWindowPos(m_tooltipHwnd, HWND_TOPMOST,
0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
// Set the max text width before multi-line tooltip is used.
::SendMessage(m_tooltipHwnd, TTM_SETMAXTIPWIDTH, 0, m_nMaxWinTooltipWidth);
if (isUnicode){
m_toolInfoW.uFlags = TTF_SUBCLASS | TTF_IDISHWND | TTF_TRACK;
m_toolInfoW.hinst = hInstance;
m_toolInfoW.hwnd = parentHwnd;
m_toolInfoW.uId = (UINT_PTR)parentHwnd;
::GetClientRect (parentHwnd, &m_toolInfoW.rect);
::SendMessage(m_tooltipHwnd, TTM_ADDTOOLW, 0, (LPARAM) (LPTOOLINFOW) &m_toolInfoW);
::SendMessage(m_tooltipHwnd, TTM_ACTIVATE, TRUE, (LPARAM)(LPTOOLINFOW) &m_toolInfoW);
}
else{
m_toolInfoA.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
m_toolInfoA.hinst = hInstance;
m_toolInfoA.hwnd = parentHwnd;
m_toolInfoA.uId = (UINT_PTR)parentHwnd;
::GetClientRect (parentHwnd, &m_toolInfoA.rect);
::SendMessage(m_tooltipHwnd, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &m_toolInfoA);
::SendMessage(m_tooltipHwnd, TTM_ACTIVATE, TRUE, (LPARAM)(LPTOOLINFO) &m_toolInfoA);
}
m_bTooltipInitialized = true;
}
if (isUnicode)
::SendMessage(m_tooltipHwnd, TTM_UPDATETIPTEXTW, 0, (LPARAM) (LPTOOLINFOW) &m_toolInfoW);
else
::SendMessage(m_tooltipHwnd, TTM_UPDATETIPTEXT, 0, (LPARAM) (LPTOOLINFO) &m_toolInfoA);
//Repaint the screen so that the area beneath the previous location of the tooltip is restored correctly.
::UpdateWindow(pWnd->GetParentOwner()->GetSafeHwnd());
pWnd = NULL;
The problem is that you try to use common controls version 6, but you does not get to use it.
More in details,
typedef struct tagTOOLINFOW {
UINT cbSize;
UINT uFlags;
HWND hwnd;
UINT_PTR uId;
RECT rect;
HINSTANCE hinst;
LPWSTR lpszText;
LPARAM lParam;
#if (NTDDI_VERSION >= NTDDI_WINXP)
void *lpReserved;
#endif
} TTTOOLINFOW, NEAR *PTOOLINFOW, *LPTTTOOLINFOW;
for xp+, the header file CommCtrl.h assume you'll use comctl version 6, but if you does not enable it explictly with manifest file, you'll still use the old comctl version 5.x. Then here comes the problem, the size of TOOLINFO of version 5.x is different to version 6.x.
So if you need to use comctl version 5 under windows xp+, you should init TOOLINFO with follwing code,
TOOLINFO ti;
ti.cbSize = sizeof(TOOLINFO) - 4;
Otherwise, you should enable visual-style look with manifest file or prgram directive:
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
Finally, I'd recommand you always enable visual-look in xp+. Here are the comparision of visual effects:
Note: If you use ANSI/MBCS to compile the program, the sizeof(TOOLINFO) will be 48, which have already remove the lpReserved member. So ANSI version would works, but UNICODE would fail.
Good explanation and a solution that will work by Jichao above, but hard-wiring the size of the TOOLINFO structure will fix only the tooltips. If the problem is that the program was compiled with common controls 6.0+ in mind, but may be run on (say) a Windows XP system with 6.0+ either not installed, or not fully installed (like someone installed IE, but never used or updated it), then the more general solution is to restrict the application to using only 5.x common controls.
As can be seen here, there are more things that have structure size changes than just tooltips.
What I did to insure that everything would work on Windows XP is put the following at the very top of my program, before any includes (in the case of visual studio, a good place would be at the top of targetver.h if you have one):
#define _WIN32_WINNT 0x0500
In the Unicode case you have TTF_TRACK, which I believe requires you to manually show or hide the tooltip. In the ANSI case, you don't have that option.
http://msdn.microsoft.com/en-us/library/bb760252(VS.85).aspx
Scroll down to "Implementing Tracking Tooltips".
I created a simple class to hide the details of creating a toolbar in win32 API but I don't like the toolbars it is producing. (See image for clarification. I don't have reputation points so I have just posted a link)
http://i35.tinypic.com/1zmfeip.jpg
I have no idea now the black background is coming into my application.
Here is the class declaration in file CToolBar.h
#ifndef _CTOOLBAR_H
#define _CTOOLBAR_H
#include<windows.h>
#include<commctrl.h>
class CToolBar
{
public:
CToolBar();//constructor
~CToolBar();//destructor
void AddButton(int iconID, int command);//add Both a button, its icon and its command ID
void Show();//display the toolbar
void Initialise(HINSTANCE hInst, HWND hParent);
protected:
HINSTANCE m_hInst;
HWND m_hParent;
HWND m_hToolBar;
HIMAGELIST m_hImageList;
TBBUTTON m_Tbb[4]; //toolbar buttons
int m_numberButtons;
};
#endif
here is the implementation in file CToolBar.cpp
//CToolBar.cpp
#include "CToolBar.h"
#include<windows.h>
#include<commctrl.h>
CToolBar::CToolBar()//the constructor
{
m_hImageList=ImageList_Create(32, 32, ILC_COLOR32, 0, 15);//returns NULL if the function fails
//finish other initialisations
InitCommonControls();//initialise commctrl.dll whatever.. or else your toolbar wont appear
}
void CToolBar::Initialise(HINSTANCE hInst, HWND hParent)
{
m_hInst=hInst;
m_hParent=hParent;
m_hToolBar=CreateWindowEx(
WS_EX_PALETTEWINDOW ,
TOOLBARCLASSNAME,
"",
WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS |WS_VISIBLE|TBSTYLE_BUTTON | TBSTYLE_TOOLTIPS | CCS_ADJUSTABLE | CCS_TOP ,
0, 0,
0, 0,
m_hParent,
NULL,
m_hInst,
0);
}
CToolBar::~CToolBar()//destructor
{
ImageList_Destroy(m_hImageList);
}
void CToolBar::AddButton(int iconID, int command)
{
HICON hIcon = LoadIcon(m_hInst, MAKEINTRESOURCE(iconID));
ImageList_AddIcon(m_hImageList, hIcon);
DeleteObject(hIcon);
if(iconID!= -1)//-1 means the separator. The rest are mere buttons
{
m_Tbb[m_numberButtons].iBitmap =m_numberButtons;
m_Tbb[m_numberButtons].idCommand = command;
m_Tbb[m_numberButtons].fsState = TBSTATE_ENABLED;
m_Tbb[m_numberButtons].fsStyle = TBSTYLE_BUTTON;
m_Tbb[m_numberButtons].dwData = 0;
m_Tbb[m_numberButtons].iString = 0;
}
else//ie if (iconID== -1) ; then display the separator. the command value is ignored
{
m_Tbb[m_numberButtons].iBitmap =-1;
m_Tbb[m_numberButtons].idCommand = 0;
m_Tbb[m_numberButtons].fsState = TBSTATE_ENABLED;
m_Tbb[m_numberButtons].fsStyle = TBSTYLE_SEP;
m_Tbb[m_numberButtons].dwData = 0;
m_Tbb[m_numberButtons].iString = 0;
}
m_numberButtons++;
}
void CToolBar::Show()
{
SendMessage(m_hToolBar, TB_SETIMAGELIST , (WPARAM)0, (LPARAM)m_hImageList);
SendMessage(m_hToolBar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);//message for backward
//compatibility
SendMessage(m_hToolBar, TB_ADDBUTTONS, m_numberButtons, (LPARAM)m_Tbb);
SendMessage(m_hToolBar,WM_SIZE,0,0);
ShowWindow(m_hToolBar, SW_SHOW);
}
How i used the class
in main.cpp, i created a global instance of the class.
CToolBar myToolBar;
in the callback procedure, under WM_CREATE, I used some member functions.
case WM_CREATE:
myToolBar.Initialise(g_hInst,hwnd);
myToolBar.AddButton(IDI_OPEN, ID_OPEN);
myToolBar.AddButton(IDI_MAIN,ID_OPEN);//Separator button
myToolBar.AddButton(IDI_CLOSE, ID_CLOSE);
myToolBar.AddButton(IDI_CLOSEALL, ID_CLOSE);
myToolBar.Show();
break;
That's about it.
Try modifying the flags parameter of ImageList_Create to include ILC_MASK as well
Looks like you are using bitmap with transparency channel. GDI does not support alpha channel. It uses special color which will be transparent. If you want to support 32-bit bitmaps you could use GDI+ for drawing such bitmaps. Another option is to use CAplhaToolbar which already supports bitmaps with alpha transparency.