Ver Mensaje Individual
  #1  
Antiguo 27-05-2010
Avatar de Chris
[Chris] Chris is offline
Miembro Premium
 
Registrado: abr 2007
Ubicación: Jinotepe, Nicaragua
Posts: 1.678
Reputación: 19
Chris Va por buen camino
problemas con las comparaciones "num1 in [num2 .. num3]" en Delphi 2009

Hola compañeros, recurro a ustedes para que me ayuden a aclarar una duda que tengo. Es con respecto a las evaluaciones del tipo "if num1 in [num2 .. num3]" específicamente evaluaciones de rangos. Resulta que ayer estaba escribiendo este algoritmo:

Código Delphi [-]
/ Calcula si el punto donde será dibujado TextToDraw será bajo el puntero del Ratón.
// PARÁMETROS:
//  CanvasHandle:       Handle del canvas donde se está haciendo el dibujo.
//  TextToDraw:         Texto que se va dibujar.
//  ConstrolOrigin:     Punto relativo a la pantalla en donde se inicia el 
//                      control sobre el que se está haciendo el dibujado. 
//                      (Propiedad "ClientOrigin" de TControl).
//  MainRectTopLeft:    Rect principal del canvas
//  ExtraDrawingRect:   Rect del buffered bitmap que se está utilizando para
//                      hacer el dibujado. Rect compuesto de 0's si no se está
//                      usando un buffered bitmap.  
function MouseOverTextCanvas(const CanvasHandle: HDC;
                             const TextToDraw: String;
                             const ControlOrigin: TPoint;
                             const MainRectTopLeft: TRect;
                             const ExtraDrawingRect: TRect): boolean;
var
    CalcRect: TRect;
    TextRect: TRect;
begin

    // calcular el la posición y dimensión del texto a dibujar
    TextRect := Rect((ControlOrigin.X + MainRectTopLeft.Left + ExtraDrawingRect.Left),
                     (ControlOrigin.Y + MainRectTopLeft.Top + ExtraDrawingRect.Top),
                     0, 0);
    CalcRect := Rect(0, 0, 0, 0);
    DrawText(CanvasHandle,
             PChar(TextToDraw),
             Length(TextToDraw),
             CalcRect, DT_CALCRECT);
    TextRect.Right := TextRect.Left + CalcRect.Right;
    TextRect.Bottom := TextRect.Top + CalcRect.Bottom;

    // el problema es con la evaluación en la línea siguiente. Hasta donde llegan
    // mis conocimientos esta evaluación debería funcionar, pero no lo hace
    // en Delphi 2009 sin Updates
    Result := ((Mouse.CursorPos.X in [TextRect.Left .. TextRect.Right]) and
               (Mouse.CursorPos.Y in [TextRect.Top .. TextRect.Bottom]));

end;

El anterior algoritmo es para probar si un texto dibujado está sobre cierta región del Canvas, para dibujar el texto con estilo "Underline" por ejemplo, al estilo Web.

El problema no es con el algoritmo en si, sino es con la evaluación. Ya que luego de quebrarme la cabeza por gran rato modifiqué la anterior evaluación y me quedé con el mismo algoritmo, pero con una evaluación más de principiante :

Código Delphi [-]
function MouseOverTextCanvas(const CanvasHandle: HDC;
                             const TextToDraw: String;
                             const ControlOrigin: TPoint;
                             const MainRectTopLeft: TRect;
                             const ExtraDrawingRect: TRect): boolean;
var
    CalcRect: TRect;
    TextRect: TRect;
    X, L, R, Y, T, B: Integer;
begin

    // calcular el la posición y dimensión del texto a dibujar
    TextRect := Rect((ControlOrigin.X + MainRectTopLeft.Left + ExtraDrawingRect.Left),
                     (ControlOrigin.Y + MainRectTopLeft.Top + ExtraDrawingRect.Top),
                     0, 0);
    CalcRect := Rect(0, 0, 0, 0);
    DrawText(CanvasHandle,
             PChar(TextToDraw),
             Length(TextToDraw),
             CalcRect, DT_CALCRECT);
    TextRect.Right := TextRect.Left + CalcRect.Right;
    TextRect.Bottom := TextRect.Top + CalcRect.Bottom;

    X := Mouse.CursorPos.X;
    Y := Mouse.CursorPos.Y;
    L := TextRect.Left;
    R := TextRect.Right;
    T := TextRect.Top;
    B := TextRect.Bottom;

    Result := (((x >= l) and (x <= r)) and ((Y >= T) and (Y <= B)));

end;

Un dato curioso también, la existencia de las variables X, Y, L, R, T y B se debe a que la anterior evaluación hecha directamente con las variables Mouse.CursorPos y TextRect no funcionaba tampoco.

No sé si es que estoy equivocado sobre la función de in o es que realmente mi suposición de que es un problema de Delphi 2009 es correcta.

Quisiera saber sus opiniones al respecto.

Saludos,
Chris
__________________
Perfil Github - @chrramirez - Delphi Blog - Blog Web
Responder Con Cita