Programmatically show tooltip in winforms application

前端 未结 6 1511
[愿得一人]
[愿得一人] 2020-12-09 15:26

How can I programatically cause a control\'s tooltip to show in a Winforms app without needing the mouse to hover over the control? (P/Invoke is ok if necessary).

6条回答
  •  青春惊慌失措
    2020-12-09 16:09

    This is the code I use:

    static HWND hwndToolTip = NULL;
    
    void CreateToolTip( HWND hWndControl, TCHAR *tipText )
    {  
        BOOL success;
    
      if( hwndToolTip == NULL )
      {
        hwndToolTip = CreateWindow(  TOOLTIPS_CLASS, 
                                     NULL, 
                                     WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,                                     
                                     CW_USEDEFAULT, CW_USEDEFAULT, 
                                     CW_USEDEFAULT, CW_USEDEFAULT,                                     
                                     NULL, NULL,
                                     hInstResource, 
                                     NULL ); 
      }
    
      if( hwndToolTip )
      { 
        TOOLINFO ti; 
    
        ti.cbSize   = sizeof(ti); 
        ti.uFlags   = TTF_TRANSPARENT | TTF_SUBCLASS; 
        ti.hwnd     = hWndControl; 
        ti.uId      = 0; 
        ti.hinst    = NULL; 
        ti.lpszText = tipText; 
    
        GetClientRect( hWndControl, &ti.rect ); 
    
        success = SendMessage( hwndToolTip, TTM_ADDTOOL, 0, (LPARAM) &ti ); 
      }
    }
    

    Call CreateToolTip function to create a tool tip for a certain control.

提交回复
热议问题