프로그램 만들다보면 힌트의 존재는 고객들에게 자주 요구되곤 합니다.
그런데 그냥 주어지는 힌트는 좀 멋이 없습니다.
이걸 자신만의 힌트로 만들어 볼수 없을까요?
특히 예쁜 그림과 설명이 같이 들어간다면 더 좋겠죠.
그래서 델파이쪽 힌트자료를 응용하여 빌더용으로 비트맵 그림을
설정가능하게 추가 기능을 넣어 클래스를 제작해 봤습니다.
소스와 데모를 그대로 첨부했으므로 자신 나름대로의 힌트를 꾸미는데 도움이 될 것입니다.
다시 말해, 조금 응용하면 힌트에 뭐든 표시 가능하다는 뜻이죠.
비트맵만 다루도록 되어 있는데 소스를 약간만 손보면 jpg gif 등
여러가지를 다룰 수도 있으며 표시형식도 임의로 수정할 수 있습니다.
델파이로 만들었으면 델과 빌더 양쪽에서 다 쓰는 건데...
제가 C++빌더를 더 좋아하다 보닌까. ㅎㅎ
하여간 응용은 알아서 하시기를...
소스는 첨부를 했지만 핵심소스를 아래와 같습니다.
ps. 볼랜드포럼 광고차원에서 빌더포럼 그림을 데모에 넣어 놨음 .. ㅎㅎ...
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "TBitmapHintWindow.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
// 비트맵 그림을 넣을 수 있는 힌트클래스
// Written by 김태선 jsdkts@korea.com
// Copyright Free: 사용할 때는 저자에게 통보만 하면 됨.
// 선언부
class TBitmapHintWindow : public THintWindow
{
private:
Graphics::TBitmap *FImg;
String FbmpFilename;
public:
static String bmpFilename;
public:
__fastcall TBitmapHintWindow(TComponent *AOwner);
virtual __fastcall ~TBitmapHintWindow();
virtual void __fastcall ActivateHint(const TRect& Rect, const String AHint);
void SetBitmap(String Filename);
protected:
virtual void __fastcall Paint();
virtual void __fastcall NCPaint(HDC dc);
};
//---------------------------------------------------------------------------
// 구현부
String TBitmapHintWindow::bmpFilename;
__fastcall TBitmapHintWindow::TBitmapHintWindow(TComponent *AOwner)
: THintWindow(AOwner)
{
FImg = NULL;
FbmpFilename = "";
}
__fastcall TBitmapHintWindow::~TBitmapHintWindow()
{
if (FImg)
delete FImg;
}
void TBitmapHintWindow::SetBitmap(String Filename)
{
if (FbmpFilename == Filename)
return;
FbmpFilename = Filename;
if (Filename == "")
{
delete FImg;
FImg = NULL;
return;
}
if (FImg == NULL)
FImg = new Graphics::TBitmap;
try
{
FImg->LoadFromFile(Filename);
}
catch(...)
{
ShowMessage(Filename + " 그림 없슴."); // 이 라인은 주석처리해도 좋다.
FImg = NULL;
}
}
void __fastcall TBitmapHintWindow::Paint()
{
Canvas->Brush->Style = bsClear;
Canvas->Pen->Color = clOlive;
Canvas->Rectangle(ClientRect);
TRect r = ClientRect;
r.left += 4;
r.top += (FImg ? 8 + FImg->Height : 4);
//Canvas->Font->Color = clInfoText; // 표준 칼라.
Canvas->Font->Color = clGreen; // 임의로 칼라를 주어 이쁘게 꾸밀수도 있다.
DrawText(Canvas->Handle, Caption.c_str(), -1, &r, DT_LEFT | DT_NOPREFIX | DT_WORDBREAK);
if (FImg)
Canvas->Draw(4, 4, FImg);
}
void __fastcall TBitmapHintWindow::NCPaint(HDC dc)
{
; // 코딩속의 나의 미소.
}
void __fastcall TBitmapHintWindow::ActivateHint(const TRect& R, const String AHint)
{
SetBitmap(bmpFilename);
Caption = AHint;
TRect& Rect = (TRect)R;
Rect.Bottom += 6 + (FImg ? FImg->Height + 6: Canvas->Font->Height + 14);
Rect.right += 4;
if (FImg)
{
if (FImg->Width > Canvas->TextWidth(AHint))
Rect.right += 4 + (FImg->Width - Canvas->TextWidth(AHint));
}
BoundsRect = Rect;
if (Rect.Top + Height > Screen->Height)
Rect.Top = Screen->Height - Height;
if (Rect.Left + Width > Screen->Width)
Rect.Left = Screen->Width - Width;
if (Rect.Left < 0)
Rect.Left = 0;
if (Rect.Bottom < 0)
Rect.Bottom = 0;
SetWindowPos(Handle, HWND_TOPMOST, Rect.Left, Rect.Top, 0, 0,
SWP_SHOWWINDOW | SWP_NOACTIVATE | SWP_NOSIZE);
}
// 비트맵힌트 클래스 끝
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 힌트 보이기 이벤트
void __fastcall TForm1::DoOnShowHint(String& HintStr, bool& CanShow, THintInfo& HintInfo)
{
// 여기서 if (HintInfo.HintControl == 컴포넌트) 로 어떤 것의 힌트인지 구분가능하다.
// 컨트롤당 원하는 그림을 설정한다.
String filename = "cbuilder1.bmp";
if (HintInfo.HintControl == Button1)
filename = "help.bmp";
else if (HintInfo.HintControl == Button3)
filename = "ㅎㅎ.bmp";
else if (HintInfo.HintControl == Button4)
filename = "";
// TBitmapHintWindow 클래스는 VCL이 내부적으로 생성 처리하므로 이렇게 클래스내의 static 변수를 통해 파일명을 넘긴다.
TBitmapHintWindow::bmpFilename = filename;
Types::TPoint pt;
GetCursorPos(&pt);
pt.x += 16;
pt.y += 10;
HintInfo.HintWindowClass = __classid(TBitmapHintWindow);
HintInfo.HintPos = pt;
HintInfo.HintColor = clWhite;
HintInfo.HintMaxWidth = 300;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
// 예쁜 흰트가 나오도록 설정한다.
Application->OnShowHint = DoOnShowHint;
}
//---------------------------------------------------------------------------
|