Removing and restoring window borders

I want to remove the window borders of another process in C #; I used RemoveMenu to remove borders. It almost works, but I have 2 problems left:

  • I need to remove borders twice, the first time the menu bar exists.
  • I can not restore the menu

This is what I already wrote:

public void RemoveBorders(IntPtr WindowHandle, bool Remove)
    {
        IntPtr MenuHandle = GetMenu(WindowHandle);

        if (Remove)
        {
            int count = GetMenuItemCount(MenuHandle);
            for (int i = 0; i < count; i++)
                RemoveMenu(MenuHandle, 0, (0x40 | 0x10));
        }
        else
        {
            SetMenu(WindowHandle,MenuHandle);
        }

        int WindowStyle = GetWindowLong(WindowHandle, -16);

        //Redraw
        DrawMenuBar(WindowHandle);
        SetWindowLong(WindowHandle, -16, (WindowStyle & ~0x00080000));
        SetWindowLong(WindowHandle, -16, (WindowStyle & ~0x00800000 | 0x00400000));
    }

Can someone show me what I did wrong? I already tried to save the MenuHandle and restore it later, but this does not work.

+3
source share
3 answers
  • I can not restore the menu

This is because your MenuHandle is local .

When the first call to your RemoveBorders method completes, the Garbage Collector removes the MenuHandle and free memory.

, RemoveBorders, MenuHandle - .

:

MenuHandle , , .

- MenuHandle global RemoveBorders.

, , , , . , .

MenuHandle:

private IntPtr MenuHandle;
//or
IntPtr MenuHandle; //Defined outside of RemoveBorders, which is defined below this line, to show that MenuHandle is not local variable.
public void RemoveBorders(IntPtr WindowHandle, bool Remove)
//or
protected IntPtr MenuHandle;
//or
public IntPtr MenuHandle
//or
private static IntPtr MenuHandle
//or
static IntPtr MenuHandle
//etc...

:

IntPtr MenuHandle = GetMenu(WindowHandle);

:

if (Remove)

GetMenuItemCount.

, , IntPtr, , MenuHandle - MenuHandle , RemoveBorders. IntelliSense , undefined.

MenuHandle , this. IntPtr MenuHandle ( , IntPtr this.), , MenuHandle - , , RemoveBorders .

, MenuHandle IntPtr.Zero . RemoveBorders , MenuHandle GetMenu if (Remove).

RemoveBorders , MenuHandle , .

, RemoveBorders , , if (Remove) else, remove = false SetMenu, RemoveBorders. , , , .

, , . , . . , , .

+1

. . . .

,



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

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

, .

0

:

, , .

no , , , .

, RemoveBorders, , , ( Ctrl + A, ), ( = > "" , Ctrl + C ), ( = > "" Ctrl + V ) . , , , RemoveBorder, . , RemoveBorders:

public IntPtr RemoveBorders(IntPtr WindowHandle, IntPtr MenuHandle)
{
    if (MenuHandle == IntPtr.Zero)
    {
        MenuHandle = GetMenu(WindowHandle);
        int count = GetMenuItemCount(MenuHandle);
        for (int i = 0; i < count; i++)
            RemoveMenu(MenuHandle, 0, (0x40 | 0x10));
    }
    else
    {
        SetMenu(WindowHandle,MenuHandle);
    }

    int WindowStyle = GetWindowLong(WindowHandle, -16);

    //Redraw
    DrawMenuBar(WindowHandle);
    SetWindowLong(WindowHandle, -16, (WindowStyle & ~0x00080000));
    SetWindowLong(WindowHandle, -16, (WindowStyle & ~0x00800000 | 0x00400000));
    return MenuHandle;
}

, RemoveBorders:

, void IntPtr,

return MenuHandle;

SetWindowLong. , RemoveBorders ( IntPtr #), , , . , , RemoveBorders, , . RemoveBorders (bool remove) IntPtr MenuHandle, . IntPtr MenuHandle , , MenuHandle , . IntPtr.Zero( NULL ++), . - remove , if (remove)

if (MenuHandle == IntPtr.Zero)

, , . if. , MenuHandle NULL (.. IntPtr.Zero), else . , , IntPtr , GetMenu if, GetMenuItemCount, , , . , "" , , GetMenu, , NULL (IntPtr.Zero #), else IntPtr.Zero ( NULL ++). RemoveBorders .

RemoveBorders , . , . : IntPtr, , RemoveBorders, , , , . RemoveBorders, , , , RemoveBorders. , !

0

All Articles