[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
Win32 API Code Snippets
Note that window handles move in memory in time! So you cannot retrieve them once at the start of your program (thus making the std::map const).
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
Win32 API Code Snippets
(Win32 API) Code snippets
Page overview
- How to: Obtain all window handles?
- How to: obtain a window's text?
- How to: get the last error?
- Topic links
How to: Obtain all window handles?
Example to get a std::map of std::string and HWND.std::map<std::string,HWND> getWindows(const HWND& anyHandle) { std::map<std::string,HWND> myWindows; const int sizeBuffer = 100; char buffer[sizeBuffer]; HWND handle = GetWindow(anyHandle,GW_HWNDFIRST); while(handle!=0) { GetWindowText(handle,buffer,sizeBuffer); if (std::string(buffer)!="") myWindows[std::string(buffer)] = handle; handle = GetNextWindow(handle,GW_HWNDNEXT); } return myWindows; }
- include <map>
- include <string>
- include <windows>
Note that window handles move in memory in time! So you cannot retrieve them once at the start of your program (thus making the std::map const).
How to: obtain a window's text?
Easier function around GetWindowText.
std::string getWindowText(const HWND& handle)
{
const int sizeBuffer = 100;
char buffer[sizeBuffer];
GetWindowText(handle,buffer,sizeBuffer);
const std::string windowText(buffer);
return windowText;
}
How to: get the last error?
Easier function for around GetLastError
std::string getLastError()
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
0,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
0
);
//Transform to std::string
const std::string lastError = (char*)lpMsgBuf; //A dirty cast!
// Free the buffer.
LocalFree( lpMsgBuf );
//Return the std::string
return lastError;
}
Topic links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
