Sign in to follow this  
thomasvantroyen

mouse cursor on RenderForm won't hide after alt-tab

Recommended Posts

Hey,

On Win7 DirectX11 using SlimDX, i have an empty project that's fullscreen with some mousecursor-hiding code like found on this forum too. The mouse is nicely hidden initially.

When alt-tabing out and activating the app again, the fullscreen returns but with a visible mousecursor.

Whichever code i try, it won't work. I even have Cursor.Hide() in my main loop :)

Here is the code, it's messy because it's test code :)

Anyone see mistakes? When i translate to DirectX 10, i get the same problem.

[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]var [/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]form = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]new[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]RenderForm[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]();[/size][/font][/size][/font]

[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Device[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] device;[/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]SwapChain[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] swapChain;[/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Device[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].CreateWithSwapChain([/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]DriverType[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].Hardware, [/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]DeviceCreationFlags[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].Debug, swapChainDescription, [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]out[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] device, [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]out[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] swapChain);[/size][/font][/size][/font]

[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]using[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] ([/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]var[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] factory = swapChain.GetParent<[/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Factory[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]>())[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]factory.SetWindowAssociation(form.Handle, [/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]WindowAssociationFlags[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].IgnoreAltEnter);[/size][/font][/size][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"]form.Activated += (o, e) => { [/size][/font][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]swapChain.IsFullScreen = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]true[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]; [color="#2b91af"]Cursor[/color][font="Consolas"][size="2"][font="Consolas"][size="2"].Hide(); [/size][/font][/size][/font][/size][/font][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]};[/size][/font][/size][/font]

[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]var[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] backBuffer = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Resource[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].FromSwapChain<[/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Texture2D[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]>(swapChain, 0);[/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]var[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] renderView = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]new[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]RenderTargetView[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"](device, backBuffer);[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]device.ImmediateContext.OutputMerger.SetTargets(renderView);[/size][/font][/size][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"]device.ImmediateContext.Rasterizer.SetViewports([/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]new[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Viewport[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"](0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f));[/size][/font][/size][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"]form.MouseEnter += (s, e) => [/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Cursor[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].Hide();[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]form.MouseLeave += (s, e) => [/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Cursor[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].Show();[/size][/font][/size][/font]

[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]MessagePump[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].Run(form, () => [/size][/font][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]{ [/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]device.ImmediateContext.ClearRenderTargetView(renderView, [/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Color[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].CornflowerBlue);[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"] swapChain.Present(0, [/size][/font][/size][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]PresentFlags[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].None);[/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"] Cursor[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].Hide();[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]});[/size][/font][/size][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"]swapChain.IsFullScreen = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]false[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"];[/size][/font][/size][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"]device.Dispose();[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]swapChain.Dispose();[/size][/font][/size][/font]

Share this post


Link to post
Share on other sites
I found it myself.

As i was refactoring to get a nicely structured base class to get me started, i accidently found out that this mouse cursor problem only exists in a specific situation:

When you AltTab out of a fullscreen app, it actually becomes windowed. When reactivating the app, it remains windowed. Because I always want a fullscreen, I added the following eventhandler:

[font="Consolas"][size="2"][font="Consolas"][size="2"]WindowsForm.AppActivated += (sender, args) => { SwapChain.IsFullScreen = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]true[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]; }; [/size][/font][/size][/font]


Because of common sense i also set IsWindowed = false when calling Device.CreateWithSwapChain().

This combination caused the mouse cursor to stay visible after alt-tab and reactivating the app.

But when you change one thing: call Device.CreateWithSwapChain() with [b]IsWindowed = true[/b]; this Cursor problem doesn't happen anymore. You start DirectX windowed but my AppActivated eventhandler above is triggered at startup too, so I get a fullscreen immediately at startup.


So, now I have a fullscreen app, that never becomes windowed and never shows the windows mouse cursor, finally :)


If any expert can explain this difference, I'm happy to listen, but for now I'l just accept it :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this