如何自訂提示視窗(Hint Window)

來源:互聯網
上載者:User
// 自訂 THintWindow 類
class TCcrunHintWindow : public THintWindow
{
  bool FActivating;
  __fastcall TCcrunHintWindow(TComponent* Owner)
      : THintWindow(Owner)
  {
    Canvas->Font->Name = "宋體";
    Canvas->Font->Color = clBlack;
    Canvas->Font->Size = 9;
  }
  void __fastcall Paint( void)
  {
    TRect rect = ClientRect;
    // Hint邊框顏色
    Canvas->Brush->Color = TColor(0xDBB8BA);
    Canvas->FillRect(rect);
    // 繪製整個Hint的邊框
    Canvas->Pen->Color = TColor(0x69230E);
    Canvas->Rectangle(rect);
    // Hint背景的顏色
    Color = clWhite;
    // Hint文字透明
    Canvas->Brush->Style = bsClear;
    // 繪出Hint文字
    Canvas->Font->Color = clBlack;
    Canvas->TextOut(4, int(rect.Bottom / 2)
        - int(Canvas->TextHeight(Caption) / 2), Caption);
  }
  virtual void __fastcall NCPaint(HDC hdc)
  {
    // 63 63 72 75 6E 2E 63 6F 6D
    Invalidate();
  }
  virtual void __fastcall CreateParams(TCreateParams &Params)
  {
    // 去掉Hint視窗的邊框
    Params.Style = Params.Style & ~WS_BORDER;
    THintWindow::CreateParams(Params);
  }
  // Code by ccrun(老妖),做人要厚道,轉載請留名
  virtual void __fastcall ActivateHint( const TRect &Rect, const String AHint)
  {
    FActivating = true;
    try
    {
      Caption = AHint;
      TRect r = Rect;
      r.Left -= 10;
      r.Right += 10;
      r.Top -= 5;
      r.Bottom += 5;
      // 更新地區
      UpdateBoundsRect(r);
      // Hint視窗處於螢幕邊緣時的調整
      if(r.Top + Height > Screen->DesktopHeight)
        r.Top = Screen->DesktopHeight - Height;
      if(r.Left + Width > Screen->DesktopWidth)
        r.Left = Screen->DesktopWidth - Width;
      if(r.Left < Screen->DesktopLeft)
        r.Left = Screen->DesktopLeft;
      if(r.Bottom < Screen->DesktopTop)
        r.Bottom = Screen->DesktopTop;
      // 建立一個矩形
      // 63 63 72 75 6E 2E 63 6F 6D
      HRGN hrgn = CreateRectRgn(0, 0, r.Width(), r.Height());
      // HRGN hrgn = CreateRoundRectRgn(0, 0, r.Width(), r.Height(), 4, 4);
      // 設定指定控制代碼的視窗形狀
      SetWindowRgn(Handle, hrgn, true);
      // 改變視窗的位置,Z Order,及其他一些屬性
      SetWindowPos(Handle, HWND_TOPMOST, r.Left, r.Top, r.Width(),
          r.Height(), SWP_SHOWWINDOW | SWP_NOACTIVATE);
      // 重畫視窗
      Invalidate();
    }
    __finally
    {
      FActivating = false;
    }
  }
};
//---------------------------------------------------------------------------
// 實現代碼
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
  HintWindowClass = __classid(TCcrunHintWindow);
  //
  ShowHint = true;
  Button1->Hint = "這是一個按鈕";
  Edit1->Hint = "這是一個文字框";
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.