Recent

Author Topic: [SOLVED] Captionless form  (Read 686 times)

pcurtis

  • Sr. Member
  • ****
  • Posts: 351
[SOLVED] Captionless form
« on: October 19, 2020, 03:06:48 am »
Hi All,

Is it possible to create a form without a caption but WITH a border? (like BorderStyle := bsNone, but with an outline)

(Win 10)

Thanks in advance.
« Last Edit: October 19, 2020, 05:39:49 pm by pcurtis »
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

Handoko

  • Hero Member
  • *****
  • Posts: 3913
  • My goal: build my own game engine using Lazarus
Re: Captionless form
« Reply #1 on: October 19, 2020, 03:58:14 am »
I haven't tested on Win10 but on Linux this code works, it should work too on Windows.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, ExtCtrls, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Label1: TLabel;
  16.     Label2: TLabel;
  17.     Shape1: TShape;
  18.     procedure Button1Click(Sender: TObject);
  19.     procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
  20.       Shift: TShiftState; X, Y: Integer);
  21.     procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  22.     procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
  23.       Shift: TShiftState; X, Y: Integer);
  24.     procedure FormPaint(Sender: TObject);
  25.     procedure Shape1MouseUp(Sender: TObject; Button: TMouseButton;
  26.       Shift: TShiftState; X, Y: Integer);
  27.   private
  28.     FClicking:   Boolean;
  29.     FMousePoint: TPoint;
  30.     FFormPoint:  TPoint;
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37.  
  38. {$R *.lfm}
  39.  
  40. { TForm1 }
  41.  
  42. procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  43.   Shift: TShiftState; X, Y: Integer);
  44. begin
  45.   case Button of
  46.     mbLeft:
  47.       begin
  48.         if not(FClicking) then
  49.         begin
  50.           FMousePoint := Mouse.CursorPos;
  51.           FFormPoint  := Point(Left, Top);
  52.         end;
  53.         FClicking := True;
  54.         Shape1.Brush.Color := clRed;
  55.       end;
  56.     mbRight:
  57.       Shape1.Brush.Color := clYellow;
  58.   end;
  59. end;
  60.  
  61. procedure TForm1.Button1Click(Sender: TObject);
  62. begin
  63.   Close;
  64. end;
  65.  
  66. procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  67.   Y: Integer);
  68. begin
  69.   if not(FClicking) then Exit;
  70.   Left := Mouse.CursorPos.X - (FMousePoint.X - FFormPoint.X);
  71.   Top  := Mouse.CursorPos.Y - (FMousePoint.Y - FFormPoint.Y);
  72. end;
  73.  
  74. procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  75.   Shift: TShiftState; X, Y: Integer);
  76. begin
  77.   FClicking := False;
  78.   Shape1.Brush.Color := clWhite;
  79. end;
  80.  
  81. procedure TForm1.FormPaint(Sender: TObject);
  82. begin
  83.   with Canvas do
  84.   begin
  85.     Pen.Width := 5;
  86.     Pen.Color := clRed;
  87.     Rectangle(0, 0, Width, Height);
  88.   end;
  89. end;
  90.  
  91. procedure TForm1.Shape1MouseUp(Sender: TObject; Button: TMouseButton;
  92.   Shift: TShiftState; X, Y: Integer);
  93. begin
  94.   Close;
  95. end;
  96.  
  97. end.

The problem with captionless form is you have provide the code to let users to move and close the form. Also minimizing, resizing and maximizing.

~edit~
I forget to initialize the private fields. You should do it in Form.OnCreate event.
« Last Edit: October 19, 2020, 04:41:23 am by Handoko »

pcurtis

  • Sr. Member
  • ****
  • Posts: 351
Re: Captionless form
« Reply #2 on: October 19, 2020, 04:27:54 am »
Thanks. Oops, one small problem I forgot to mention, I also need the shadow around the form.
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

jamie

  • Hero Member
  • *****
  • Posts: 3797
Re: Captionless form
« Reply #3 on: October 19, 2020, 03:52:30 pm »
I assume you are looking for the fuzzy shadow casting that windows does on the desktop ?

 if not, you can try this

Set the form for a single border in design mode;

and at runtime, maybe like in the formcreate do this.

 setwindowlong(Form1.WindowHandle, GWL_STYLE, GetWindowLong(form1.WindowHandle,GWL_STYLE) and (not WS_BORDER));

 you need the WINDOWS unit in the uses list
this will give you the 3D shadow drop like a button..
The only true wisdom is knowing you know nothing

pcurtis

  • Sr. Member
  • ****
  • Posts: 351
Re: Captionless form
« Reply #4 on: October 19, 2020, 04:28:22 pm »
Jamie - I want a borderless form with a drop shadow. When I use you code (with = bsNone) there is no dropshadow. If I set BorderStyle = bsSingle then there is automatically a dropshadow.
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

jamie

  • Hero Member
  • *****
  • Posts: 3797
Re: Captionless form
« Reply #5 on: October 19, 2020, 04:30:45 pm »
I said, a form with a single border.. not no border..

without border you won't see anything around it.

so set the form in the OI as single outline and at runtime apply that code I posted.

 like I said it gives you a 3D effect, it most likely isn't what you want but it does give you a border of 3d.
The only true wisdom is knowing you know nothing

pcurtis

  • Sr. Member
  • ****
  • Posts: 351
Re: Captionless form
« Reply #6 on: October 19, 2020, 04:35:54 pm »
What I meant was that setting BS = bsSingle then there is an automatic dropshadow. Therefore the code is pointless.
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

Handoko

  • Hero Member
  • *****
  • Posts: 3913
  • My goal: build my own game engine using Lazarus
Re: Captionless form
« Reply #7 on: October 19, 2020, 04:53:02 pm »
@pcurtis

On my Ubuntu Mate, my demo shows soft shadow. I believe that is handled by window decorator, which can be changed on the appearance/theme settings. Does my Captionless Dragable Form not show the shadow on Windows?

If you want it on normal forms, I have no idea how to do it. But if you want captionless splash form with soft or drop shadow, I think I know how to do it. I observed Corel Graphics Suite splash screens, I believe this is how they do it:

1. On starting the program do a screen capture
2. Paint a drop or soft shadow on the captured image
3. Crop the image and put it on the Form.OnPaint
4. Draw the logo, name and copyright information
5. Now the form can have semi-transparent shadow on the desktop background
6. User should not be allowed to move the splash screen

This is the examples of splash form I meant:
https://community.coreldraw.com/talk/coreldraw_graphics_suite_x7/f/coreldraw-graphics-suite-feature-requests/17401/my-welcome-screen-for-x5

The drop/soft shadow should be able done using BGRABitmap. For screen capturing, it has been discussed on the forum you can search for it.

Below is the previous demo showing the shadow, which is provided automatically by the window decorator on Ubuntu Mate:
« Last Edit: October 19, 2020, 05:04:07 pm by Handoko »

pcurtis

  • Sr. Member
  • ****
  • Posts: 351
Re: Captionless form
« Reply #8 on: October 19, 2020, 05:04:02 pm »
On Windows it doesn't show a shadow.

The form will be a "normal" form so the suggestions don't really help.
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

jamie

  • Hero Member
  • *****
  • Posts: 3797
Re: Captionless form
« Reply #9 on: October 19, 2020, 05:04:19 pm »
What I meant was that setting BS = bsSingle then there is an automatic dropshadow. Therefore the code is pointless.

You are absolutely correct in your assertion

"Its pointless"

The only true wisdom is knowing you know nothing

pcurtis

  • Sr. Member
  • ****
  • Posts: 351
Re: Captionless form
« Reply #10 on: October 19, 2020, 05:10:27 pm »
Does that mean it can't be done?
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

Handoko

  • Hero Member
  • *****
  • Posts: 3913
  • My goal: build my own game engine using Lazarus
Re: Captionless form
« Reply #11 on: October 19, 2020, 05:16:35 pm »
It can. But it won't be easy. Try to start a new thread with a catchy title like "Challenge: Can Anyone Do ...". I believe you will get the answers some days later. This forum is full of experts who can do many things that you can't imagine. I wish to take the challenge unfortunately I am busy at the moment.
« Last Edit: October 19, 2020, 05:18:39 pm by Handoko »

rvk

  • Hero Member
  • *****
  • Posts: 4402
Re: Captionless form
« Reply #12 on: October 19, 2020, 05:19:47 pm »
Does that mean it can't be done?
Not easily.

I think the dropshadow is connected with the WS_BORDER. Removing it will remove the shadow.

There are some things like DwmExtendFrameIntoClientArea with which you can regain the shadow but all the examples are for Delphi or C++.
(for example https://stackoverflow.com/questions/43818022/borderless-window-with-drop-shadow)
« Last Edit: October 19, 2020, 05:21:24 pm by rvk »

jamie

  • Hero Member
  • *****
  • Posts: 3797
Re: Captionless form
« Reply #13 on: October 19, 2020, 05:29:12 pm »
with a borderless form do this at some point in code

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   Hide;
  4.   setclasslong(Form1.WindowHandle, GCL_STYLE, GetClassLong(form1.WindowHandle,GCL_STYLE) or CS_DropShadow);
  5.   Show;
  6. end;                          
  7.  
The only true wisdom is knowing you know nothing

pcurtis

  • Sr. Member
  • ****
  • Posts: 351
Re: Captionless form
« Reply #14 on: October 19, 2020, 05:39:07 pm »
Jamie - Thanks. Job done.
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

 

TinyPortal © 2005-2018