Recent

Author Topic: Quirky windows  (Read 2822 times)

440bx

  • Hero Member
  • *****
  • Posts: 4743
Quirky windows
« on: April 21, 2024, 04:32:04 am »
Hello,

The following code might be the shortest possible Windows GUI program and the behavior is a bit unusual in some cases (hint: the scrollbar is the most fun to play with.)  Here is the code:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE GUI}
  2.  
  3. program FunMess;
  4.   { short and quirky GUI programs                                             }
  5.  
  6.   {---------------------------------------------------------------------------}
  7.   { UNUSUAL: creating a main window that is a standard control may give the   }
  8.   { window a caption but, the caption isn't always active/functional.         }
  9.  
  10.   { 1. Button    - ignores the button styles,                                 }
  11.   {                caption is partially functional                            }
  12.   { 2. Listbox   - caption is partially functional                            }
  13.   { 3. Scrollbar - when used, the scroll bars get redrawn incorrectly, the    }
  14.   {                caption if partially functional                            }
  15.   { 4. Static    - the caption is utterly useless                             }
  16.   { 5. Edit      - caption is partially functional                            }
  17.  
  18.   { 1. Combobox  - there is no caption                                        }
  19.  
  20.   { for all the above, the window style is mostly ignored                     }
  21.   {---------------------------------------------------------------------------}
  22.  
  23. uses
  24.   Windows
  25.   ;
  26.  
  27. function WinMain : integer;
  28.   { application entry point                                                   }
  29. var
  30.   Wnd : HWND;
  31.   Msg : TMSG;
  32.  
  33. begin
  34.   { Create the main application window                                        }
  35.  
  36.                        { uncomment your choice of control, recompile and run  }
  37.  
  38.   Wnd := CreateWindow ('listbox',
  39.                        //'scrollbar',
  40.                        //'static',
  41.                        //'edit',
  42.                        //'Combobox',
  43.                        //'Button',              { class name                  }
  44.  
  45.                        'Hello World',           { window caption text         }
  46.                        0,                       { window style                }
  47.  
  48.                        10,
  49.                        10,
  50.                        100,
  51.                        100,
  52.  
  53.                        0,
  54.                        0,
  55.                        GetModuleHandle(nil),    { instance handle             }
  56.                        nil);                    { parameter sent to WM_CREATE }
  57.  
  58.   if Wnd = 0 then halt;                         { could not create the window }
  59.   ShowWindow(Wnd, SW_NORMAL);
  60.  
  61.   while GetMessage (Msg, 0, 0, 0) do            { pump and dispatch messages  }
  62.   begin
  63.     DispatchMessage(Msg);
  64.   end;
  65.  
  66.  
  67.   WinMain := Msg.wParam;                        { terminate with return code  }
  68. end;
  69.  
  70. begin
  71.   WinMain;
  72. end.
  73.  
you can copy/paste that into a Lazarus source editor window or if you like your code with a side of .lpi and .lps, download the attachment.

NOTE: you will almost every time have to forcefully terminate the little program (use Task Manager or whatever system monitoring program you like/use.)

Have fun!
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

ASerge

  • Hero Member
  • *****
  • Posts: 2337
Re: Quirky windows
« Reply #1 on: April 21, 2024, 10:18:40 am »
The following code might be the shortest possible Windows GUI program...
It can be shorten by removing ShowWindow, but specifying the WS_VISIBLE style.

Quote
NOTE: you will almost every time have to forcefully terminate the little program (use Task Manager or whatever system monitoring program you like/use.)
Hook WM_DESTROY to terminate app:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE GUI}
  2.  
  3. uses Windows, CommCtrl;
  4.  
  5. function SubclassProc(Wnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM;
  6.   uIdSubclass: UINT_PTR; dwRefData: DWORD_PTR): LRESULT; stdcall;
  7. begin
  8.   if uMsg = WM_DESTROY then
  9.     PostQuitMessage(0);
  10.   Result := DefSubclassProc(Wnd, uMsg, wParam, lParam);
  11. end;
  12.  
  13. function WinMain: Integer;
  14. var
  15.   Wnd: HWND;
  16.   Msg: TMSG;
  17. begin
  18.   Wnd := CreateWindow(
  19.     //'listbox',
  20.     'scrollbar',
  21.     //'static',
  22.     //'edit',
  23.     //'Combobox',
  24.     //'Button',
  25.  
  26.     'Hello World',
  27.     WS_VISIBLE,
  28.     10, 10, 100, 100, 0, 0, 0, nil);
  29.   if Wnd = 0 then
  30.     Halt;
  31.   SetWindowSubclass(Wnd, @SubclassProc, 1, 0);
  32.   while GetMessage(Msg, 0, 0, 0) do
  33.     DispatchMessage(Msg);
  34.   Result := Msg.wParam;
  35. end;
  36.  
  37. begin
  38.   WinMain;
  39. end.

440bx

  • Hero Member
  • *****
  • Posts: 4743
Re: Quirky windows
« Reply #2 on: April 21, 2024, 03:20:03 pm »
It can be shorten by removing ShowWindow, but specifying the WS_VISIBLE style.
Good idea! 

Hook WM_DESTROY to terminate app:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE GUI}
  2.  
  3. uses Windows, CommCtrl;
  4.  
  5. function SubclassProc(Wnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM;
  6.   uIdSubclass: UINT_PTR; dwRefData: DWORD_PTR): LRESULT; stdcall;
  7. begin
  8.   if uMsg = WM_DESTROY then
  9.     PostQuitMessage(0);
  10.   Result := DefSubclassProc(Wnd, uMsg, wParam, lParam);
  11. end;
  12.  
  13. function WinMain: Integer;
  14. var
  15.   Wnd: HWND;
  16.   Msg: TMSG;
  17. begin
  18.   Wnd := CreateWindow(
  19.     //'listbox',
  20.     'scrollbar',
  21.     //'static',
  22.     //'edit',
  23.     //'Combobox',
  24.     //'Button',
  25.  
  26.     'Hello World',
  27.     WS_VISIBLE,
  28.     10, 10, 100, 100, 0, 0, 0, nil);
  29.   if Wnd = 0 then
  30.     Halt;
  31.   SetWindowSubclass(Wnd, @SubclassProc, 1, 0);
  32.   while GetMessage(Msg, 0, 0, 0) do
  33.     DispatchMessage(Msg);
  34.   Result := Msg.wParam;
  35. end;
  36.  
  37. begin
  38.   WinMain;
  39. end.
I didn't want to incur the extra code and just thought of the fact that alt-F4 might work and, it does!  therefore, no need for that code, Alt-F4 ends the program.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

ASerge

  • Hero Member
  • *****
  • Posts: 2337
Re: Quirky windows
« Reply #3 on: April 21, 2024, 09:51:59 pm »
I didn't want to incur the extra code and just thought of the fact that alt-F4 might work and, it does!  therefore, no need for that code, Alt-F4 ends the program.
Windows 7, x64. The Alt+F4 keyboard shortcut does not terminate the program (I tested all the controls).

440bx

  • Hero Member
  • *****
  • Posts: 4743
Re: Quirky windows
« Reply #4 on: April 21, 2024, 10:16:07 pm »
Windows 7, x64. The Alt+F4 keyboard shortcut does not terminate the program (I tested all the controls).
You're right !!  It makes the window disappear but the program is still running, which on second thought makes sense because the control isn't supposed to post a quit message.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

ASerge

  • Hero Member
  • *****
  • Posts: 2337
Re: Quirky windows
« Reply #5 on: April 22, 2024, 01:09:24 am »
...because the control isn't supposed to post a quit message.
Therefore, for the correct behavior of the program, it is necessary to hook the WM_DESTROY message.

440bx

  • Hero Member
  • *****
  • Posts: 4743
Re: Quirky windows
« Reply #6 on: April 22, 2024, 01:20:54 am »
Therefore, for the correct behavior of the program, it is necessary to hook the WM_DESTROY message.
Yes, true but, it is a bit difficult to conceive of a useful program that consists of only a standard control.

IOW, adding the hook doesn't make the program useful and, if code is added to make the program useful in some way, it is very unlikely that hooking the control's window proc would be necessary because the additional code that makes the program useful could call PostQuitMessage to make it end properly.

All that said, it is nice to have the program terminate as it should but it was already a given that the program was "peculiar" from the start.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

tetrastes

  • Hero Member
  • *****
  • Posts: 595
Re: Quirky windows
« Reply #7 on: April 22, 2024, 10:07:12 am »
Code: Pascal  [Select][+][-]
  1. {$APPTYPE GUI}
  2.  
  3. uses windows;
  4.  
  5. {$R *.res}
  6.  
  7. begin
  8.   windows.MessageBox(0, 'Or not?', nil, 0);
  9. end.
  10.  

ASerge

  • Hero Member
  • *****
  • Posts: 2337
Re: Quirky windows
« Reply #8 on: April 22, 2024, 06:23:20 pm »
Code: Pascal  [Select][+][-]
  1. {$APPTYPE GUI}
  2. begin
  3.   Error(reNone);
  4. end.
8-)

tetrastes

  • Hero Member
  • *****
  • Posts: 595
Re: Quirky windows
« Reply #9 on: April 23, 2024, 08:59:06 am »
Code: Pascal  [Select][+][-]
  1. {$APPTYPE GUI}
  2. begin
  3.   RunError;
  4. end.
:D

ASerge

  • Hero Member
  • *****
  • Posts: 2337
Re: Quirky windows
« Reply #10 on: April 23, 2024, 07:07:16 pm »

 

TinyPortal © 2005-2018