dear Experts.
I have a Automation Program ,Communicates with Word2010.
I want to Disable Close command on System Menu and Title bar.
I wrote this code by get Error on Line that Highlithed by Red:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Const MF_REMOVE = &H1000&

Const MF_BYPOSITION = &H400&

Dim Hwnd As Long

Dim MenuHWND As IntPtr

Hwnd = 0

Hwnd = FindWindow(Nothing, "Microsoft Word")

If Hwnd <> 0 Then

MessageBox.Show("Find Window")

MenuHWND = GetSystemMenu(Hwnd, 0)

If Not MenuHWND.Equals(IntPtr.Zero) Then

MessageBox.Show("Menu Can Handle")


End If

End If

any Solution available?

In the Form_Close event, you can use e.Cancel = True .
That will cancel the closing of the form no matter how the user initiates the closing of the form.


Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Private Declare Function GetSystemMenu Lib "user32.dll" (ByVal hWnd As Long, ByVal bRevert As Int32) As IntPtr
    Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Integer, ByVal uPosition As Integer, ByVal uFlags As Integer) As Boolean
Public sub ...
        Dim Hwnd As IntPtr
        Dim MenuHWND As IntPtr
        Const SC_CLOSE = 61536
        Const MF_BYCOMMAND = 0
          Hwnd = 0
          MenuHWND = 0
          Hwnd = FindWindow("OpusApp", Nothing)
          If Hwnd <> 0 Then
             MenuHWND = GetSystemMenu(Hwnd, False)
             If MenuHWND <> 0 Then
              DeleteMenu(MenuHWND, SC_CLOSE, MF_BYCOMMAND)
             End If
End sub
This article has been dead for over six months. Start a new discussion instead.