首先要感谢两位的指点,也希望我们的问题能帮助很多迷惑的开发或入门人员.
修正后的代码运行发现不能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);
}