尝试这个。这对我有用。在这个例子中,边框和菜单的移除是在它自己的应用程序内部完成的。但只需稍作调整,您就可以使其适用于外部窗口。
这些是我在代码中声明的一些常量
const uint WS_BORDER = 0x00800000;
const uint WS_DLGFRAME = 0x00400000;
const uint WS_THICKFRAME = 0x00040000;
const uint WS_CAPTION = WS_BORDER | WS_DLGFRAME;
const uint WS_MINIMIZE = 0x20000000;
const uint WS_MAXIMIZE = 0x01000000;
const uint WS_SYSMENU = 0x00080000;
const uint WS_VISIBLE = 0x10000000;
const int GWL_STYLE = -16;
对于窗口边框
Point originallocation = this.Location;
Size originalsize = this.Size;
public void RemoveBorder(IntPtr windowHandle, bool removeBorder)
{
uint currentstyle = (uint)GetWindowLongPtr(this.Handle, GWL_STYLE).ToInt64();
uint[] styles = new uint[] { WS_CAPTION, WS_THICKFRAME, WS_MINIMIZE, WS_MAXIMIZE, WS_SYSMENU };
foreach (uint style in styles)
{
if ((currentstyle & style) != 0)
{
if(removeBorder)
{
currentstyle &= ~style;
}
else
{
currentstyle |= style;
}
}
}
SetWindowLongPtr(windowHandle, GWL_STYLE, (IntPtr)(currentstyle));
//this resizes the window to the client area and back. Also forces the window to redraw.
if(removeBorder)
{
SetWindowPosPtr(this.Handle, (IntPtr)0, this.PointToScreen(this.ClientRectangle.Location).X, this.PointToScreen(this.ClientRectangle.Location).Y, this.ClientRectangle.Width, this.ClientRectangle.Height, 0);
}
else
{
SetWindowPosPtr(this.Handle, (IntPtr)0, originallocation.X, originallocation.Y, originalsize.Width, originalsize.Height, 0);
}
}
对于菜单,您可以这样做。
public void RemoveMenu(IntPtr menuHandle, bool removeMenu)
{
uint menustyle = (uint)GetWindowLongPtr(menuStrip1.Handle, GWL_STYLE).ToInt64();
SetWindowLongPtr(menuStrip1.Handle, GWL_STYLE, (IntPtr)(menustyle^WS_VISIBLE));
// forces the window to redraw (makes the menu visible or not)
this.Refresh();
}
另请注意,我使用带 IntPtr 作为参数的 GetWindowLongPtr、SetWindowLongPtr 和 SetWindowPosPtr,而不是 GetWindowLong、SetWindowLong 和 SetWindowPos int/uint。这是因为 x86/x64 兼容性。
这是我如何导入 GetWindowLongPtr
[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
public static extern IntPtr GetWindowLong64(IntPtr hWnd, int nIndex);
public static IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex)
{
if (IntPtr.Size == 8)
{
return GetWindowLong64(hWnd, nIndex);
}
else
{
return new IntPtr(GetWindowLong(hWnd, nIndex));
}
}
希望这可以帮助。