델파이

TDBCtrlGrid 마우스휠 이벤트 구현, TMessage 타입 변경이 원인

미스터몽키 2016. 3. 17. 19:12

델파이2007 이하 버전에서 마우스휠 이벤트가 없는 TDBCtrlGrid 등의 컴포넌트에서  마우스 휠을 사용하기 위해

구현했던 프로시저가 델파이 2009 이후부터는 원할하게 작동하지 않는다.


원인은 프로시저에서 TMessage를 사용하는데

TMessage 멤버의 타입이 델파이 2009 버전부터 변경되었기 때문이다. (나는 델파이10 seattle)


(델파이 2007에서)

  TMessage = packed record
    Msg: Cardinal;
    case Integer of
      0: (
        WParam: Longint;      // -21억 ~ 21억
        LParam: Longint;
        Result: Longint);
      1: (
        WParamLo: Word;
        WParamHi: Word;
        LParamLo: Word;
        LParamHi: Word;
        ResultLo: Word;
        ResultHi: Word);
  end;


(델파이10 시애틀)

  TMessage = record
    Msg: Cardinal;
    case Integer of
      0: (
        WParam: WPARAM;   -> UINT_PTR = System.UIntPtr;  // NativeUInt;   0 ~ 42억
        LParam: LPARAM;
        Result: LRESULT);
      1: (
        WParamLo: Word;
        WParamHi: Word;
        WParamFiller: TDWordFiller;
        LParamLo: Word;
        LParamHi: Word;
        LParamFiller: TDWordFiller;
        ResultLo: Word;
        ResultHi: Word;
        ResultFiller: TDWordFiller);
  end;


따라서 기존 프로시저에서 Integer로 타입캐스팅 해야 원할하게 작동된다.


procedure TForm1.MouseWheelHandler(var Message: TMessage);
begin
//  아래는 TDBMemo에 포커스를 TDBCtrlGrid로 이동하고  마우스 휠이 작동되도록

  if Message.msg = WM_MOUSEWHEEL then
  begin
    if (ActiveControl is TDBMemo) then
    begin
      DBCtrlGrid1.SetFocus;  // 포커스 강제로 이동하여 아래 쪽 코드가 작동되도록
    end;

    if (ActiveControl is TDBCtrlGrid) then     
    begin
      // 델2009부터 TMessage.WParam 타입이 integer(-21억 ~ 21억)에서  NativeUInt(0 ~ 42억)로 변경되어
      // 휠마우스를 위, 아래 스크롤시 wParam 이 모두 양수가 나옴, 원래는 아래는 음수임
      // 때문에 Integer로 타입캐스팅해야함

      if Integer(Message.wParam) > 0 then         
      begin
        keybd_event(VK_UP, VK_UP, 0, 0);
      end
      else if Integer(Message.wParam) < 0 then
      begin
        keybd_event(VK_DOWN, VK_DOWN, 0, 0);
      end;
    end;
  end;


end;