* * *

Author Topic: Painting directly on the form using GDI  (Read 1071 times)

stoffman

  • New member
  • *
  • Posts: 15
Painting directly on the form using GDI
« on: May 15, 2018, 08:40:23 pm »
Hi I created the most simple app you can think of and in the FormPaint event I do the following:

procedure TForm1.FormPaint(Sender: TObject);
var
  x : HWND;
  dc : HDC;
  rect : TRect;
  blueBrush : HBRUSH;
  newPen : HPEN;
begin
  x := Handle;
  dc := GetDeviceContext(x);

  newPen := CreatePen(PS_SOLID,1,RGB(0,0,255));
  SelectObject(dc,newPen);
  movetoex(dc,70,70,nil);
  lineto(dc,500,500);

  rect := TRect.Create(100,100,100,100);
  blueBrush := CreateSolidBrush(RGB(0,123,255));
  FillRect(dc,rect,bluebrush);
end;

And while the line is being drawn the Rect doesn't. Any ideas?

knuckles

  • Full Member
  • ***
  • Posts: 122
Re: Painting directly on the form using GDI
« Reply #1 on: May 15, 2018, 08:57:35 pm »
Aren't  the rect parameters wrong? Unless I'm being stupid your making a rect at the same place with no actual size difference.

stoffman

  • New member
  • *
  • Posts: 15
Re: Painting directly on the form using GDI
« Reply #2 on: May 15, 2018, 10:01:09 pm »
You are right about this specific rectangle (although I would expect a rectangle of 1 pixel)  but this is just an example. No matter what I try it doesn't work. Very strange. I'm using 1.8rc1 I'll upgrade and test again.

Thanks anyway!

wp

  • Hero Member
  • *****
  • Posts: 4988
Re: Painting directly on the form using GDI
« Reply #3 on: May 15, 2018, 10:58:20 pm »
although I would expect a rectangle of 1 pixel
The width of a rectangle is Rect.Right - Rect.Left, i.e. when Rect.Left = Rect.Right the width is zero, not 1 pixel. How would you draw a zero-size rectangle if this case would show 1 pixel?

Side note: Of course, you may have good reasons for this, but painting using the GDI directly is the hard way. Why don't you go the easy way and use the TCanvas which Lazarus has for you?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

howardpc

  • Hero Member
  • *****
  • Posts: 2781
Re: Painting directly on the form using GDI
« Reply #4 on: May 15, 2018, 11:22:10 pm »
You need to select the object you want drawn into the device context. You also need to clean up after yourself or you will leak resources badly.
Code: Pascal  [Select]
  1. procedure TForm1.FormPaint(Sender: TObject);
  2. var
  3.   x : HWND;
  4.   dc : HDC;
  5.   rect : TRect;
  6.   blueBrush : HBRUSH;
  7.   newPen : HPEN;
  8. begin
  9.   x := Handle;
  10.   dc := GetDeviceContext(x);
  11.  
  12.   newPen := CreatePen(PS_SOLID,1,RGB(0,0,230));
  13.   SelectObject(dc,newPen);
  14.   movetoex(dc,115,115,nil);
  15.   lineto(dc,200,200);
  16.   DeleteObject(newPen);
  17.  
  18.   rect := TRect.Create(90,90,100,100);
  19.   blueBrush := CreateSolidBrush(RGB(0,123,255));
  20.   SelectObject(dc, blueBrush);
  21.   FillRect(dc,rect,bluebrush);
  22.   DeleteObject(blueBrush);
  23.  
  24.   ReleaseDC(x, dc);
  25. end;

RobbieL91

  • Newbie
  • Posts: 3
Re: Painting directly on the form using GDI
« Reply #5 on: May 17, 2018, 10:24:42 pm »
Thank you so much for the useful info.!!

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus