在Windows Mobile和Wince(Windows Embedded CE)下進行Win32開發,取出視窗控制代碼的方法

來源:互聯網
上載者:User

在上一篇文章講述了取進程資訊的方法,連結如下:

在Windows Mobile和Wince(Windows Embedded CE)下進行Win32開發,取出當前所有運行中進程資訊的方法

本文講述取出進程對應視窗控制代碼的方法。

 

程式是在上一篇文章的例子上進行修改的。

核心代碼如下:

ProcessWndsInfo procInfo;
procInfo.processId = it->th32ProcessID;
if (EnumWindows(EnumWindowsProc, (LPARAM)&procInfo) == FALSE)
{
wprintf(TEXT("EnumWindowsProc Failed. Error: %d\n"),
GetLastError());
continue;
}

通過調用EnumWindows()來取出進程下所有視窗資訊。EnumWindows()函數的第一個參數是 回呼函數的指標,代碼如下:

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
ProcessWndsInfo* procInfo = (ProcessWndsInfo*)lParam;
DWORD ProcessId;

GetWindowThreadProcessId ( hwnd, &ProcessId );

if (procInfo != NULL && ProcessId == procInfo->processId)
{
procInfo->windowsHandles.push_back(hwnd);
}

// Keep enumerating
return true;
}

由於EnumWindows()函數輪詢所有視窗,每個視窗都會會回調EnumWindowsProc()一次。所以需要判斷這個視窗所屬進程號。如果屬於該進程,就當到list裡面。

ProcessWndsInfo的定義如下:

struct ProcessWndsInfo
{
DWORD processId;
std::list<HWND> windowsHandles;
};

包含的進程ID和進程下所有視窗的控制代碼的list。

 

下面是顯示視窗控制代碼資訊的代碼。

// Output information for each running process
for( std::list<PROCESSENTRY32>::iterator it=processes.begin();
it!=processes.end(); ++it)
{
wprintf(TEXT("%-*s %8X %13d %9d %9X %10X\n"),
maxProcessNameLength,
it->szExeFile,
it->th32ProcessID,
it->pcPriClassBase,
it->cntThreads,
it->th32MemoryBase,
it->th32AccessKey
);

ProcessWndsInfo procInfo;
procInfo.processId = it->th32ProcessID;
if (EnumWindows(EnumWindowsProc, (LPARAM)&procInfo) == FALSE)
{
wprintf(TEXT("EnumWindowsProc Failed. Error: %d\n"),
GetLastError());
continue;
}

//Output a header to describe the HWnd
wprintf(TEXT("\tHWnd\t\tWindows Title\n"));

WCHAR title[255];
for( std::list<HWND>::iterator itHWnd=procInfo.windowsHandles.begin();
itHWnd!=procInfo.windowsHandles.end(); ++itHWnd)
{
//Get title of the window
GetWindowText(*itHWnd, title, 255);
wprintf(TEXT("\t%10X\t%s\n"), *itHWnd, title);
}
}

為了顯示更詳細的資訊,我把視窗名稱一同顯示了。效果如下:

 

原始碼:http://files.cnblogs.com/procoder/GetHWnd-without-project-file.rar

由於我在wince下進行開發的,不是通用平台,我把專案檔刪掉了,上傳原始碼,只用於參考。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.