2. PowerBuilder
  3. Wednesday, 12 January 2022 20:10 PM UTC



PB 2021.1288


I want to control X closing button of window. What event get fired when user click X button and window get closed?




Accepted Answer
Roland Smith Accepted Answer Pending Moderation
  1. Wednesday, 12 January 2022 21:28 PM UTC
  2. PowerBuilder
  3. # Permalink

CloseQuery event.

The return from the event:

0 -- Allow the window to be closed

1 -- Prevent the window from closing

There doesn't appear to be a way of telling if the window is closing due to clicking the X or by the Close function in a script.

  1. John Fauss
  2. Wednesday, 12 January 2022 21:54 PM UTC
Tomaz - If you wish to discern if the user closes the window via a script in the window, such as in the Clicked event of a "Close" command button, for example, you can:

1. Define a Boolean instance variable in the window and set its initial value to False.

2. Prior to issuing the Close function set the instance Boolean variable to True.

3. Check the value of the instance Boolean in the window's CloseQuery event.
  1. Helpful 4
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Thursday, 13 January 2022 06:03 AM UTC
  2. PowerBuilder
  3. # 1

In addition to all the good advice given already, here's how to "disable" the red close X on the top right of a window:

Declare these external functions in your window:

EDIT: changed some uLong types for longptr (the hWnd ones):

// api functions to manipulate menu on title bar
Function ulong GetSystemMenu (longptr hWnd, boolean bRevert) Library "user32.dll" 
Function boolean DeleteMenu (ulong hMenu, uint uPosition, uint uFlags) Library "user32.dll"
Function boolean DrawMenuBar (longptr hWnd) Library "user32.dll"

And then call this script in the Open() event of the window. It will still show the "X" but in a disabled state and nothing happens when you click on it:

//// System Menu Command Values
//constant ulong SC_SIZE         = 61440
//constant ulong SC_MOVE         = 61456
//constant ulong SC_MINIMIZE     = 61472
//constant ulong SC_MAXIMIZE     = 61488
//constant ulong SC_NEXTWINDOW   = 61504
//constant ulong SC_PREVWINDOW   = 61520
//constant ulong SC_RESTORE      = 61632
constant ulong SC_CLOSE        = 61536

// Menu Flags 
constant ulong MF_BYCOMMAND       = 0

uLong hMenu
longptr hWnd

hWnd = Handle(this)

// get menu handle for this window's system menu 
hMenu = GetSystemMenu(hWnd, False)

// If successful, delete menu items from this window's system menu
// which is displayed when clicking the control box.

if hMenu > 0 then
	//Delete menu items close, size, minimize, maximize, move
	DeleteMenu(hMenu, SC_CLOSE, MF_BYCOMMAND)
	// DrawMenuBar will redraw the menu with the "deletemenu" changes applied	
end if

Just in case it might be useful to anyone.


  2. Thursday, 13 January 2022 06:54 AM UTC
Thank you Miguel!
  1. Helpful
There are no comments made yet.
  • Page :
  • 1

There are no replies made for this question yet.
However, you are not allowed to reply to this question.