Windows remove window border

Removing window frame / border properly

I’ve been working on a custom GUI framework since I just can’t deal with managed crap or native code which requires development of UIs through markup (XAML). I am trying to create a prototype of an application which uses that GUI framework, but I am having a platform-specific issue with the nature of windows within WinAPI.

The DWM doesn’t really allow customization of the non-client area which breaks immersion, the only thing it allows is extension into the client area in order to give an illusion of customization.

So, the best course of action is to reconstruct the «non-client area» within the client area (relative to WINAPI) and that required me to strip the caption, maximize, minimize buttons etc. So, I basically enumerated all the things I want out and OR-ed them together and flipped all the bits in order to deactivate them.

Once these style go away, I cannot use normal shutdown procedures (Alt+F4, or right clicking in the taskbar and going «Close») because they don’t work. I had to intercept VK_ESCAPE and PostQuitMessage(0) manually just so I could exit without being forced to kill the process.

Why is this so? And how can I fix this?

1 Answer 1

The short answer

And no more funky behavior. The application responds correctly. Enjoy the cake.

The long answer

Ah, as with everything on MSDN lately, the cake is a lie. Window styles are not really just visual. They also specify what inherent window functionalities are available to the application’s window(s). Now, there is a fair amount of trickery here to be observed.

First of all, the MSDN isn’t really forthcoming and useful with its window style definition table. The default behavior for windows is the classic caption, close, border package which is identified as the WS_OVERLAPPEDWINDOW which occupies the simplest expression, 0 (a 32-bit value, all bits down, 0x00000000), so someone wishing to rush through things could just set 0 for styles in the CreateWindow* function and it would yield a classic window.

What you want is a bare-bone, dirty and empty window. And Microsoft’s got exactly the thing you’re looking for — WS_POPUP which sets the highest bit to 1 and everything else is 0. This will drop all the fancy resizing automata, window captioning and the cute minimize, maximize and close buttons.

Which means you’re going to have to reimplement everything. But that’s what you’re going for, right?

Just flipping all the bits isn’t enough, you will drop the wanted options, but also activate the rest of the options resulting in the application acting funny, what you’re experiencing right now. Therefore, you either AND it with something else or use something readily defined by Microsoft — WS_POPUP .

And again. Enjoy the cake and happy coding.

Removing window border?

I have a window with a solid border around it. How can I remove the border (all of the non-client area) by using SetWindowLong and GetWindowLong?

4 Answers 4

WS_CAPTION is defined as (WS_BORDER | WS_DLGFRAME). You can get away with removing just these two styles, since the minimize maximize and sytem menu will disappear when the caption disappears, but it’s best to remove them as well.

Читайте также:  Почему не работает плеер windows

It’s also best to remove the extended border styles.

And finally, to get your window to redraw with the changed styles, you can use SetWindowPos.

The following Delphi codes does it:

Of course, these API calls look the same in all languages.

This line of code below removes the border of any given window, and remains only its client:

You can use WS_POPUPWINDOW instead in the third parameter of SetWindowLong function. It also removes the borders of the given window and works too, but the difference is that it also draws outlined black rectangle all over the remaining client of the window. The thickness of that outlined rectangle is 1 pixel. WS_POPUP doesn’t draw that rectangle, actually it doesn’t draw anything, just only remove window’s borders.

If you are about to return back the borders of the window, before you use that line of code I posted above, call first that line of code below:

but of course that this function retuns the styles of the window, so create new variable that will keep these styles, i.e. set this variable to the return value of that function.

Then you use SetWindowLong as I showen above to remove its borders, and when you want later to restore its borders back, just recall again SetWindowLong , the first two parameters are same (hWnd and GWL_STYLE), but the third parameter is the styles of the window that returned from GetWindowLong . If you don’t want to call GetWindowLong , but still return the borders of the window, then you can use SetWindowLong with the same first two parameters, and in the third parameter, you can use one of the following: WS_OVERLAPPED or/and WS_OVERLAPPEDWINDOW or/and WS_SIZEFRAME .

NOTE: If you try my answer, but it doesn’t work for you, this can be, because that the both functions SetWindowLong and GetWindowLong have been superseded and doesn’t work for you, and that because they are compatible with only 32-bit version of Windows. Probably you are using 64-bit version of Windows, then use SetWindowLongPtr and GetWindowLongPtr instead, which are compatible with both 32-bit and 64-bit versions of Windows. MSDN informs that about these functions in the Note section. Just search for them in that site. Here are the links to them:

Removing and restoring Window borders

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

  • I need to remove the borders twice, the first time the menu bar still exists.
  • I can’t restore the menu’s

This is what I already wrote:

Can someone show me what I did wrong? I already tried to save the MenuHandle and restore it later, but that doesn’t work.

3 Answers 3

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.

Читайте также:  Ошибка 0xc1900101 0x40017 при обновлении windows

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:

You’ll have to move the line:

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.

Try this. This works for me. In this example the border and menu removing is done inside the app it self. But with minor adjustments you can make it work for an external window.

These are some constants I declare in my code

For the window border

For the menu you can do this.

Also notice I use GetWindowLongPtr, SetWindowLongPtr and SetWindowPosPtr with IntPtr as arguments instead of GetWindowLong, SetWindowLong and SetWindowPos int/uint. This is because of x86/x64 compatibility.

Here is how I do the import GetWindowLongPtr

Hope this helps.

I solved your problem about the following point:

About your problem in the other point

I need to remove the borders twice, the first time the menu bar still exists.

I have no solution for that sorry, but I hope that other people will help with that.

Читайте также:  The doors the woman in the windows

Delete all code that defines your RemoveBorders method, which you posted in your question, and then select all the following code, that I posted below (use Ctrl + A if it works), copy it (right mouse button click => select «Copy» or just press Ctrl + C quicker), and then paste it (right mouse button click => select «Paste» or just press Ctrl + V quicker) to your code. Make sure that the position of the cursor in the code editor stands in the right place where your old code that defined your RemoveBorder method was, before you paste the new code. My new code that redefines RemoveBorders is:

Changes that were made from your old version to my new version of RemoveBorders method:

First of all, the return value of the method changed from void to IntPtr , so the code line

question

how can I remove yellow border line when screen capture?

I am building screen recording applications using Windows.Graphics.Capture namespace. Especially I am capturing monitors with the handle of HMONITOR.

When I run the applications, I see yellow border around the screen. I am wondering if there is any way to remove this. Looking at user side, it would help for them recognize app is recording, however, it’s also annoying them.

Does my reply make sense? Please feel free to contact us if you have other questions.

2 Answers

Welcome to Microsoft Q&A!

> how can I remove yellow border line when screen capture?

No, this is by design. As you mentioned, you’ve already known that this border will let the customer know the app is recording. On the other hand, it will also let the customer know which part of their device is being recorded. The users should be aware of what and when and where the current app is recording.

I’m in IT and I find this feature ridiculous, even for security purposes. This feature does not indicate that an app is recording, (as your statement would indicate), only that an app MAY be recording — it shows up immediately once a window has been selected for recording purposes. This feature should be user controlled, even if you have to add password controls to it for security. A user shouldn’t have to be subjected to this while recording their own screen anyway. One window with a yellow border is bad enough. Try adding a bunch of windows with yellow borders. Then where does that leave you? Especially since the borders aren’t actually indicating that the windows are being recorded, only selected. So it’s not even a good tracking system. It only serves its purpose when a hacker shows up, but not for normal every day use.

In short, good idea, bad implementation. This needs to be reworked.

Between the audio problems the 2004 update caused, the constant need to «reset» the photos app in order to use it, and now this hideous yellow window border, I am not a happy Windows user right now!

Оцените статью