VC MFC 當滑鼠移到控制項上時顯示提示資訊

2021-05-27 17:02:26 字數 1702 閱讀 8054

vc/mfc 當滑鼠移到控制項上時顯示提示資訊

tooltip是win32中乙個通用控制項,mfc中為其生成了乙個類ctooltipctrl,總的說來其使用方法是較簡單的,下面講一下它的一般用法和高階用法。 

一般用法步驟: 

新增ctooltipctrl成員變數 m_tt。 

在父視窗中呼叫enabletooltips(true); 

在視窗的oncreate(或者其他適當的位置)中向tooltip中新增需要顯示tip的子視窗,並同時指定相應的顯示字串ctooltipctrl::addtool(pwnd, "string to display ")。 

過載父視窗的 bool pretranslatemessage(msg* pmsg) ,在函式中呼叫 m_tt.relayevent(pmsg)。 

下面假設在視窗cwndyour中使用ctooltipctrl 

在類定義中新增變數說明: 

class cwndyour:*** 

在oncreate中新增需要顯示tip的子視窗 

cwndyour::oncreate(....) 

在bool pretranslatemessage(msg* pmsg)中新增** 

bool cwndyour::pretranslatemessage(msg* pmsg) 

return cparentclass::pretranslatemessage(pmsg); 

}  這樣當滑鼠移動到相應的子視窗上時會顯示出相應的tooltip。 

動態改變tooltip的顯示內容的方法及步驟: 

上面所講的1、2、4步驟。 

在增加tooltip時不指定顯示的字串,而是使用lpstr_textcallback。 

在視窗中增加訊息對映 on_notify_ex( ttn_needtext, 0, settiptext )。 

在視窗中增加乙個函式用於動態提供顯示內容,其原型為 bool settiptext( uint id, nmhdr * ptttstruct, lresult * presult ),下面的**可以根據傳入的引數判定應該顯示的內容。 

bool cwndyour::settiptext( uint id, nmhdr * ptttstruct, lresult * presult )  

return(false);

}

另外的就是在相應函式中區分unicode編碼

bool cpreparent::ontooltiptext(uint, nmhdr* pnmhdr, lresult* presult)

if (nid != 0) //不為分隔符

else

#else

if (pnmhdr->code == ttn_needtexta)

else

#endif

*presult = 0;

//使工具條提示視窗在最上面

::setwindowpos(pnmhdr->hwndfrom, hwnd_top, 0, 0, 0, 0,

swp_noactivate|swp_nosize|swp_nomove|swp_noownerzorder);

return true;

} return true;

}

控制滑鼠移到到特定控制項上然後模擬滑鼠單擊

public partial class demoform form dllimport user32.dll static extern bool setcursorpos int x,int y dllimport user32.dll static extern void mouse even...

VC中當滑鼠停靠在按鈕等控制項上時出現文字提示功能

首先在對話方塊的標頭檔案中加入初始化語句 public 下,加入 ctooltipctrl m stat 然後在初始化對話方塊函式中加入 注 我開始在oncreate函式中加的這段 發現有addtool那句程式執行就出錯,不知道為什麼。然後把這段放在初始化函式,就沒錯了 m stat.create ...

Qt實現當滑鼠移動到窗體上,窗體由半透明變為不透明

qt4.7中可以設定窗體的透明度。下面的方法讓窗體實現當滑鼠移動到窗體上,窗體由半透明變為不透明的效果。首先我們在窗體初始化函式中設定窗體屬性 setwindowflags qt windowstaysontophint 設定窗體置頂。如果不設定置頂,當窗體被覆蓋時,其透明度的改變就觀察不到了。大家...