C# Window Focus

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

C# Window Focus

Post by N64vSNES »

Okay I made kind of a place holder editor in C# a while back due to people being busy/lazy.

Now here is a video of it:


You can see the are 2 windows. One for the map and one for tools, tile selecting etc.
My issue is that even if the window with the map has no focus, when I switch tiles or whatever on the tool window the map window still thinks that you're clicking on the map window and lays tiles.

If you watch the beginning of the video you'll see this happen (in case I didn't explain it too well).

Anyone have any ideas on how I could work around this?

Thanks.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: C# Window Focus

Post by Ginto8 »

It looks like you're using the same WndProc for both windows, which is causing issues. However, if that's not it, you might also want to put some sort of check for whether or not the window has focus in each one's WndProc.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

Re: C# Window Focus

Post by JesseGuarascia »

Well as long as you're doing what the title says, you should be fine. With C# forms, I believe there is a way to tell if the window has focus. So in your procedure for clicking tiles on the map, you could have a nice little:

Code: Select all

if (mapWindow.hasFocus()){
    // Draw tiles
}
That should fix your problem.
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
krilik
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Sat Apr 18, 2009 11:24 pm
Favorite Gaming Platforms: SNES,PS1

Re: C# Window Focus

Post by krilik »

It depends on how you are integrating forms into your XNA project. From the looks of it you have an XNA game running with windows forms on top of it and I don't think XNA allows you to directly manipulate all the functions/attributes of window/forms that the Game class uses, so you can't do it the way Jesse suggested.

You'll have to make a reference from your Game1 object into a form instance to manipulate it.

Code: Select all

//in your draw function
System.Windows.Forms.Form myForm = (System.Windows.Forms.Form)System.Windows.Forms.Form.FromHandle(this.Window.Handle);

....

//a check before you draw a tile
if (myForm.Focused == true)
{
     //draw your tiles
}
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

Re: C# Window Focus

Post by JesseGuarascia »

krilik wrote:It depends on how you are integrating forms into your XNA project. From the looks of it you have an XNA game running with windows forms on top of it and I don't think XNA allows you to directly manipulate all the functions/attributes of window/forms that the Game class uses, so you can't do it the way Jesse suggested.

You'll have to make a reference from your Game1 object into a form instance to manipulate it.
I have very little experience with XNA, so I wasn't aware that form integration doesn't work the same way normal forms work. Never-the-less, I find having a literal copy of the form to be sort of pointless. A more proper approach (in my opinion anyways), would be to set up a global boolean of sorts, then just use that in your tile placement function. So when the form for selecting tiles has focus (using my code I posted earlier), you could set a boolean to true. Using that same boolean, the XNA portion for tile placement can decide to paint or not.

An example would be something like:

TileSelectForm.cs:

Code: Select all

if (this.hasFocus()){
    // Set the global boolean to true
    selectFormHasFocus = true;
}
MapPainter.cs:

Code: Select all

if (!selectFormHasFocus){
    // Paint tiles
}
Although I'm sure krilik's would work just as fine, I don't like the idea of copying an entire form, just to tell if it has focus or not.
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: C# Window Focus

Post by N64vSNES »

Once again.
I don't know where I'd be without you guys :bow:
krilik
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Sat Apr 18, 2009 11:24 pm
Favorite Gaming Platforms: SNES,PS1

Re: C# Window Focus

Post by krilik »

JesseGuarascia wrote:I have very little experience with XNA, so I wasn't aware that form integration doesn't work the same way normal forms work. Never-the-less, I find having a literal copy of the form to be sort of pointless. A more proper approach (in my opinion anyways), would be to set up a global boolean of sorts, then just use that in your tile placement function. So when the form for selecting tiles has focus (using my code I posted earlier), you could set a boolean to true. Using that same boolean, the XNA portion for tile placement can decide to paint or not.


Although I'm sure krilik's would work just as fine, I don't like the idea of copying an entire form, just to tell if it has focus or not.
Yea as far as I know XNA won't allow you to modify the window the game runs in directly. Probably to prevent people from breaking the Xbox 360 version of their game. Its a bitch like that. They have other functions that will allow you to change the resolution and such to the game window, but its done through another object called a GraphicsDeviceManager, so you're very limited in what you can do.

So I think the only real way it can be accomplished is by passing a reference to a wrapper and manipulating it through that. Kind of dumb, and really inefficient, but C#'s garbage collection cleans up the object once the method is finished.

A better approach would be to design your tools in a Windows Form project and integrate an XNA control into a Windows Form and do it the way you suggest.
Post Reply