读书人

Whycom;Akira;依然是EnumWindows,Enum

发布时间: 2012-07-28 12:25:13 作者: rapoo

Whycom;Akira;依然是EnumWindows,EnumDesktopWIndows的问题
首先要感谢两位的指点,也希望我们的问题能帮助很多迷惑的开发或入门人员.
修正后的代码运行发现不能enum桌面所有窗体,
运行result=EnumDesktopWindows(Desk,ewp,0);后使用GetLastError发现error code 为6,
如果使用Enumwindows只执行了1次就结束了,但桌面的窗口一个没有抓到
即handle is invalid
绿色的就是修正后的代码,目的是为了获取桌面所有窗口的标题
#pragma once
namespace EnumWindow {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Runtime::InteropServices ;
/// <summary>
/// Form1 摘要
///
/// 警告: 如果更改此类的名称,则需要更改
/// 与此类所依赖的所有 .resx 文件关联的托管资源编译器工具的
/// “资源文件名”属性。否则,
/// 设计器将不能与此窗体的关联
/// 本地化资源正确交互。
/// </summary>
delegate int EnumWndProc(System::IntPtr wnd,int LPARAM); public ref class Form1 : public System::Windows::Forms::Form
{

[DllImport("user32.dll")]
static public int EnumWindows(EnumWndProc^ ep,int LPARAM);
[DllImport("user32.dll")]
static public long GetDesktopWindow(void);
[DllImport("user32.dll")]
static public bool EnumDesktopWindows(long hwnd,EnumWndProc^ ep,int LPARAM);
[DllImport("user32.dll")]
static public int GetWindowText(System::IntPtr hwnd,char* name,int COUNT);
[DllImport("Kernel32.dll")]
static public long GetLastError(void);
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此处添加构造函数代码
//
}

protected:
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::ListBox^ listBox1;
protected:

private:
/// <summary>
/// 必需的设计器变量。
/// </summary>
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this-> listBox1 = (gcnew System::Windows::Forms::ListBox());
this-> SuspendLayout();
//
// listBox1
//
this-> listBox1-> FormattingEnabled = true;
this-> listBox1-> ItemHeight = 12;
this-> listBox1-> Location = System::Drawing::Point(43, 33);
this-> listBox1-> Name = L"listBox1";
this-> listBox1-> Size = System::Drawing::Size(221, 76);
this-> listBox1-> TabIndex = 0;
//
// Form1
//
this-> AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this-> AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this-> ClientSize = System::Drawing::Size(292, 266);
this-> Controls-> Add(this-> listBox1);
this-> Name = L"Form1";
this-> Text = L"Form1";
this-> Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this-> ResumeLayout(false);

}


#pragma endregion
private: System::Void Form1_Load(System::Object^ sender,System::EventArgs^ e)
{
bool result;
long error;
EnumWndProc^ ewp=gcnew EnumWndProc(Form1::enproc);
long Desk;
Desk=GetDesktopWindow();
result=EnumDesktopWindows(Desk,ewp,0);
error=GetLastError();
//EnumWindows(ewp,0);
}

static int enproc(System::IntPtr hwnd,int lparam)
{

char WinName[255];
GetWindowText(hwnd,WinName,255);
System::Console::Write(WinName);
return 1;
}

};
}

[解决办法]

C/C++ code
using namespace System;using namespace Runtime::InteropServices;using namespace Text;public delegate IntPtr CallBack(IntPtr hwnd, IntPtr lParam);private ref class Program{public:    [DllImport("user32")]    static public IntPtr EnumWindows(CallBack^ x, IntPtr y);    [DllImport("user32")]    static public IntPtr GetWindowText(IntPtr hwnd, StringBuilder^ sysDirBuffer, IntPtr len);    static public IntPtr Report(IntPtr hwnd, IntPtr lParam)    {        StringBuilder^ str = gcnew StringBuilder( 256 );        GetWindowText( hwnd , str , IntPtr(255) );        Console::WriteLine( "{0}={1}", hwnd , str );        return IntPtr(1);    }};int main(array<System::String ^> ^args){    CallBack^ pfn = gcnew CallBack( Program::Report );    Program::EnumWindows( pfn , IntPtr::Zero );    return 0;} 

读书人网 >VC

热点推荐