在VS2005,vc.net中使用EnumWindows的问题

marcus2006 2008-02-20 01:38:33
原代码和报错如下:
#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;
/// <summary>
/// Form1 摘要
///
/// 警告: 如果更改此类的名称,则需要更改
/// 与此类所依赖的所有 .resx 文件关联的托管资源编译器工具的
/// “资源文件名”属性。否则,
/// 设计器将不能与此窗体的关联
/// 本地化资源正确交互。
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
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
bool CALLBACK EnumFunc(HWND H,LPARAM L)
{
char WinName[255];
GetWindowText(H,WinName,255);
System::Console::WriteLine (WinName);
return 0;
};

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {

EnumWindows((WNDENUMPROC)EnumFunc,0);

}
};
}


1>------ 已启动生成: 项目: EnumWindow, 配置: Debug Win32 ------
1>正在编译...
1>EnumWindow.cpp
1>c:\projects\enumwindow\enumwindow\Form1.h(85) : error C2146: 语法错误 : 缺少“;”(在标识符“EnumFunc”的前面)
1>c:\projects\enumwindow\enumwindow\Form1.h(85) : error C2061: 语法错误 : 标识符“HWND”
1>c:\projects\enumwindow\enumwindow\Form1.h(86) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\projects\enumwindow\enumwindow\Form1.h(91) : warning C4183: “EnumFunc”: 缺少返回类型;假定为返回“int”的成员函数
1>c:\projects\enumwindow\enumwindow\Form1.h(88) : error C2065: “H”: 未声明的标识符
1>c:\projects\enumwindow\enumwindow\Form1.h(88) : error C3861: “GetWindowText”: 找不到标识符
1>c:\projects\enumwindow\enumwindow\Form1.h(89) : warning C4800: “char *”: 将值强制为布尔值“true”或“false”(性能警告)
1>c:\projects\enumwindow\enumwindow\Form1.h(94) : error C2065: “WNDENUMPROC”: 未声明的标识符
1>c:\projects\enumwindow\enumwindow\Form1.h(94) : error C2146: 语法错误 : 缺少“)”(在标识符“EnumFunc”的前面)
1>c:\projects\enumwindow\enumwindow\Form1.h(94) : error C2059: 语法错误 : “)”
1>.\EnumWindow.cpp(12) : fatal error C1903: 无法从以前的错误中恢复;正在停止编译
1>生成日志保存在“file://c:\Projects\EnumWindow\EnumWindow\Debug\BuildLog.htm”
1>EnumWindow - 9 个错误,2 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
...全文
240 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ms44 2008-02-20
  • 打赏
  • 举报
回复
佩服楼上的。
支持。。
whycom 2008-02-20
  • 打赏
  • 举报
回复
#pragma once

#include "windows.h"
namespace callenumwnd {

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>
#pragma unmanager
static BOOL CALLBACK EnumWndProc( HWND hwnd , LPARAM lpAram )
{
return TRUE;
}
#pragma mananager
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此处添加构造函数代码
//
}

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

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

#pragma region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(129, 186);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// 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->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);

}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
#pragma unmanager
::EnumWindows(EnumWndProc,0);
#pragma manager

}

};

}


这是非托管方式,注意项目不能不同clr/pure or clr/safe 方式编译
whycom 2008-02-20
  • 打赏
  • 举报
回复
#pragma once


namespace callenumwnd {

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);
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此处添加构造函数代码
//
}

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

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

#pragma region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(129, 186);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// 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->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);

}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
EnumWndProc^ ewp = gcnew EnumWndProc(Form1::enproc);
EnumWindows(ewp,0);

}
static int enproc(System::IntPtr hwnd , int lpAram)
{
return 1;
}
};
}

这是importdll 方式调用
whycom 2008-02-20
  • 打赏
  • 举报
回复
等等 我给你一个完整的代码
marcus2006 2008-02-20
  • 打赏
  • 举报
回复
To whycom,ms44
我的环境是VS2005的vc.net
marcus2006 2008-02-20
  • 打赏
  • 举报
回复
To ms44
直接包含头文件,调用。即可
直接包含啥头文件?
marcus2006 2008-02-20
  • 打赏
  • 举报
回复
To whycom---

发表于:2008-02-20 13:46:592楼 得分:0
1、api 最好用 dllimport 方式
2、你那个类是托管类,而内部有非托管代码
前面加上

#program unmanager

问题1:
加在哪里呢?是不是#pragma unmanager 而不是#program unmanager?
#pragma unmanager
bool CALLBACK EnumFunc(HWND H,LPARAM L)
{
char WinName[255];
GetWindowText(H,WinName,255);
System::Console::WriteLine (WinName);
return 0;
};
但是我写成#pragma unmanager并没变成兰色关键字哦!!!
2. 这个类是托管类,我在那里写才不算非托管代码?
3. 我怎么知道我写在了一个托管类里了?
ms44 2008-02-20
  • 打赏
  • 举报
回复
直接包含头文件,调用。即可
marcus2006 2008-02-20
  • 打赏
  • 举报
回复
在VC.net中使用windows API是否有什么要求,麻烦写一下调用的步骤
比如:
方法1,用include <XXX.h>
在主程序中调用api函数
方法2,用[dllimport XXX]
申明api函数
在主程序中调用
whycom 2008-02-20
  • 打赏
  • 举报
回复
1、api 最好用 dllimport 方式
2、你那个类是托管类,而内部有非托管代码
前面加上

#program unmanager

marcus2006 2008-02-20
  • 打赏
  • 举报
回复
绿色的是自己写的,其他的都是系统自己默认的

7,540

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧