简体   繁体   中英

how to disable or hide minimize/maximize buttons in other application from 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!

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

NB
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);
  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM