Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Need to Disable the Window Close Button (Upper right X)
Confused on using API.
The hwnd (A handle to a window) is giving a compile error...

     BC30451: "hwnd" as not declared. It may be inaccessible due to its
     protection level.

hwnd relates to a Handle to a WiNDow.  So how do I get the window handle 
that I want (need) to manipulate and pass that in as the hwnd value (that must
be declared)?

The following is the VB code and RemoveMenu at the end is where the 
compile error is...

    Private Declare Function GetSystemMenu Lib "user32" _
   (ByVal hwnd As Integer, ByVal bRevert As Long) As _
   Long

    Private Declare Function RemoveMenu Lib "user32" _
   (ByVal hMenu As Integer, ByVal nPosition As Long,
   ByVal wFlags As Long) As Long

    Const MF_BYPOSITION = &H400
    Const MF_APPEND = &H100&

    Private Sub Form_Load()
        RemoveMenu(GetSystemMenu(hwnd, 0), 2, MF_BYPOSITION)
    End Sub

What is the window handle (hwnd) needed for GetSystemMenu?

Thanks in advance.


What I have tried:

All the research I done does not explain how to get window handle.
The VB code I copied seems to be the least complexed.
Using Microsoft Visual Studio Community 2019 Version 16.8.4 VB.NET WPF
Posted
Updated 28-Jul-21 4:33am

You can't remove the Close box without also removing the Min and Max boxes: windows doesn't provide any way of doing that. This may help though: Disable the Close box on a form[^]
 
Share this answer
 
Comments
Vince McElwee 28-Jul-21 21:55pm    
The "Disable the Close box on a form" quickest solution using...
Form.ControlBox = False
however my Visual Studio Visual Basic WPF flagged "Form" with a
error message...
BC30188: Declaration Expected.
Can you explain and help to correct?
Thanks.
OriginalGriff 29-Jul-21 0:29am    
From a single line of code in isolation? Probably not, unless ... you did replace the class name "Form" with your actual instance variable name, or with "this" ... didn't you?
Vince McElwee 29-Jul-21 20:01pm    
I wish to thank you for your patience and
quick responses. You seem to be very knowledgeable
in helping me with this obstacle of mine.

It seems all my documentation research uses C# as
examples instead of VB .NET WPF. This makes it
difficult for me to translate it into the VB code.

Yes, the Form.ControlBox = False VB statement is written
on a single line of code in isolation. I have no idea what "Form"
needs to be replaced with and its value.

No, I did not replace the class name "Form" with a actual instance
variable name, or with "this". The documentation says the "this"
keyword refers to the current instance of the class. I experimented
with the following VB statements:

VB Code Test #1...
this.ControlBox = False

"this" is flagged the same as "form" by Visual Studio with...

BC30188: Declaration Expected.

VB Code Test #2..
Dim hwnd As IntPtr = New System.Windows.Interop.WindowInteropHelper(this).Handle

and "this" is flagged by Visual Studio with...

BC30451: 'this' as not declared. It may be inaccessible due to its
protection level.

If you can use your expertise on what I'm failing to do, it would be
appreciated.

Perhaps you can write some VB .NET WPF code to prove results
to remove the form Close (X) button.

During my research I found the following article (again it is using C#):

How to hide close button in WPF window?
https://stackoverflow.com/questions/743906/how-to-hide-close-button-in-wpf-window

I know your time must be valuable, I can only express my sincere Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900