开发者

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

开发者 https://www.devze.com 2023-01-27 11:59 出处:网络
I open an Excel (2003) application from c#. I want that user will not be ab开发者_开发技巧le to change it\'s size (it\'s opened maximized), therefore that the system menu and minimize/maximize buttons

I open an Excel (2003) application from c#. I want that user will not be ab开发者_开发技巧le 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);

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


  • 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消