Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Основная";
Текущий архив: 2002.08.22;
Скачать: [xml.tar.bz2];

Вниз

Как в коде узнать handle моего же консольного окна?   Найти похожие ветки 

 
MAYOR   (2002-08-09 17:13) [0]

Привет!

см. тема


 
Ученик   (2002-08-09 17:23) [1]

FindWindow


 
MAYOR   (2002-08-09 17:59) [2]

Привет!

А элегантнее решение есть?


 
Ученик   (2002-08-09 18:15) [3]

Это рекомендует Microsoft

HOWTO: Obtain a Console Window Handle (HWND)
ID: Q124103

The information in this article applies to:

Microsoft Win32 Software Development Kit (SDK)


SUMMARY
It may be useful to manipulate a window associated with a console application. The Win32 API provides no direct method for obtaining the window handle associated with a console application. However, you can obtain the window handle by calling FindWindow(). This function retrieves a window handle based on a class name or window name.

Call GetConsoleTitle() to determine the current console title. Then supply the current console title to FindWindow().



MORE INFORMATION
Because multiple windows may have the same title, you should change the current console window title to a unique title. This will help prevent the wrong window handle from being returned. Use SetConsoleTitle() to change the current console window title. Here is the process:

1. Call GetConsoleTitle() to save the current console window title.

2. Call SetConsoleTitle() to change the console title to a unique title.

3. Call Sleep(40) to ensure the window title was updated.

4. Call FindWindow(NULL, uniquetitle), to obtain the HWND

this call returns the HWND -- or NULL if the operation failed.


5. Call SetConsoleTitle() with the value retrieved from step 1, to
restore the original window title.


You should test the resulting HWND. For example, you can test to see if the returned HWND corresponds with the current process by calling GetWindowText() on the HWND and comparing the result with GetConsoleTitle().
The resulting HWND is not guaranteed to be suitable for all window handle operations.



Sample Code
The following function retrieves the current console application window handle (HWND). If the function succeeds, the return value is the handle of the console window. If the function fails, the return value is NULL. Some error checking is omitted, for brevity.


HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
HWND hwndFound; // This is what is returned to the caller.
char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
// WindowTitle.
char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
// WindowTitle.

// Fetch current window title.

GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);

// Format a "unique" NewWindowTitle.

wsprintf(pszNewWindowTitle,"%d/%d",
GetTickCount(),
GetCurrentProcessId());

// Change current window title.

SetConsoleTitle(pszNewWindowTitle);

// Ensure window title has been updated.

Sleep(40);

// Look for NewWindowTitle.

hwndFound=FindWindow(NULL, pszNewWindowTitle);

// Restore original window title.

SetConsoleTitle(pszOldWindowTitle);

return(hwndFound);
}


Additional query words:
Keywords : kbcode kbNTOS kbGrpUser kbWinOS95 kbWinOS98 kbWndw
Issue type : kbhowto



Last Reviewed: December 26, 1998
© 1999 Microsoft Corporation. All rights reserved. Terms of Use.




--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.



Страницы: 1 вся ветка

Форум: "Основная";
Текущий архив: 2002.08.22;
Скачать: [xml.tar.bz2];

Наверх




Память: 0.46 MB
Время: 0.006 c
6-71296
Vygantas
2002-06-08 16:05
2002.08.22
kak po imeni kompa uznat IP ??


7-71405
Avenger
2002-06-07 23:39
2002.08.22
XP-темы


1-71101
Kattzyn
2002-08-12 17:18
2002.08.22
Динамическое формирование GIF


1-71240
V.Turecky
2002-08-11 18:29
2002.08.22
Проверка существования строки в INI файле...


3-70996
Ruslan_55
2002-07-31 13:58
2002.08.22
Помогите





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский