|
如下代码实现的功能:当把鼠标指向ID为IDC_BUTTON_TEST的按钮时,会出现提示信息,也就是ToolTip功能。代码很简单,对话框资源就是一个对话框和对话框上的一个按钮。
这样的功能,相同的代码,在EVC(用的是WCE模拟器)中老是实现不了,而用VC编译即可实现,大家可以直接简单编译下看看。。这是为何?然道EVC下使用ToolTip有特别的地方?望高手赐教。
#include
#include
#include
#include "Resource.h"
//#pragma comment(lib,"comctl32.lib")//VC中放开,EVC就注释掉。
HINSTANCE g_hInstance = NULL;
HWND g_hDlg = 0;
HWND g_hButtonTest = 0;
HWND g_hTTWnd = 0;
void AddTooltip(HWND hDlg, HWND hCtrlWnd, LPCTSTR szTipText)
{
TOOLINFO ti;
HWND hTTWnd = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP/* | TTS_BALLOON*/,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
hDlg, NULL, g_hInstance, NULL);
SetWindowPos(hTTWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
ZeroMemory(&ti, sizeof(TOOLINFO));
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_IDISHWND|TTF_SUBCLASS;
ti.hwnd = hDlg;
ti.hinst = g_hInstance;
ti.uId = (UINT)hCtrlWnd;
ti.lpszText = (LPTSTR)szTipText;
SendMessage(hTTWnd, TTM_ACTIVATE, TRUE, 0);
SendMessage(hTTWnd, TTM_ADDTOOL, 0, (LPARAM)&ti);
SendMessage(hTTWnd, TTM_SETMAXTIPWIDTH, 0, 260);
SendMessage(hTTWnd, TTM_SETDELAYTIME, TTDT_AUTOPOP, 4000);
g_hTTWnd = hTTWnd;
}
void OnInitDialog(HWND hDlg)
{
g_hButtonTest = GetDlgItem(hDlg, IDC_BUTTON_TEST);
AddTooltip(hDlg, g_hButtonTest, TEXT("TEST"));
}
INT_PTR CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
OnInitDialog(hDlg);
return TRUE;
case WM_CLOSE:
DestroyWindow(hDlg);
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
case IDCANCEL:
PostQuitMessage(0);
break;
}
return FALSE;
case WM_DESTROY:
PostQuitMessage(0);
return TRUE;
};
return FALSE;
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MSG msg;
g_hInstance = hInstance;
InitCommonControls();
g_hDlg = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG_MAIN), 0, DlgProc);
if(NULL == g_hDlg)
{
return 0;
}
ShowWindow(g_hDlg, nShowCmd);
UpdateWindow(g_hDlg);
while( GetMessage(&msg, NULL, 0, 0) )
{
if(g_hTTWnd)
SendMessage(g_hTTWnd, TTM_RELAYEVENT, 0, (LPARAM)&msg);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
|
|