Tooltips

#include<windows.h>
#include<Commctrl.h>
#include"resource.h"

#pragma comment(lib,"comctl32.lib")

HWND CreateToolTips(HWND hWnd1,HINSTANCE hInst,HWND DlgItem,WCHAR *szName,int n=1);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    HWND hWnd=CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,(DLGPROC)WndProc);
        ShowWindow(hWnd,nCmdShow);
        MSG msg;
        while (GetMessage(&msg,NULL,NULL,NULL))
        {
            if (msg.message==WM_KEYDOWN)
            {
                SendMessage(hWnd,msg.message,msg.wParam,msg.lParam);
            }else
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }

    return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    switch (message)
    {
    case WM_INITDIALOG:
        {
            CreateToolTips(hWnd,GetModuleHandle(NULL),GetDlgItem(hWnd,IDC_BUTTON1),L"This is the 'OK' button");
        }
        break;
    case WM_CLOSE:
        PostQuitMessage(0);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return false;
    }
    return true;
}
/*窗口句柄 模块句柄 控件句柄 说明文字*/
HWND CreateToolTips(HWND hWnd1,HINSTANCE hInst,HWND DlgItem,WCHAR *szName,int n)
{
    INITCOMMONCONTROLSEX icex;
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC = ICC_TAB_CLASSES;// tab, tooltips
    //InitCommonControlsEx可以指定初始化什么控件
    InitCommonControlsEx(&icex);
    //Tooltips是当鼠标停止在某一个工具元件时,界面对用户的提示
    HWND hwndTip =CreateWindowEx(WS_EX_TOPMOST,TOOLTIPS_CLASS,0,TTS_ALWAYSTIP|TTS_NOPREFIX,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,hWnd1,0,hInst,0);
    TOOLINFO ti={0};
    ti.cbSize = sizeof(TOOLINFO)-4;
    //TTF_IDISHWND说明不填充ti.rect
    ti.uFlags = TTF_SUBCLASS|TTF_IDISHWND;

    //ti.uFlags = TTF_SUBCLAS
    //ti.rect.bottom=200;
    //ti.rect.left=100;
    //ti.rect.right=400;
    //ti.rect.top=100;
    ti.hwnd   = hWnd1;
    ti.hinst=hInst;
    ti.uId =(UINT_PTR)DlgItem;
    ti.lpszText =szName;
    SendMessage(hwndTip,TTM_SETMAXTIPWIDTH,0,n);
    SendMessage(hwndTip,TTM_ADDTOOL,0,(LPARAM)&ti);
    //设置背景颜色
    SendMessage(hwndTip,TTM_SETTIPBKCOLOR,RGB(0,0,0),0);
    //设置字体颜色
    SendMessage(hwndTip,TTM_SETTIPTEXTCOLOR,RGB(0,255,0),0);
    return hwndTip;
}

鼠标移动到OK时,如上图所示。

posted @ 2013-12-06 12:17  乾卦  阅读(475)  评论(0编辑  收藏  举报