-1

I already know and have used these methods to create a form without boders that can be moved. Move form without border style

I'm using the WMNCHitTest override. The MouseDown event don't work at all.

This form is very simple it is displaying a countdown, a number that changes very second and that's all. The number being painted using a big TLabel with big fonts.

But I also made this form transparent by using Delphi's standard form properties. Now, if I try to click on the form to move it, the only area I can use is the lines drawing the changing numbers, even if they are not so thin, this is not practical. I'd like the user to be able to move the numbers to any position of the screen by clicking anywhere near the numbers, let's say inside a "0" or an "8".

I'd think about drawing a transparent rectangle over the numbers and be that the clickable area, but the effect would be the same, the click would go throu.

I know an application that does this, so it is possible, but how?

procedure TfrmCountDown.Timer1Timer(Sender: TObject);
begin
  iCount := iCount - 1;
  lblTime.Caption := FormatFloat('00', iCount);
end;

procedure TfrmCountDown.FormCreate(Sender: TObject);
begin
  iCount := 60;
  BorderStyle:=bsNone;
  Self.Color := clGray;
  Self.TransparentColor := true;
  Self.TransparentColorValue := clGray;
end;

procedure TfrmCountDown.WMNCHitTest(var Message: TWMNCHitTest);
var
  Pt: TPoint;
begin
  Pt := ScreenToClient(SmallPointToPoint(Message.Pos));
  if Pt.Y < 160 then
    Message.Result := HTCAPTION
  else
    inherited;
end;
Community
  • 1
  • 1
Craig Stevensson
  • 1,336
  • 4
  • 21
  • 43
  • Where is the information (code and property settings for the form) that will allow us to reproduce the problem you're asking us to solve? (Note: I did not downvote your question, but as it stands it's a "please give me code" question that asks us to debug code we can't see.) – Ken White Oct 16 '14 at 19:37
  • 1
    I will put the code, but this is not something to debug or troubleshoot, either anyone knows the answer, or don't. Could you please agree? – Craig Stevensson Oct 16 '14 at 19:38
  • 1
    It is something to debug. "I'm using the WMNCHitTest override... I made the form transparent...Now, if I try to click on the form to move it it doesn't work." is a description of code that has an issue, but you have not provided the details that allow us to reproduce the issue in order to debug it. – Ken White Oct 16 '14 at 19:42
  • An MCVE would be great. Then we could be up and running in seconds. That's the holy grail. – David Heffernan Oct 16 '14 at 19:58
  • I can use your code and, with one minor change make it work as long as you actually press the mouse button over the actual painted portion of the number, but it has to actually be over the **painted** portion of the number. If it's an empty spot (like in the center space of a `4` or inside the round parts of an `8`), it won't work for the reason @Deltics describes in an answer. – Ken White Oct 16 '14 at 20:36
  • 1
    Ken, but that's what I explained ! lol.... I said "if I try to click on the form to move it, the only area I can use is the lines drawing the changing numbers" and "I'd like the user to be able to move the numbers to any position of the screen by clicking anywhere near the numbers, let's say inside a "0" or an "8". " – Craig Stevensson Oct 16 '14 at 20:50
  • Give your labels a color, anything but 'TransparentColor' of the form. And set them 'Transparent' false. – Sertac Akyuz Oct 17 '14 at 00:24
  • @SertacAkyuz - And then they will have a background which Craig clearly wishes to avoid. – mg30rg Oct 17 '14 at 08:26
  • @mg3 - There's either something there and you can hold on to it and move it, or there's nothing there and you cannot move it.. unless you install a mouse hook I guess, wishing is not very relevant. I think faking regions with near completely transparency/partial transparency with UpdateLayeredWindow would be the most visually pleasing solution. – Sertac Akyuz Oct 17 '14 at 09:24
  • @SertacAkyuz - Yesm they would be visually pleasing, but Craig was clearly looking for a solution which does not involve visible background. Since it is a UI question installing a mouse hook, or doing any other kind of "background voodoo" workaround is more a solution than advising to redesign the UI. – mg30rg Oct 17 '14 at 09:28

1 Answers1

4

VCL Form Transparency (by which presume you to mean the TransparentColor property, as opposed to the Alpha properties) uses Layered Windows to achieve the transparent drawing. Using this technique any transparent area of the window is not only transparent visually but also with respect to hit testing.

i.e. Using VCL form transparency, the transparent areas in your form may as well not exist at all.

What could work is to turn off the VCL form transparency and instead implement your form using the WS_EX_TRANSPARENT window style. This enables visual transparency but allows you to handle hit testing to make different areas of your form transparent, or not, with respect to clicks.

Unfortunately WS_EX_TRANSPARENT is not a complete "transparency" solution - it only tells Windows that your form is transparent, but you also then have to take additional steps to actually be properly transparent, or to interpret what "transparent" means for your specific form.

This means it complicates the visual rendering of your form and you will also have to override the paint mechanism to properly draw your form. From how you describe the content on your form this does not sound like it will be too arduous however, though it is far some straightforward (I do not have a working example unfortunately).

At the very least you will probably be best to replace your TLabel with calls to select an appropriate font and render text into the window client area with a transparent background. But there will be additional house keeping required.

In your WM_NCHITTEST handler, respond with HTNOWHERE for those areas of your form which you wish to be interpreted as "click through" areas, and HTCAPTION for the areas that you wish to support dragging (i.e. from what you describe, within a region defined by the bounds of your text).

Unfortunately I think you will find that there are lots of fiddly aspects to the implementation of painting a transparent window.

Deltics
  • 22,162
  • 2
  • 42
  • 70