Overcome TImage Control limitations

478 Views Asked by At

I am using TImage control inside TScrollBox control in Delphi 4. Here i am populating data in TImage control by increasing the height accordingly.

My functionality is based on the mousedown event where i am using X, Y parameters to do some validations with highlighting that record.

Now issue here is X, Y are integer type parameters, and they return values in between -32768 to 32767. I am facing issues when my records are going beyond 32767 height. Mousedown event is returning Y value as negative causes issues with my functionality.

There are 2 possible ways to resolve this issue,

  1. Any workaround to fix this issue.

  2. Using another inbuilt component of Delphi 4 as an replacement.

I know one dirty approach (may be this is also not correct but working as of now) as mentioned below, but i am looking for better solution.

if Y < 0 then 
begin
      Y := Y + 65536;
end
else
begin
     if ScrollBox1.VertScrollBar.Position > 32767 then
        Y := Y + 65536;
end;

Please advice a better solution to this problem

2

There are 2 best solutions below

0
On

There is a third way to solve it. Stop using a Scrollbox, and write your own control with its own scroll bar handles, that can render part of an image, at X,Y co-ordinates specified by you. You could in fact do this by adding scrollbars to any simple TControl-based component written by you; if you insist on using a built-in control in Delphi you will in fact be doing something faintly rediculous. Writing your own control can be done in a few lines of code, and is the right answer if you do not wish to use third party controls.

The second alternative, and in fact, the solution that 99% of Delphi developers would pick, is to switch to a third-party image viewer control that will already have this functionality. Obviously, there is no point reinventing the wheel. Your question seems to stipulate that you can't add a third party control.

Scrollboxes + TImage are a terrible solution from a performance point of view, especially when you must load an enormous image entirely into GDI memory to contain the picture. I am surprised that a 32K-pixel high by 1000+pixel high image would even load into Memory without serious problems. Your current idea is not only limited by the Scrollbox's limitations (explained well by the other person who answered this question) it is limited by GDI's image processing capabilities, which are (for very large images) not exactly ideal.

Whether you like it or not, either write your own, or use an image viewer for delphi. On the linked page, the Image Visualizer for Delphi v.1.00.00 by Ahmoy Law looks good to me.

3
On

The mouse coordinates are usually derived from a WMMouseMove message, the record definition for this message is as follows:

type TLMMouse = record
  Msg: Cardinal;
  Keys: PtrInt;
  case Integer of
    0: (XPos: SmallInt;
        YPos: SmallInt;);
    1: (Pos: TSmallPoint;);
    2: (Dummy: LPARAM;
        Result: LRESULT;);
end;

Because the Mouse Messages pack the coordinates in 2 smallints, there is no way around your issue.

Note that GetCursorPos gives a TPoint record with two Int32's, however this is not the function that your control uses, so you don't benefit from this.