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")

RemoveMenu(MenuHWND, -1, MF_REMOVE Or MF_BYPOSITION)

End If

End If

any Solution available?

Recommended Answers

All 2 Replies

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.

{SOLVED}

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
         Try
          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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.