?
Solved

program focus

Posted on 2007-11-27
30
Medium Priority
?
333 Views
Last Modified: 2012-06-21
#include <iostream>
#include <windows.h>


using namespace std;




int main ()
{
      HWND hwnd = FindWindow(NULL,"notepad");
      
      MoveWindow(hwnd, 100, 100, 400, 200, TRUE);
      
      
      
      
      
      
      
      return 0;
}






this is my small test program, it pops a console window briefly. is it possible to make it NOT pop the console program, in other words, have it not taking focus at all?



also, given hwnd, how do I focus a window?
0
Comment
Question by:Troudeloup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 15
  • 13
  • 2
30 Comments
 
LVL 7

Expert Comment

by:lucky_james
ID: 20357500
if you want not it to have the focus, then why dont you make it invisible?

use ShowWindow(SW_HIDE);

Let me know if you have any doubts.
0
 

Author Comment

by:Troudeloup
ID: 20357506
no I have a handle of a window currently in the background and i make to focus it.

no i don't want it to be invisible.
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20357518
check out:
http://blogs.msdn.com/oldnewthing/archive/2004/08/02/205624.aspx

Let me know if you have any doubts.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Troudeloup
ID: 20357559
i don't want a dialogue box.
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20357623
not a problem.......use file i/o instead of normal i/o operations

check out:
http://www.mkssoftware.com/docs/man3/fgetln.3.asp

Hope it helps.
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20357628
sorry, ignore above comment.

0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20357719
to get focus :
SwitchToThisWindow
check out :
http://msdn2.microsoft.com/en-us/library/ms633553.aspx

and to switch the window to the background or even bring it to front:
SetWindowPos
check out:
http://msdn2.microsoft.com/en-us/library/ms633545.aspx
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20357725
Let me know if you have any doubt .......
0
 

Author Comment

by:Troudeloup
ID: 20357742
this one doesn't work














#include <iostream>
#include <windows.h>


using namespace std;




int main ()
{
      HWND hwnd = FindWindow(NULL,"notepad");
      
      // MoveWindow(hwnd, 100, 100, 400, 200, TRUE);
      
      
      ::SwitchToThisWindow( hwnd,  false );

      
      
      
      
      return 0;
}

0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20357759
>>::SwitchToThisWindow( hwnd,  false );

this is used to FOCUS on your application.
false parameter just signifies that the focus is not using the alt+tab sequence.


To keep in the background. please try SetWindowPos .......

revert back if you face any issues.
0
 

Author Comment

by:Troudeloup
ID: 20357776
you mean i should change that false to true for it to work?
0
 

Author Comment

by:Troudeloup
ID: 20357781
this is the error message

focus_test.cpp:17: error: `::SwitchToThisWindow' has not been declared
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20357794
i mean if you want your window to run behind other applications.....then dont use SwitchToThisWindow.

Use SetWindowPos.
0
 

Author Comment

by:Troudeloup
ID: 20357799
no i want to focus the window with this hwnd.


why do I have that error message?
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20357801
>>`::SwitchToThisWindow' has not been declared

#include "Windows.h"

is the solution.
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20357820
oh then use ::SwitchToThisWindow( hwnd,  true );
0
 

Author Comment

by:Troudeloup
ID: 20357843
I already have

#include <windows.h>



is this different?

#include "windows.h"
0
 

Author Comment

by:Troudeloup
ID: 20358059
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20358103

>>is this different?
no its not different.
0
 

Author Comment

by:Troudeloup
ID: 20358113

why do I have that error message?

::SwitchToThisWindow' has not been declared
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20358136
try without scope resolution operator.
SwitchToThisWindow
if your findwindow is working then SwitchToThisWindow should also work as both are defined in user32.dll and declared in windows.h
0
 

Author Comment

by:Troudeloup
ID: 20358153
#include <iostream>
#include <windows.h>

using namespace std;




int main ()
{
      HWND hwnd = FindWindow(NULL,"notepad");
      
      // MoveWindow(hwnd, 100, 100, 400, 200, TRUE);
      
      cout << hwnd << endl;
      
      switchtothisWindow( hwnd,  false );

      
      
      
      
      return 0;
}





this gets the same error message
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20358172
you are using "switchtothisWindow" and it is "SwitchToThisWindow". The cases you are using are different. :)
0
 

Author Comment

by:Troudeloup
ID: 20358187
#include <iostream>
#include <windows.h>

using namespace std;




int main ()
{
      HWND hwnd = FindWindow(NULL,"");
      
      // MoveWindow(hwnd, 100, 100, 400, 200, TRUE);
      
      cout << hwnd << endl;
      
      SwitchToThisWindow( hwnd,  false );

      
      
      
      
      return 0;
}






nope, doesn't work.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 2000 total points
ID: 20358285
If you don't want a console window you should turn your prog to a WIN32 prog.

Goto the project properties, Linker - System and change from 'Console (/subsystem:console)' to 'Windows (/subsystem:windows)'. Both for 'Debug' and 'Release' configuration.

Then, replace

      int main()  

by

      int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);

Regards, Alex
0
 

Author Comment

by:Troudeloup
ID: 20358294
how do I do a win32 prog with generic c++ code?

I use g++.


also, can you take a look of the code I posted?
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20358341
>>SwitchToThisWindow( hwnd,  false );
try using with true.
0
 

Author Comment

by:Troudeloup
ID: 20358350
ok i ll say it again:

it still pops this:

focus_test.cpp:17: error: `SwitchToThisWindow' was not declared in this scope


how many times do I have to tell you that?!
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20358392
>>how many times do I have to tell you that?!
Troudeloup, would have been gud if you had told me in ur previous post. By "It doesn't work", i thought you are into the functionality rather than compile time errors.

Anyway, thanks.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20358619
>>>> how do I do a win32 prog with generic c++ code?
win32 is a properietary platform. There is no generic code c++ for a platform specific thing.

>>>> I use g++.
Check the docs whether they support  linker option

    /subsystem:windows

If yes, it should work with g++ as well.

>>>> SwitchToThisWindow( hwnd,  false );

Did you try the SetFocus instead?

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question