- I can't restore the menu's
This is because that your MenuHandle is local variable.
When the first call to your method RemoveBorders ends, the Garbage Collector deletes MenuHandle, and free memory.
The second time you call RemoveBorders, MenuHandle recreated as new local variable, and reassigned to the current state of the menu of your window - a menu with no menu items.
As a result:
MenuHandle doesn't save the previous state of your window's menu, and this explains why you cannot restore the window's menu.
My advice to you is to make MenuHandle global variable, and define it out of RemoveBorders method definition.
You can define it as a private, protected or public field and also define another property for it, but this is optional, and not necessary. You also can define it as static, if this attribute is better for you.
Here are some examples for MenuHandle's definition:
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...
You'll have to move the line:
IntPtr MenuHandle = GetMenu(WindowHandle);
Inside:
if (Remove)
and before the call to GetMenuItemCount function.
You'll also have to modify that line, and at least remove the IntPtr, to declare that MenuHandle is not local variable, and to refer to the MenuHandle field, which is defined out of RemoveBorders method.
IntelliSense still will recognize it as a field, and won't alert you the undefined error.
If MenuHandle is not static, then you also can add the this
. keyword after removing the IntPtr before MenuHandle (In other words, you can replace IntPtr
with this.
), to memorize yourself that MenuHandle is not local variable anymore, so the Garbage Collector won't delete it whenever RemoveBorders finishes the job.
When you will start your program, MenuHandle will be assigned to IntPtr.Zero
as default value. When you call RemoveBorders for the first time, MenuHandle's value will be set to the returned value of the GetMenu function in the if (Remove)
.
When RemoveBorders finishes for the first time, MenuHandle is not deleted, and saves the previous state of the window's menu, before all it's items removed.
So when you call RemoveBorders for the second time in order to restore the menu, the executor will reach if (Remove)
code and jump immediately to the else
code, because remove = false, and in there you call SetMenu function, when you give it the previous state of the window's menu since the first call to RemoveBorders.
This way you'll be able finally to restore window's menu.
I still don't realize why you need to remove the borders twice, the first time the menu bar still exist.
I want to help you to solve this problem too, but have no idea.
Your code is correct in this case. Sorry, but I hope that other people can solve this problem for you and give you the solution for this too.