Ver Mensaje Individual
  #2  
Antiguo 05-02-2017
Avatar de escafandra
[escafandra] escafandra is offline
Miembro Premium
 
Registrado: Nov 2007
Posts: 1.937
Reputación: 13
escafandra Va camino a la famaescafandra Va camino a la fama
Lo que sigue es la versión de la misma clase para su uso en Builder C++

Código:
// MouseLeave.cpp
//--------------------------------------------------------------------------------------------------
// TMouseLeave (Versión Hook estilo C++)
// escafandra 2017
// Clase para manejo de WM_MOUSELEAVE de una ventana


#ifndef MouseLeaveCPP
#define MouseLeaveCPP

#include <Windows.h>

#ifndef STRICT
  typedef int  (__stdcall *PLRESULT)();
#else
  typedef WNDPROC PLRESULT;
#endif

//typedef void (__fastcall *POnMouseLeave)(HWND hWnd, BOOL Enter);
typedef void __fastcall(__closure* POnMouseLeave)(HWND hWnd);
typedef void __fastcall(__closure* POnMouseEnter)(HWND hWnd);


class TMouseLeave
{
  private:
  HWND Handle;
  PLRESULT OldWndProc;
  static LRESULT __stdcall DefWndProc(HWND hWnd, UINT Msg, WPARAM WParam, LPARAM LParam)
  {
    TMouseLeave* pMouseLeave = (TMouseLeave*)GetWindowLongPtr(hWnd, GWL_USERDATA);
    if(pMouseLeave)
      return pMouseLeave->WndProc(hWnd, Msg, WParam, LParam);
    else
      return DefWindowProc(hWnd, Msg, WParam, LParam);
  }

  LRESULT __stdcall WndProc(HWND hWnd, UINT Msg, WPARAM WParam, LPARAM LParam)
  {
    if(Msg == WM_MOUSELEAVE && OnMouseLeave)
      OnMouseLeave(Handle);

    else if(Msg == WM_MOUSEMOVE && OnMouseEnter){
      TRACKMOUSEEVENT TE = {sizeof(TRACKMOUSEEVENT)};
      TE.dwFlags = TME_LEAVE;
      TE.hwndTrack = Handle;
      TE.dwHoverTime = HOVER_DEFAULT;
      TrackMouseEvent(&TE);
      OnMouseEnter(Handle);
    }

    return CallWindowProc(OldWndProc, hWnd, Msg, WParam, LParam);
  }

  public:
  POnMouseLeave OnMouseLeave;
  POnMouseEnter OnMouseEnter;

  void SetHandle(HWND hWnd)
  {
    if(hWnd != INVALID_HANDLE_VALUE && hWnd != Handle){
      if(!hWnd){
        SetWindowLong(GetParent(Handle), GWL_USERDATA, 0);
        SetWindowLong(GetParent(Handle), GWL_WNDPROC, (LONG)OldWndProc);
      }
      if(hWnd){
        SetWindowLongPtr(hWnd, GWL_USERDATA, (LONG)this);
        OldWndProc = (PLRESULT)SetWindowLongPtr(hWnd, GWL_WNDPROC, (LONG)TMouseLeave::DefWndProc);
      }
      Handle = hWnd;
    }
  }

  TMouseLeave(HWND hWnd = 0): OnMouseLeave(0), OnMouseEnter(0)
  {
    SetHandle(hWnd);
  }

  ~TMouseLeave() {SetHandle(0);}
};
#endif
Y un ejemplo con un TButton:
Código:
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "MouseLeave.cpp"
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published:    // IDE-managed Components
  TButton *Button1;
private:
  TMouseLeave ML;
  void __fastcall OnMouseLeave(HWND hWnd);
  void __fastcall OnMouseEnter(HWND hWnd);
public:        // User declarations
  __fastcall TForm2(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif
Código:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
  : TForm(Owner)
{
  ML.SetHandle(Button1->Handle);
  ML.OnMouseLeave = OnMouseLeave;
  ML.OnMouseEnter = OnMouseEnter;
}
//---------------------------------------------------------------------------

void __fastcall TForm2::OnMouseLeave(HWND hWnd)
{
  TButton *B = static_cast<TButton*>(FindControl(hWnd));
  if(B) B->Caption = "Adios";
}

void __fastcall TForm2::OnMouseEnter(HWND hWnd)
{
  TButton *B = static_cast<TButton*>(FindControl(hWnd));
  if(B) B->Caption = "Hola";
}

Saludos.
Responder Con Cita