Thursday, July 9, 2009

How can I find the hWnd of my C++ console application?

I am writing a C++ console application and I need to get my console window to focus, but I can not find a way other than to use the WinAPI FindWindow function. This is not an ideal solution, hovever, because the path to the program is displayed in the title bar and may change from computer to computer. If there is another way to find the hWnd or even a way to set the window's title/class, please let me know.





Thanks, Baggins.

How can I find the hWnd of my C++ console application?
Try:





HWND GetConsoleWindow(void);





Requirements





Client: Included in Windows XP and Windows 2000 Professional.


Server: Included in Windows Server 2003 and Windows 2000 Server.


Header: Declared in Wincon.h; include Windows.h.


Library: Use Kernel32.lib.











If all you're tryingto do is set the windows title, try:





DWORD GetConsoleTitle(


LPTSTR lpConsoleTitle,


DWORD nSize


);








BOOL SetConsoleTitle(


LPCTSTR lpConsoleTitle


);











Examples:





To get window handle:





HWND consoleWnd = GetConsoleWindow();





To get the current console title:





TCHAR currentTitle[512];


GetConsoleTitle(currentTitle, sizeof(currentTitle) / sizeof(TCHAR));





To set the console title:





SetConsoleTitle(_T("Testing window title"));


No comments:

Post a Comment