All of the GDIPlus demo code I can find draws without invalidation. So how do you invalidate a rectangle in GDIPlus API when drawing with MouseMove with TImage on a TScrollbox?
function NormalizeRect ( R: TRect ): TRect;
begin
// This routine normalizes a rectangle. It makes sure that the Left,Top
// coords are always above and to the left of the Bottom,Right coords.
with R do
begin
if Left > Right then
if Top > Bottom then
Result := Rect ( Right, Bottom, Left, Top )
else
Result := Rect ( Right, Top, Left, Bottom )
else if Top > Bottom then
Result := Rect ( Left, Bottom, Right, Top )
else
Result := Rect ( Left, Top, Right, Bottom );
end;
end;
procedure TFormMain.Image1MouseDown ( Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer );
begin
if Line1.Down then
begin
GPPointStart := MakePoint ( X, Y );
end;
end;
procedure TFormMain.Image1MouseMove ( Sender: TObject; Shift: TShiftState; X, Y: Integer );
var
graphics: TGPGraphics;
pen: TGPPen;
SolidBrush: TGPSolidBrush;
rgbTriple: windows.RGBTRIPLE;
iRect: TRect;
begin
if Line1.Down then
begin
if ssLeft in Shift then
begin
iRect := NormalizeRect ( Rect ( X, Y, Image1.Picture.Bitmap.Width, Image1.Picture.Bitmap.Height ) );
InvalidateRect ( ScrollBox1.Handle, #iRect, TRUE );
graphics := TGPGraphics.Create ( Image1.Picture.Bitmap.Canvas.Handle );
graphics.Flush ( FlushIntentionFlush );
GPPointEnd := MakePoint ( X, Y );
rgbTriple := ColorToRGBTriple ( ColorBox1.Selected );
pen := TGPPen.Create ( MakeColor ( StrToInt ( Alpha1.Text ), rgbTriple.rgbtRed, rgbTriple.rgbtGreen, rgbTriple.rgbtBlue )
);
pen.SetWidth ( StrToInt ( Size1.Text ) );
graphics.DrawLine ( pen, GPPointStart.X, GPPointStart.Y, GPPointEnd.X, GPPointEnd.Y );
graphics.Free;
Image1.Refresh;
end;
end;
end;
This is what it looks like:
Using GDIPlus Library from http://www.progdigy.com with Delphi 2010.
The InvalidateRect command has nothing to do with GDI+. It's a command that tells the OS that a certain portion of a window is invalid and should be repainted. When the OS next decides to repaint that window, the program can ask the OS how much of the window needs painting.
Your code is calling InvalidateRect, and then it's painting over that same portion of the window. The window is still invalidated, though, so the OS will ask your program to repaint that area later, when you next process a wm_Paint message.
I don't know why you would expect your image to look any different, and it has nothing to do with invalidating the scroll box. It looks like you clicked on the character's eye, and the dragged the mouse down and the the right, clockwise.
At each mouse movement, you draw a new line from the original pint to the current mouse position. You draw the line directly on the currently displayed bitmap, and then you ask the image control to redraw itself. It obeys and draws the bitmap — that bitmap that you just added another line to.
I suspect what you intended to happen was for each mouse movement to result in one black line to appear over an otherwise unsullied image. InvalidateRect won't help with that. You need to redraw the original image over the previous line position, and the draw the new line. InvalidateRect does not help you "undo" a previous graphic operation. It just tells the OS that a certain portion of a window should be repainted sometime. It doesn't say what colors those invalidated pixels should be repainted with. That's what wm_Paint is for.
Related
I want know how detect to what side i'm moving mouse: to left, right, top, bottom inside TImage component on mousemove event?
Thank you.
Here's an example to be used in an FMX project. For a VCL project, you would use integer variables.
First, declare two variables Xold, Yold: single; for example in the private section of the form.
private
Xold, Yold: Single;
Initialize these variables e.g. in the forms OnCreate() event. Using NaN requires System.Math in the uses clause.
procedure TForm5.FormCreate(Sender: TObject);
begin
Xold := NaN;
Yold := NaN;
end;
Then, in the OnMouseMove() event, calculate the movement horizontally and vertically, negative value indicate moving left or up, positive right or down.
procedure TForm5.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Single);
var
horz, vert: Single;
begin
if not IsNan(Xold) then horz := X - Xold else horz := 0;
if not IsNan(Yold) then vert := Y - Yold else vert := 0;
Xold := X; // save new values
Yold := Y; //
// use horz and vert as needed
Label1.Text := Format('h: %f, v: %f',[horz, vert]);
end;
You may also want to reset the Xold and Yold variables to NaN when the mouse leaves the image.
procedure TForm5.Image1MouseLeave(Sender: TObject);
begin
Xold := NaN;
Yold := NaN;
end;
It was asked in comments, why initialize to NaN instead of just zero? Xold := 0; Yold := 0 is the top-left corner. If the mouse entry to the image happens at e.g. right side, the first move would be a jump from 0 to image width. Using NaN we can omit the first entry as a move and just store the entry point in Xold and Yold for use with next move.
I am using TCanvas to draw a blue line whenever the left mouse button is clicked, and a red line whenever the right mouse button is clicked. Currently whenever I click another line is drawn on the chart. What I want to do is to clear the old line, and then draw a new line.
Below is some sample code.
The code for the onClick event
procedure TForm2.Chart1ChartClick(Sender: TJvChart; Button: TMouseButton;
Shift: TShiftState; X, Y, ChartValueIndex, ChartPenIndex: Integer;
var ShowHint, HintFirstLineBold: Boolean; HintStrs: TStrings);
begin
if Button = mbLeft then
begin
canvas.pen.color := clblue;
PlotHorizontal(X, Y);
end
else if Button = mbRight then
begin
Canvas.Pen.color := clred;
PlotHorizontal(X, Y);
end
end;
The PlotHorizontal procedure
procedure TForm2.PlotHorizontal(X, Y : integer);
begin
with canvas do
begin
// draw the horizontal line
MoveTo(X, Y);
// without the 4 the line doesn't seem to reach the end of the graph
LineTo(X, Chart1.Options.XStartOffset -4);
MoveTo(X, Y);
LineTo(X, Chart1.Options.XStartOffset +
+ Chart1.Options.Yend);
end;
end;
I was able to get it working, by saving the old X, and Y values for where the lines were draw. Then when the mouse was clicked again, I refreshed the chart, and redrew the line again.
Following code makes quite some troubles:
procedure TForm1.Button1Click(Sender: TObject);
var dc : HDC;
meta : TMetafile;
metaCanv : TMetafileCanvas;
cr : TRect;
sz : TSize;
begin
dc := GetDC(0);
SetWindowExtEx(dc, 4800, 1300, #sz);
ShowMessage(Format('size %d, %d', [sz.cx, sz.cy]));
meta := TMetafile.Create;
meta.SetSize(4500, 1300);
metaCanv := TMetafileCanvas.Create(meta, dc);
try
IntersectClipRect(metaCanv.Handle, 0, 0, 4600, 1300);
cr := metaCanv.ClipRect;
with cr do
ShowMessage(Format('clip rect: %d, %d, %d, %d', [Top, Left, Bottom, Right]));
finally
metaCanv.Free;
meta.Free;
end;
DeleteDC(dc);
end;
The problem is that the clipping rectangle is bound to the display resolution e.g. if your screen has 1920 pixels width the clipping rectangle is bound to this value.
Note it is NOT a problem to remove clipping at all and paint lines event to the complete bottom rect corner. The problem arises if a clipping region is set (e.g. to the complete metafile width/height as shown in the example) and then paint the line -> it is clipped to the screen width/height.
I know that I could use e.g. a printer dc as reference which will basically fix the problem but there are a few side effects (e.g. gdi+ drawing on metafiles with such
dc's simply does not work).
Anyone knows how to "trick" the system such that this odd clipping behaviour is not
there any more?
ClipRect being the only part in which you can draw is a false presumption.
The documentation on TCustomCanvas.ClipRect:
Use ClipRect to determine where the canvas needs painting.
This is easily verified by drawing beyond ClipRect and trying to show what has been drawn, for example as follows:
procedure TForm1.Button1Click(Sender: TObject);
var
MetaFile: TMetafile;
MetaCanvas: TMetafileCanvas;
begin
MetaFile := TMetafile.Create;
try
MetaCanvas := TMetafileCanvas.Create(MetaFile, 0);
try
MetaFile.SetSize(4500, 1300);
MetaCanvas.LineTo(4500, 1300);
finally
MetaCanvas.Free;
end;
Canvas.Draw(-4400, -1200, MetaFile);
finally
MetaFile.Free;
end;
end;
I'm trying to make a small game based on the canvas in Delphi. Basically, I'd like to make a fairly large bitmap ( 3000x3000, for example ), then load it into the canvas, and being able to scroll right/left/up/down just like an ordinary image viewer, however I can't seem to find what I'm looking for. Any ideas?
Load the image to an off-screen TBitmap object. Then, OnPaint, or whenever is suitable in your particular application, use BitBlt or Canvas.Draw to draw a rectangular subimage of the TBitmap onto the canvas. The subpart should start at (X, Y) on the TBitmap and have a width and height equal to ClientWidth and ClientHeight of the form, respectively.
Now, respond to keyboard events. Write a FormKeyDown event handler, and listen to Key = VK_LEFT, Key = VK_RIGHT, Key = VK_UP, and Key = VK_DOWN (use a case statement). When you detect such a key being pressed, increase/decrease X or Y, as appropriate, and paint the scene again using this starting point.
You can also respond to the MouseDown, MouseMove, and MouseUp events to scroll using the mouse. Either you can use the middle one only (MouseMove): You can check if the cursor is near an edge of the form, and if so, scroll in this direction smoothly (using a TTimer, for instance). Alternatively, you can set a FMouseDown flag to true in MouseDown, and reset it to false in MouseUp. Then, in MouseMove, scroll the bitmap by a delta X-XOld in the x direction if FMouseDown is true, and a delta Y-YOld in the y direction. (Here, X and Y are parameters of the MouseMove event handler; (X, Y) is the current position of the cursor.) The MouseMove procedure should end with
XOld := X;
YOld := Y;
no matter if FMouseDown is on or off.
I had the same problem. My Bitmap is about 5000x5000 pixel, loaded into an Timage of 500x500 pixels.
I wrote a code to move the bitmap arround in the Timage, and it cant go out of the "borders"
AlteMausPos is declared in Form1 var at the beginning.
Kerzenbitmap is your bitmap that contains the 5000x5000 picture.
MausPosDifferenz contains the absolut amount of pixels(x,y) you have moved your mouse while mousekey down.
Then it checks if everything is in Range of the bitmap before copying it with CopyRect.
It took some time for my brain to find out that the best way to copy the rect ist to use the absolut changed mouseposition.
procedure Form1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var picLimits: Tpoint;
begin
AlteMausPos.X := X;
AlteMausPos.Y := Y;
end;
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var SourceRect, DestRect: TRect;
begin
var tempBMP:= Tbitmap.Create;
MausPosDifferenz.X := MausPosDifferenz.X+ (AlteMausPos.X- X);
MausPosDifferenz.Y := MausPosDifferenz.Y+ (AlteMausPos.Y- Y);
if MausPosDifferenz.X >= Kerzenbitmap.Width- Image1.Width then MausPosDifferenz.X := Kerzenbitmap.Width-Image1.Width;
if MausPosDifferenz.X < 0 then MausPosDifferenz.X:=0;
if MausPosDifferenz.Y >= Kerzenbitmap.Height-Image1.Height then MausPosDifferenz.Y := Kerzenbitmap.Height-Image1.Height;
if MausPosDifferenz.Y < 0 then MausPosDifferenz.Y:=0;
SourceRect:= Rect( MausPosDifferenz.X, MausPosDifferenz.Y, Image1.Width+ MausPosDifferenz.X, Image1.Height+ MausPosDifferenz.Y);
DestRect:= Rect( 0,0, Image1.Width, Image1.Height);
tempBMP.Assign(Kerzenbitmap);
TempBMP.Canvas.CopyRect(DestRect, Kerzenbitmap.Canvas, SourceRect);
Image1.Picture.Assign(tempBMP);
tempBMP.Free;
end;
Using the following code in Delphi 2007:
procedure TfrmTest.PaintBox1Paint(Sender: TObject);
const
Rect_Size = 10;
begin
PaintBox1.Canvas.Brush.Color := clYellow;
PaintBox1.Canvas.FillRect(Rect(0, 0, PaintBox1.width, PaintBox1.height));
PaintBox1.Canvas.Brush.Color := clRed;
DrawARect(PaintBox1.Canvas, 0, 0, Rect_Size, Rect_Size);
end;
procedure TfrmTest.DrawARect(ACanvas: TCanvas; iLeft, iTop, iWidth, iHeight: Integer);
var
rgnMain: HRGN;
begin
rgnMain := CreateRectRgn(iLeft, iTop, iLeft + iWidth, iTop + iHeight);
try
SelectClipRgn(ACanvas.handle, rgnMain);
ACanvas.FillRect(ACanvas.ClipRect);
SelectClipRgn(ACanvas.handle, 0);
finally
DeleteObject(rgnMain);
end;
end;
I get this:
(Yellow area shows boundaries of PaintBox1).
alt text http://www.freeimagehosting.net/uploads/62cf687d29.jpg
(Image shows a form with a yellow box [PaintBox1] in the center. However my red rectange [rgnMain] has been drawn at pos 0,0 on the form)
My expectation was that the red rectangle would be at the top left of the PaintBox1 canvas, not the form's canvas. Why is it not? Can regions only be used with controls that have a Windows handle?
Thanks
Device Contexts require a window handle. What VCL does for non-windowed controls is to offset the view port of the DC acquired for the TWinControl they are on, by using SetWindowOrgEx in TWinControl.PaintControls. The new view port is in logical units. So for 'TGraphicControl's, which does not descend from TWinControl, you can use GDI functions which work on logical coordinates. See the remarks section for SelectClipRgn, which says the coordinates should be specified in device units. You'd offset the region or the coordinates.