Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Trucos (https://www.clubdelphi.com/foros/forumdisplay.php?f=52)
-   -   Wm_mouseleave / Wm_mouseenter (https://www.clubdelphi.com/foros/showthread.php?t=91429)

escafandra 05-02-2017 12:54:03

Wm_mouseleave / Wm_mouseenter
 
El título lleva a engaño puesto que el mensaje WM_MOUSEENTER no existe aunque el concepto si.

Se trata de manejar los eventos OnMouseEnter y OnMouseLeave de una ventana en versiones Delphi antiguas que no implementan esta característica, al igual que en versiones Builder de la misma época. También servirá para usarlo con cualquier ventana sin necesidad de que se trate de un control - componente VCL específico.

El mensaje WM_MOUSELEAVE es recibido por una ventana si preparó previamente su solicitud con una llamada a TrackMouseEvent. Simplemente informa que el cursor del ratón abandonó el área cliente de dicha ventana. Para detectar la presencia del cursor en la ventana (WM_MOUSEENTER) basta con gestionar WM_MOUSEMOVE.

Propongo una clase que habilita el tratamiento del mensaje WM_MOUSELEAVE recibido por cualquier ventana (incluidos componentes derivados de TControl) Para conseguirlo realiza un Hook a la función de tratamiento de mensajes realizando un subclassing que genere dos eventos: OnMouseLeave y OnMouseEnter.

Este sería el código de la Unit con un apunte propuesto por [Agustin Ortu] en los constructores y el destructor de la clase:
Código Delphi [-]
unit MouseLeave;

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

interface

uses Windows, Messages;

type
  TOnMouseLeave = procedure(Handle: HWND) of object;
  TOnMouseEnter = procedure(Handle: HWND) of object;

type
  TMouseLeave = class
  private
    Handle: HWND;
    OldWndProc: Pointer;
    function WndProc(Handle: HWND; Msg: DWORD; WParam: Longint; LParam: Longint): Longint; stdcall;
  public
    OnMouseLeave: TOnMouseLeave;
    OnMouseEnter: TOnMouseEnter;
    constructor Create(WND: HWND); overload;
    destructor Destroy; override;
    procedure  SetHandle(WND: HWND);
  end;

implementation


function DefWndProc(Handle: HWND; Msg: DWORD; WParam: Longint; LParam: Longint): Longint; stdcall;
var
  pMouseLeave: TMouseLeave;
begin
  pMouseLeave:= TMouseLeave(GetWindowLong(Handle, GWL_USERDATA));
  if pMouseLeave <> nil then
    Result:= pMouseLeave.WndProc(Handle, Msg, WParam, LParam)
  else
    Result:= DefWindowProc(Handle, Msg, WParam, LParam);
end;

constructor TMouseLeave.Create(WND: HWND);
begin
  inherited Create;
  SetHandle(WND);
end;

function TMouseLeave.WndProc(Handle: HWND; Msg: DWORD; WParam: Longint; LParam: Longint): Longint; stdcall;
var
  TE: TTRACKMOUSEEVENT;
begin
  if (Msg = WM_MOUSELEAVE) and (@OnMouseLeave <> nil) then
    OnMouseLeave(Handle)

  else if (Msg = WM_MOUSEMOVE) and (@OnMouseEnter <> nil) then
  begin
    TE.cbSize:= sizeof(TTRACKMOUSEEVENT);
    TE.dwFlags:= TME_LEAVE;
    TE.hwndTrack:= Handle;
    TE.dwHoverTime:= HOVER_DEFAULT;
    TrackMouseEvent(TE);
    OnMouseEnter(Handle);
  end;
  Result:= CallWindowProc(OldWndProc, Handle, Msg, WParam, LParam);
end;


procedure TMouseLeave.SetHandle(WND: HWND);
begin
  if (WND <> INVALID_HANDLE_VALUE) and (WND <> Handle) then
  begin
    if WND = 0 then
    begin
      SetWindowLong(Handle, GWL_USERDATA, 0);
      SetWindowLong(Handle, GWL_WNDPROC, LongInt(OldWndProc));
    end;
    if WND <> 0 then
    begin
      SetWindowLong(WND, GWL_USERDATA, LongInt(self));
      OldWndProc:= Pointer(SetWindowLong(WND, GWL_WNDPROC, LongInt(@DefWndProc)));
    end;
    Handle:= WND;
  end;
end;

destructor TMouseLeave.Destroy;
begin
  SetHandle(0);
  inherited Destroy;
end;

end.

Lo que sigue es un ejemplo para usarlo con un TButton:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, MouseLeave;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    ME: TMouseLeave;
    procedure OnMouseLeave(Wnd: HWND);
    procedure OnMouseEnter(Wnd: HWND);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ME:= TMouseLeave.Create(Button1.Handle);
  ME.OnMouseEnter:= OnMouseEnter;
  ME.OnMouseLeave:= OnMouseLeave;
end;

procedure TForm1.OnMouseLeave(Wnd: HWND);
begin
  with FindControl(Wnd) as TButton do Caption:= 'Adios';
  if ME.InheritsFrom(TButton) then
    Windows.Beep(1000, 100);
end;

procedure TForm1.OnMouseEnter(Wnd: HWND);
begin
  with FindControl(Wnd) as TButton do Caption:= 'Hola';
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  ME.Free;
end;

end.

Se precisa crear tantos objetos TMouseLeave como ventanas a controlar.

Alguno puede preguntarse porqué no hacer un componente con esta clase o porqué no usar clases comodín Interpuestas para utilizar el modo Hook a la función de tratamiento de mensajes que porpone la VCL. La respuesta es simple, he preferido este modo porque así permite trabajar con el Handle de cualquier ventana más allá del entorno de la VCL y sirve para aplicaciones escritas a bajo nivel, a nivel API.


Saludos.

escafandra 05-02-2017 13:05:44

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.

Casimiro Notevi 05-02-2017 14:07:25

^\||/^\||/^\||/

Neftali [Germán.Estévez] 06-02-2017 12:05:08

^\||/^\||/^\||/^\||/
Gracias.

roman 08-02-2017 19:16:50

Cita:

Empezado por escafandra (Mensaje 512949)
Se trata de manejar los eventos OnMouseEnter y OnMouseLeave de una ventana en versiones Delphi antiguas que no implementan esta característica

¿A qué versiones antiguas te refieres? En Delphi 7, si bien muchos controles no implementan los eventos como propiedades, la VCL sí genera los mensajes CM_MOUSELEAVE y CM_MOUSEENTER.

LineComment Saludos

escafandra 08-02-2017 19:40:15

Cita:

Empezado por roman (Mensaje 513055)
¿A qué versiones antiguas te refieres? En Delphi 7, si bien muchos controles no implementan los eventos como propiedades, la VCL sí genera los mensajes CM_MOUSELEAVE y CM_MOUSEENTER.

LineComment Saludos

Claro, esto es como dices pero, como apunté en el truco, se trataba de ser más general que la VCL y que sirviera para ella y cualquier ventana, incluso en "aplicaciones API puras" en las que prescindimos totalmente de la VCL.

Las nuevas versiones lo adoptan cómo propiedad del control.


Saludos.


La franja horaria es GMT +2. Ahora son las 11:40:39.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi