0

I have a code that creates a hole in Form using the mouse.

var
 FormRegion, HoleRegion: HRGN;
begin
  FormRegion := CreateRectRgn(0, 0, Form1.Width, Form1.Height);
  HoleRegion := CreateRectRgn(X1, Y1, X2, Y2);

  CombineRgn(FormRegion, FormRegion, HoleRegion, RGN_DIFF);
  SetWindowRgn(Form1.Handle, FormRegion, True);
end;

Now i wish put one Panel (that already have a fixed height) always above of hole region (and with same width of hole) to simulate a title bar, something like this:

enter image description here

How can be made?

  • Keep track of the minimum Y coordinate and the minimum and maximum X coordinates? – Andreas Rejbrand May 30 '21 at 12:28
  • 1
    Can't answer: You don't provide enough of your code. Show a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) that we can understand and play with. – fpiette May 30 '21 at 12:37
  • @Andreas Rejbrand, tried: `SetWindowPos(Form1.Panel1.Handle, HWND_TOPMOST, X1, Y1, X2, Y2, 0);` but not worked. But i think that `SetWindowPos` is the right way, since that with right offsets. –  May 30 '21 at 13:42
  • But you have not specified what should happen if and when the non-transparent part of the combined window has a different height than the panel. Should the panel resize? Should the non-transparent part resize? Or should something else happen? Or should nothing specific happen (whatever happens, happens, so what?). – Tom Brunberg May 30 '21 at 15:01
  • @TomBrunberg, i want only `Panel` above of hole region (like showed on image). *"`Should the panel resize?`"* - Yes, the widht must be equals to width of hole region (also showed on image). This is all. –  May 30 '21 at 15:24
  • Title is non client area, a panel placed on a form is in client area. You can't place the panel where the title was, you can only place it on top of client area - which you could have cut a hole. – Sertac Akyuz May 30 '21 at 15:31
  • @SertacAkyuz, Then you is saying that not is possible locate hole region and insert a Panel above of hole and set panel width to same width of hole? –  May 30 '21 at 15:38
  • No, that is not what I'm saying. I'm saying, you can't place the panel where the window title was. And that there is a possibility that the hole won't leave room for the panel. – Sertac Akyuz May 30 '21 at 16:16
  • @SertacAkyuz, the window (hole area) not have a title bar. Is simply a hole area and Panel above hole area could be compared like a "title bar". Sorry if i expressed bad the idea. –  May 30 '21 at 17:30
  • Have yok tried setting Align property to alTop? – Delphi Coder May 30 '21 at 18:30
  • 3
    You need to set top, left and width of the panel appropriately and Panel Align must be set to none. It is just simple maths (Left:=X1, top := Y1-PanelHeight, etc). – Dsm May 30 '21 at 18:47
  • Show complete code you have so far so that we can play with it easily without spending one hour guessing what you have done. – fpiette May 30 '21 at 19:17

1 Answers1

1

You did not read carefully my request for additional information, so I leave it to you to adjust however you like.

Anyway, I believe your actual question is concerning the alignment of the panel with the transparent region. You probably do not consider that the region calculations with the form window includes the borders, so you have an offset to the right and downwards.

Since the regions are calculated including the borders of the form, you will need a variable ClientOffset: TPoint to hold the values of width of the left border and the height of the top border (incl. title bar of the form).

var
  ClientOffset: TPoint; 

To calculate the ClientOffset you can use the predefined ClientOrigin and the forms Left and Top properties.

ClientOffset.X := Form36.ClientOrigin.X - Form36.Left; // Left border width
ClientOffset.Y := Form36.ClientOrigin.Y - Form36.Top;  // Top border height (incl. title bar)

Then, either subtract ClientOffset from the panels Left and Top properties, or add ClientOffset to the HoleRegions coordinates. The latter being more correct if you use the mouse (and presumably the forms client coordinates) to define the "hole" region

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
  • Please, read @Dsm comment. Is exactly what i need, but i not know how calc **width** (Panel). Waiting suggestions. –  May 30 '21 at 21:27
  • 2
    You have declared `HoleRegion := CreateRectRgn(X1, Y1, X2, Y2);`. I guess the width of that region would be `X2 - X1`, no? Ergo, the width of the panel should be the same. – Tom Brunberg May 30 '21 at 21:31