Active window holding problem
-
Hi I wrote the code for minimising the application that are active while I launch my application. But one error is coming "cannot convert from 'HWND' to 'CWnd *'" CString szTitle; CWnd *pTopWnd; UINT cmd=GW_HWNDFIRST; while (pTopWnd=GetWindow(NULL,cmd)) { pTopWnd->GetWindowText(szTitle); if(szTitle.Find("MyApp.EXE")>=0) pTopWnd->ShowWindow(SW_HIDE); cmd=GW_HWNDNEXT; } Plz tell the solution
-
Hi I wrote the code for minimising the application that are active while I launch my application. But one error is coming "cannot convert from 'HWND' to 'CWnd *'" CString szTitle; CWnd *pTopWnd; UINT cmd=GW_HWNDFIRST; while (pTopWnd=GetWindow(NULL,cmd)) { pTopWnd->GetWindowText(szTitle); if(szTitle.Find("MyApp.EXE")>=0) pTopWnd->ShowWindow(SW_HIDE); cmd=GW_HWNDNEXT; } Plz tell the solution
-
Your GetWindow() is returning an HWND and you are trying to assign that directly to a CWnd*. Do the following instead: HWND hWnd = GetWindow(NULL,cmd); CWnd *pTopWnd = CWnd::FromHandle(hWnd);
"What's on your mind, if you will allow the overstatement?"
I modified the code as : CString szTitle; HWND hWnd = GetWindow(NULL,GW_HWNDFIRST); CWnd *pTopWnd = CWnd::FromHandle(hWnd); while (pTopWnd!=NULL) { pTopWnd->GetWindowText(szTitle); if(szTitle.Find(_T("MyApp.EXE"))>=0) pTopWnd->ShowWindow(SW_HIDE); hWnd = GetWindow(NULL,GW_HWNDNEXT); pTopWnd = CWnd::FromHandle(hWnd); } But pTopWnd is coming NULL.Why?
-
I modified the code as : CString szTitle; HWND hWnd = GetWindow(NULL,GW_HWNDFIRST); CWnd *pTopWnd = CWnd::FromHandle(hWnd); while (pTopWnd!=NULL) { pTopWnd->GetWindowText(szTitle); if(szTitle.Find(_T("MyApp.EXE"))>=0) pTopWnd->ShowWindow(SW_HIDE); hWnd = GetWindow(NULL,GW_HWNDNEXT); pTopWnd = CWnd::FromHandle(hWnd); } But pTopWnd is coming NULL.Why?
You are passing NULL as the first parameter to GetWindow, you have to provide a valid window handle to GetWindow then only it gets the next window or any window thats related to the window that you provide. Use GetForegroundWindow function to get the handle to the current active window, then you can use GetWindow to get the next windows. Remember you always have to pass a valid HWND to GetWindow.
"What's on your mind, if you will allow the overstatement?"
-
I modified the code as : CString szTitle; HWND hWnd = GetWindow(NULL,GW_HWNDFIRST); CWnd *pTopWnd = CWnd::FromHandle(hWnd); while (pTopWnd!=NULL) { pTopWnd->GetWindowText(szTitle); if(szTitle.Find(_T("MyApp.EXE"))>=0) pTopWnd->ShowWindow(SW_HIDE); hWnd = GetWindow(NULL,GW_HWNDNEXT); pTopWnd = CWnd::FromHandle(hWnd); } But pTopWnd is coming NULL.Why?
pther wrote:
But pTopWnd is coming NULL.Why?
Did you bother to check the return value of
GetWindow()
, or are you assuming it succeeded and callingFromHandle()
anyway?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne