Disable close of applications CMD window by disabling the [X] button - help migrate VB code

606 views
Skip to first unread message

John Ward

unread,
Aug 28, 2019, 7:16:11 AM8/28/19
to Harbour Users
I have found VB code which uses the user32 library and the RemoveMenu function to disable the [X] button.

Has anyone migrated code similar to the below to prevent someone from improperly closing a Clipper Migrated Harbour application running in CMD window (launched via a batch file)?   

I need them to properly exit the application only using the method provided within the application.

The VB code I found:

 Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
             (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
 Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
 Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal _
                             wFlags As Long) As Long
 Private Const MF_BYPOSITION = &H400


 Private Sub Command1_Click()
 Dim cmdHwnd As Long, SysMenu As Long, Res As Long
 cmdHwnd = FindWindow(vbNullString, "C:\WINDOWS\system32\cmd.exe")
 If cmdHwnd <> 0 Then
     SysMenu = GetSystemMenu(cmdHwnd, 0)
     Res = RemoveMenu(SysMenu, 6, MF_BYPOSITION)
     SetForegroundWindow cmdHwnd
 End If
 End Sub

Any help in migrating this code would greatly be appreciated.

Thank you in advance!

fdaniele

unread,
Aug 28, 2019, 8:07:37 AM8/28/19
to Harbour Users

NOSYSMENU in the define of the windows

by(t)e 

Daniele



Klas Engwall

unread,
Aug 28, 2019, 1:24:50 PM8/28/19
to harbou...@googlegroups.com
Hi John,

> I have found VB code which uses the user32 library and the RemoveMenu
> function to disable the [X] button.
>
> Has anyone migrated code similar to the below to prevent someone from
> improperly closing a Clipper Migrated Harbour application running in CMD
> window (launched via a batch file)?
>
> I need them to properly exit the application only using the method
> provided within the application.

In a console window you can disable it by redirecting the HB_K_CLOSE key
to a UDF that either just returns NIL or displays a message of your choice:

SetKey( HB_K_CLOSE, { || DisableCloseButton() } )
at the top of the application

function DisableCloseButton()
alert( "Keep off that button" )
return NIL

Regards,
Klas

Mauricio Ventura Faria

unread,
Aug 28, 2019, 2:06:14 PM8/28/19
to Harbour Users
#include "hbgtinfo.ch"
HB_GtInfo( HB_GTI_CLOSABLE, .F.)

Maurício Faria

unread,
Aug 30, 2019, 3:02:09 AM8/30/19
to harbou...@googlegroups.com, John Ward
#include "hbgtinfo.ch"
HB_GtInfo( HB_GTI_CLOSABLE, .F.)

--
--
You received this message because you are subscribed to the Google
Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: http://groups.google.com/group/harbour-users

---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/a5c67790-e0df-4a57-9be2-3e548058502a%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages