program hButton; {$R hButton.res} uses Windows, Messages; const RES_DIALOG = 101; RC_BUTTON = 101; var OldBtnProc : Pointer; MouseOver : boolean; procedure PaintImageFileButton(DC : HDC; RtBtn : TRect; szPathFile : WideString); var NewBmp : HBITMAP; OldBmp : HBITMAP; CompDC : HDC; begin CompDC := CreateCompatibleDC(0); NewBmp := LoadImageW(hInstance, PWideChar(szPathFile), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE or LR_LOADFROMFILE); OldBmp := SelectObject(CompDC, NewBmp); BitBlt(DC, 0, 0, RtBtn.Right - RtBtn.Left, RtBtn.Bottom - RtBtn.Top, CompDC, 0, 0, SRCCOPY); SelectObject(CompDC, OldBmp); DeleteDC(CompDC); DeleteObject(NewBmp); end; function DrawCloseButton(hWnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; var point :TPoint; Rect :TRect; begin case uMsg of WM_MOUSEMOVE: begin GetCursorPos(point); GetWindowRect(hwnd, rect); if PtInRect(rect, point) then begin if GetCapture <> hWnd then begin SetCapture(hWnd); MouseOver:=True; InvalidateRect(hWnd,nil,FALSE); end; end else begin ReleaseCapture; MouseOver:=False; InvalidateRect(hWnd,nil,FALSE); end; end; end; Result := CallWindowProcW(OldBtnProc, hWnd, uMsg, wParam, lParam); end; procedure Button_OnDrawItemW(DStr : PDRAWITEMSTRUCT); begin if (DStr.itemState and ODS_SELECTED)<>0 then PaintImageFileButton(DStr.hDC, DStr.rcItem, 'CloseButtonDown.bmp') else PaintImageFileButton(DStr.hDC, DStr.rcItem, 'CloseButtonNormal.bmp'); if (MouseOver) and NOT ((DStr.itemState and ODS_SELECTED)<>0) then PaintImageFileButton(DStr.hDC, DStr.rcItem, 'CloseButtonOver.bmp'); end; function MainWndProc(hWnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM) : BOOL; stdcall; var Rect :TRect; PS :TPaintStruct; begin Result := TRUE; case uMsg of WM_INITDIALOG: begin // SetWindowLongW(hApp, GWL_EXSTYLE, GetWindowLong(hApp, GWL_EXSTYLE){ or WS_EX_LAYERED}); // SetLayeredWindowAttributes(hApp, $00FF00FF, 255, LWA_COLORKEY + LWA_ALPHA); OldBtnProc := Pointer(GetWindowLongW(GetDlgItem(hWnd, RC_BUTTON), GWL_WNDPROC)); SetWindowLongW(GetDlgItem(hWnd, RC_BUTTON), GWL_WNDPROC, LongInt(@DrawCloseButton)); end; WM_PAINT: begin GetClientRect(hWnd, Rect); BeginPaint(hWnd, PS); PaintImageFileButton(PS.hDC, Rect, 'MainWindow.bmp'); EndPaint(hWnd, PS); end; WM_DRAWITEM: begin case LoWord(wParam) of RC_BUTTON: Button_OnDrawItemW(PDRAWITEMSTRUCT(lParam)); end; end; WM_LBUTTONDOWN: SendMessage(hWnd,WM_SYSCOMMAND,$F012, 0); WM_CLOSE, WM_DESTROY: PostQuitMessage(0); else Result := FALSE; end; end; begin DialogBoxW(hInstance, PWideChar(RES_DIALOG), 0, @MainWndProc); end. |