Recent

Author Topic: Need help with Removing Caption/Title bar and Window resizing issue.  (Read 4903 times)

Firewrath

  • New Member
  • *
  • Posts: 35
Hello, I've been (slowly) learning Pascal through Lazarus/FreePascal and have started to convert an AutoHotKey script I made to Pascal, However I've ran into an issue with removing the Caption Bar from the top of my windows.
I found code on a post here:
http://forum.lazarus.freepascal.org/index.php/topic,33899.msg220717.html#msg220717
However the window shrinks each time I resize it with a key press, I figure its this code here doing it:
Height := Height - GetSystemMetrics(SM_CYCAPTION);
but when I comment that out, the Caption Bar reappears.
So does anyone have any ideas or can help me with this?
(Also its bright green in the project zip just to be 'noticeable', I copied this out of my other code to narrow the issue down to try to fix it and post it here.)
Code: Pascal  [Select][+][-]
  1. UNIT Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. INTERFACE
  6.  
  7. USES
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Windows;
  9.  
  10. TYPE
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = CLASS(TForm)
  15.     PROCEDURE FormCreate(Sender: TObject);
  16.     PROCEDURE FormKeyDown(Sender: TObject; VAR Key: Word; Shift: TShiftState);
  17.     PROCEDURE FormKeyUp(Sender: TObject; VAR Key: Word; Shift: TShiftState);
  18.   PRIVATE
  19.     { private declarations }
  20.   PUBLIC
  21.     { public declarations }
  22.   END;
  23.  
  24. VAR
  25.   Form1: TForm1;
  26.  
  27. IMPLEMENTATION
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. PROCEDURE TForm1.FormCreate(Sender: TObject);
  34. var
  35. Style: Longint;
  36. begin
  37.   DoubleBuffered := True;
  38.   // HIDE CAPTION BAR
  39.   begin
  40.     if BorderStyle = bsNone then Exit;
  41.     Style := GetWindowLong(Handle, GWL_STYLE);
  42.     if (Style and WS_CAPTION) = WS_CAPTION then
  43.     begin
  44.       case BorderStyle of
  45.         bsSingle,
  46.         bsSizeable: SetWindowLong(Handle, GWL_STYLE, Style and
  47.             (not (WS_CAPTION)) or WS_BORDER);
  48.         bsDialog: SetWindowLong(Handle, GWL_STYLE, Style and
  49.             (not (WS_CAPTION)) or DS_MODALFRAME or WS_DLGFRAME);
  50.       end;
  51.       Height := Height - GetSystemMetrics(SM_CYCAPTION);
  52.       Refresh;
  53.     end;
  54.   end;
  55. end;
  56.  
  57. PROCEDURE TForm1.FormKeyDown(Sender: TObject; VAR Key: Word; Shift: TShiftState);
  58.   var
  59.   Style: Longint;
  60.   begin
  61.   if (Shift = [ssCtrl]) then
  62.       begin
  63.        BorderStyle:=bsSizeable;
  64.          if BorderStyle = bsNone then Exit;
  65.           Style := GetWindowLong(Handle, GWL_STYLE);
  66.           if (Style and WS_CAPTION) = WS_CAPTION then
  67.             begin
  68.               case BorderStyle of
  69.                 bsSingle,
  70.                 bsSizeable: SetWindowLong(Handle, GWL_STYLE, Style and
  71.                     (not (WS_CAPTION)) or WS_BORDER);
  72.                 bsDialog: SetWindowLong(Handle, GWL_STYLE, Style and
  73.                     (not (WS_CAPTION)) or DS_MODALFRAME or WS_DLGFRAME);
  74.               end;
  75.             Height := Height - GetSystemMetrics(SM_CYCAPTION);
  76.             Refresh;
  77.           end;
  78.       END;
  79.   END;
  80.  
  81. PROCEDURE TForm1.FormKeyUp(Sender: TObject; VAR Key: Word; Shift: TShiftState);
  82. begin
  83.   if (Shift <> [ssCtrl]) then
  84.     BorderStyle:=bsNone;
  85. end;
  86.  
  87. END.
  88.  

Other Issues:
Some help with this 'KeyUp' & 'KeyDown' stuff would be nice also...
The way I have it "works", but if I press ALT or SHIFT it bugs out.
(obviously because the 'if (Shift <> [ssCtrl]) then' bit checking for any key other then CTRL.)
I want the form to be Resizable ONLY when CTRL is held down and then change back to borderless when CTRL is released (I don't want a Caption Bar in either). I've tried adding an 'else BorderStyle:=bsNone;' to the 'if (Shift = [ssCtrl]) then' but I get errors:
'unit1.pas(79,3) Fatal: Syntax error, ";" expected but "ELSE" found'
So either my 'else' isn't in the correct spot, or I'm doing it wrong. Probably the latter. :P
(I tried it on different lines 77&78, between other 'END;'s but same thing.)

Also if I resize the form while CTRL is down and release the key, the the window size is different from how I just resized it.
(I imagine that this might get fixed or have a different fix, if how I'm hiding the Caption Bar changes. So this is less important right now.)

Note: Because I live way out in the country I only have internet access once a week and thats using an old Linux netbook. So any replys I make will be very slow as I'd have to take code home and try it then wait till next week to get back to post. Sorry in advance. ^-^;
Hopefully this will be fixed in the next month or two, but It's what I have right now. -_-

Thanks for reading and any help offered, even if I am slow to reply. ^-^;
Sorry. I currently don't have Internet Access. So my replies might take a week. -_-

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Need help with Removing Caption/Title bar and Window resizing issue.
« Reply #1 on: July 18, 2018, 10:03:03 pm »
I don't use Windows so I can't help you with the first. I use BorderStyle:=bsNone when I don't want border + this way is cross-platform.

Code: [Select]
if (Shift <> [ssCtrl]) is wrong beause TShiftState is set. You should use
Code: Pascal  [Select][+][-]
  1. if not (ssCtrl in Shift)
or
Code: Pascal  [Select][+][-]
  1. if ([ssCtrl]*Shift)=[]
to test ssCtrl only, regardless of ssAlt, ssMeta, ssCapsLock etc.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Firewrath

  • New Member
  • *
  • Posts: 35
Re: Need help with Removing Caption/Title bar and Window resizing issue.
« Reply #2 on: July 18, 2018, 11:23:30 pm »
I don't use Windows so I can't help you with the first. I use BorderStyle:=bsNone when I don't want border + this way is cross-platform.

I should have been more clear. Sorry.
I don't Need the program to be Windows only. I just need to remove the Caption Bar and that Windows code was just what I found that worked.
So anything to take the Caption Bar off and allow me to change between styles while retaining the window size would be helpful.
I start off with bsNone on my form, but I want to resize it while holding down CTRL, so I have that set to change it to 'BorderStyle:=bsSizeable' but that has a Caption Bar which I don't want.

...Hrm, If I was smart I would have removed the code from 'FormCreate' that removes the Caption Bar on the form since its really useless with starting the form in bsNone anyways and see if that fixes the shrinking issue...
But that was there from me testing other styles. -_-
Added to my 'TODO' list.

And Thanks for the code for the CTRL key detection. I shall give that a try. ^-^

Otherwise I'm heading off for the day so guess I'll check the thread next week (or possibly Friday).
Sorry. I currently don't have Internet Access. So my replies might take a week. -_-

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Need help with Removing Caption/Title bar and Window resizing issue.
« Reply #3 on: July 19, 2018, 12:24:34 am »
Once you put unit Windows to uses section, it's Windows only.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Firewrath

  • New Member
  • *
  • Posts: 35
Re: Need help with Removing Caption/Title bar and Window resizing issue.
« Reply #4 on: July 25, 2018, 07:02:39 pm »
Yes, the code I found requires 'Windows' in uses, but while I need the code to run on Windows, I don't need Windows specific code.
If there is a way to make a 'bsSizable' form without the Caption/Title bar in LCL, I will happily use it.

Thanks for the help with the Keys bit though, I stupidly tried 'not' in the wrong place when I messed with it on my own. >.<
Sorry. I currently don't have Internet Access. So my replies might take a week. -_-

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Need help with Removing Caption/Title bar and Window resizing issue.
« Reply #5 on: July 25, 2018, 10:53:33 pm »
in lcl it is easier to remove everything and paint your own frame that will take care of resizing, instead of finding any working solution that will rely on the underline widgetset to do it for you.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Firewrath

  • New Member
  • *
  • Posts: 35
Re: Need help with Removing Caption/Title bar and Window resizing issue.
« Reply #6 on: August 08, 2018, 09:29:10 pm »
If anyone is curious, I went with this solution:
http://forum.lazarus.freepascal.org/index.php/topic,33844.msg220327.html#msg220327

It's not Exactly what I want but works well enough for what I'm doing.

As for painting my own frame, I wouldn't know where to start. >.>
(Well, I imagine starting with bsNone and adding panels to simulate the border for resizing, but I havnt learned enough to be there yet)
Sorry. I currently don't have Internet Access. So my replies might take a week. -_-

 

TinyPortal © 2005-2018