ヒントを表示するには、TListViewのShowHintプロパティがtrueである必要があります。
TListViewのOnInfoTipイベントを使用すると、リストビュー内の個々の項目に対応したヘルプヒントを指定できます。
void __fastcall TForm1::ListView1InfoTip(TObject *Sender, TListItem *Item, UnicodeString &InfoTip)
{
InfoTip = Item->Caption + "\r\n" +
Item->SubItems->Strings[0] + "\r\n" +
Item->SubItems->Strings[1];
}
OnInfoTipイベントでは、常に同じヒント文字になります。
項目ごとに異なるヒントを表示するには、MouseMoveイベントで項目を判定する必要があります。
ヘッダファイルにメンバ変数「FCurrentHint」を追加します。
FCurrentHint変数は、ヒント文字列に変更があるか判定するために使用します。
class TForm1 : public TForm
{
(中略)
private: // ユーザー宣言
UnicodeString FCurrentHint; //リストビューで表示しているヒント
(中略)
};
MouseMoveイベントを記述します。
ListView1->ScreenToClient(Mouse->CursorPos)でマウスカーソルの座標を取得します。
hitInfo.pt = ListView1->ScreenToClient(Mouse->CursorPos);
ListView_SubItemHitTestマクロを使用して、マウスカーソルがリストビューの項目上にあるかどうかを判定します。
// -1なら範囲外
if (ListView_SubItemHitTest(ListView1->Handle, &hitInfo) == -1) return;
hitInfo.iItemには行番号が、hitInfo.iSubItemには列番号が入ります。
行・列に応じて、ヒント文字列を作成します。
ヒント文字列を作成したら、ListView1->Hintに設定して、Application->ActivateHintで表示します。
//ヒントを表示する
ListView1->Hint = hint;
Application->ActivateHint(hitInfo.pt);
全体のソースコードは次のようになります。
void __fastcall TForm1::ListView1MouseMove(TObject *Sender, TShiftState Shift, int X,
int Y)
{
LVHITTESTINFO hitInfo;
//マウスカーソルの位置を取得
hitInfo.pt = ListView1->ScreenToClient(Mouse->CursorPos);
// -1なら範囲外
if (ListView_SubItemHitTest(ListView1->Handle, &hitInfo) == -1) return;
// hitInfo.iItem => 行番号
// hitInfo.iSubItem => 列番号
//ヒントに表示する文字を作成する
UnicodeString hint;
if (hitInfo.iSubItem == 0)
{
hint = UnicodeString().sprintf(L"書名:%s",
ListView1->Items->Item[hitInfo.iItem]->Caption);
}
else
{
hint = UnicodeString().sprintf(L"書名:%s\r\n%s:%s",
ListView1->Items->Item[hitInfo.iItem]->Caption,
ListView1->Columns->Items[hitInfo.iSubItem]->Caption,
ListView1->Items->Item[hitInfo.iItem]->SubItems->Strings[hitInfo.iSubItem-1]);
}
//ヒントの文字に変更がなければ表示を更新しない
if (this->FCurrentHint == hint) return;
this->FCurrentHint = hint;
//ヒントを表示する
ListView1->Hint = hint;
Application->ActivateHint(hitInfo.pt);
}