我从c#打开一个Excel(2003)应用程序.我希望该用户无法更改其大小(它已打开最大化),因此系统菜单和最小化/最大化按钮将被禁用甚至隐藏.谢谢你的帮助!
dig*_*All 12
这是代码:
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));
}
}
Run Code Online (Sandbox Code Playgroud)
正如DanielMošmondor的回答所述,您需要找到Excel Windows句柄,然后以这种方式调用上面的代码:
Utilities.HideMinimizeAndMaximizeButtons(windowHandle);
Run Code Online (Sandbox Code Playgroud)
NB
也许,根据你如何启动Excel进程,你可能已经拥有进程或窗口句柄,所以只需使用它而不需要调用Process.GetProcessesByName(...) / Process.GetProcesses()
编辑:
如果您启动Excel应用程序:
ApplicationClass _excel = new ApplicationClass();
Run Code Online (Sandbox Code Playgroud)
只需使用以下代码:
IntPtr windowHandle = new IntPtr(_excel.Hwnd);
Utilities.HideMinimizeAndMaximizeButtons(windowHandle);
Run Code Online (Sandbox Code Playgroud)