I did some searching on the subject, and all I could find was how to hide the window of a process that YOU are starting.
Is there anyway to hide the window of a process that is already started?
Printable View
I did some searching on the subject, and all I could find was how to hide the window of a process that YOU are starting.
Is there anyway to hide the window of a process that is already started?
I don't know if there is a better way in .Net, but you can use APIs, FindWindow to find the window you want to minimize, and SetWindowPlacement to minimize it (uses WINDOWPLACEMENT type/enum with SW_SHOWNORMAL, SW_SHOWMAXIMIZED, and SW_SHOWMINIMIZED values).
Use GetWindowPlacement to check if it's not already minimized.
Sorry. I wasn't clear. When I said "hide" I meant make the window invisible and unable to receive focus. I also don't want it to show up in the taskbar.
The reason is because I have a program that is converting songs and it uses Windows Media Player to "listen" to the song digital (at 4x speed) and every couple of minutes WMP automatically focuses itself because the song is switching and it gets really annoying.
If someone can think of a different way to solve that problem, I'd love to hear it, but the only thing I can think of is to basically make the window non-existant.
Sorry, just realised that you want to hide the window, my mistake.
You could look into ShowWindow API and use SW_HIDE constant value on the handle of the WMP. This should work.
Do you just want to hide a window?? Because doing so won't destroy it from the Processes list. You would then need CloseHandle API to close the running WMP process.
No. I don't want to destroy it. I just want to make it so it can't receive focus. Like I said, every couple of minutes it automatically gives itself focus and if I'm typing or doing ANYTHING when that happens, I have to refocus on the window I want.
I'll try that API tomorrow when I work on it some more.
VB Code:
Const SW_SHOWNORMAL = 1 Const SW_HIDE = 0 Const SW_RESTORE = 9 Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer Public Declare Function GetFocus Lib "user32" () As Integer Public Declare Function SetFocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Integer) As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ShowWindow(FindWindow(vbNullString, "AppCaptionStealingFocus"), SW_HIDE) End Sub 'You can use a timer, to check "getfocus", and then setfocus back on your window. The get/setforeground API may be helpful too, where you can update the current foreground window, and focus in a variable. If GetFocus = FindWindow(vbNullString, "AppCaptionStealingFocus") Then SetFocus(YourWindowHandle) End If
The part about hiding the window should work (I haven't had time to test it yet), but the problem with the GetFocus and SetFocus is that I'm not trying to set focus back to my program, I just need for the other window to never get focus. If it's hidden, I'm assuming it won't get focus, but I still need to test that.
The reason I don't need to set focus back to my program is because my program is running behind the scences as well. It's just when I'm surfing the internet or typing a message or something, and WMP gets focus, I have to refocus on firefox to continue typing or whatever else I was doing.
Yeah, that's why I mentioned the timer, so when wmp steals the focus, you have the handle of the last window with focus, whatever it is. So you can reset it within the closest second.
You may try EnableWindow API, to avoid focus .
This might work too:
Not sure though.VB Code:
Public Declare Function EnableWindow Lib "user32" (ByVal hwnd As Integer, ByVal fEnable As Integer) As Integer EnableWindow(FindWindow(vbnullstring, "Windows Media Player"), False)
You'll need a workaround, if the user has the full screen option on, in wmp.
EnableWindow didn't have any effect on the window at all.
The timer method doens't work because I need it to NEVER get focus. With your timer idea, it gets focus for a second and then switches back to what I was looking at before.
Okay, so it is very important, that it never recieve focus ever.
Even for a tenth of a second?
Then, I think you'll have to intercept the windows message, and block it.
This may make the wmp, malfuction, if it really needs focus for that moment.
I'll see what I can dig up, since I too inter-operate with wmp.
Anyone else got any ideas?
[EDIT]:
What happens if the media player is in a minimized state?
I thought that it couldn't get focus then. I'll try a few things.
You MIGHT be right about it being minimized and not getting focus. I thought I had tested that, but maybe not.
None of it's processing is going to be messed up if it doesn't get focus. I'm sure of that.
Yeah. It definitely still steals focus if it's minimized. It's probably because the application that is digitally "listening" to WMP is probably forcing that to happen.
Any other ideas?
I've skimmed through a few more APIs and haven't found anything. Anyone else have some thoughts?
I'm stumped on this one too!
I've tried a few sub classing techniques, but it doesn't respond.
I tried PeekMessage with no results, but maybe someone in the API forum can help better with that.
Sorry, if I find anything I'll repost.
This seems to be the way to go for you:
SubClassingHooks
PeekMessage
Also, the API GUIDE, has a decent example of PeekMessage.