试试这个。这对我有效。在这个例子中,边框和菜单的移除是在应用程序内部完成的。但是通过微小的调整,您可以使其适用于外部窗口。
这些是我在代码中声明的一些常量。
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));
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();
}
另外请注意,我使用GetWindowLongPtr、SetWindowLongPtr和SetWindowPosPtr,并将IntPtr作为参数,而不是使用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));
}
}
希望这能有所帮助。
RDW_FRAMECHANGED
йҮҚж–°з»ҳеҲ¶зӘ—еҸЈд»ҘжҳҫзӨәж–°зҡ„иҫ№жЎҶгҖӮйҡҫйҒ“дҪ дёҚеә”иҜҘдҪҝз”ЁFormBorderStyle
еұһжҖ§иҖҢдёҚжҳҜзӣҙжҺҘдҝ®ж”№ж ·ејҸеҗ—пјҹ - Raymond ChenSWP_FRAMECHANGED
。但是你为什么要干扰属于另一个进程的窗口呢?这有点不礼貌。 - Raymond ChenSWP_FRAMECHANGED
在跨进程时无法工作。也许你弄错了常量。(例如,你没有说明你是编译为32位还是64位。) - Raymond ChenGetWindowLong
的参数与32位不同。由于这并不是预期完成的任务,因此没有更好的方法来完成它。 - Raymond Chen