How to disable or hide the decrease / increase buttons in another application from C #?

I am opening an Excel (2003) application from C #. I want this user to not be able to resize it (it was opened as much as possible), so the system menu and the decrease / maximize buttons will be disabled or even hidden. Thanks for any help!

+4
source share
2 answers

Here is the code:

internal static class Utilities { [DllImport("user32.dll")] internal extern static int SetWindowLong(IntPtr hwnd, int index, int value); [DllImport("user32.dll")] internal extern static int GetWindowLong(IntPtr hwnd, int index); internal static void HideMinimizeAndMaximizeButtons(IntPtr hwnd) { const int GWL_STYLE = -16; const long WS_MINIMIZEBOX = 0x00020000L; const long WS_MAXIMIZEBOX = 0x00010000L; long value = GetWindowLong(hwnd, GWL_STYLE); SetWindowLong(hwnd, GWL_STYLE, (int)(value & ~WS_MINIMIZEBOX & ~WS_MAXIMIZEBOX)); } } 

As stated in Daniel Moschmondor's answer , you need to find the Excel Windows descriptor, and then just call the above code as follows:

 Utilities.HideMinimizeAndMaximizeButtons(windowHandle); 

NB
Perhaps depending on how you start the Excel process, you may already have a process or window handle, so just use it without calling Process.GetProcessesByName(...) / Process.GetProcesses()

EDIT:

If you start Excel using

 ApplicationClass _excel = new ApplicationClass(); 

Just use the following code:

 IntPtr windowHandle = new IntPtr(_excel.Hwnd); Utilities.HideMinimizeAndMaximizeButtons(windowHandle); 
+12
source
  • find a process with excel in it
  • Direct the main window for him
  • See which flags should be set for the window (see SetWindowStyle and SetWindowStyleEx, possibly SetClass long)
  • use PostMessage from your process to set the appropriate flag values.

For testing, first create an application that will be used as an Excel layout to make sure that you do it directly from the point of view of a "different" application. After everything works, switch to real Excel.

+1
source

All Articles