Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable or hide minimize/maximize buttons in other application from c#?

Tags:

c#

I open an Excel (2003) application from c#. I want that user will not be able to change it's size (it's opened maximized), therefore that the system menu and minimize/maximize buttons would be disabled or even hidden. Thanks for any help!

like image 962
Leon Avatar asked Dec 10 '22 12:12

Leon


2 Answers

Here's 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 Mošmondor's answer you need to find the Excel Windows handle, then just call the above code in this way:

Utilities.HideMinimizeAndMaximizeButtons(windowHandle);

N.B.
Maybe, depending on how you start the Excel process, you could already have the process or the window handle, so just use it without calling Process.GetProcessesByName(...) / Process.GetProcesses()

EDIT:

If you start your Excel application with:

ApplicationClass _excel = new ApplicationClass();

Just use the following code:

IntPtr windowHandle = new IntPtr(_excel.Hwnd);
Utilities.HideMinimizeAndMaximizeButtons(windowHandle);
like image 174
digEmAll Avatar answered Feb 08 '23 23:02

digEmAll


  • find the process with the Excel in it
  • wind the main window for it
  • look up what flags should be set for the window (look SetWindowStyle and SetWindowStyleEx, maybe SetClass long)
  • use PostMessage from your process to set the appropriate flag values

For testing, first create the app that will be used as a Excel mock just to see that you are doing it right from the 'other' app point of view. After everything works, switch to real Excel.

like image 31
Daniel Mošmondor Avatar answered Feb 08 '23 22:02

Daniel Mošmondor