📄 form1.h
字号:
#pragma once
namespace Example {
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::IO;
using namespace System::Diagnostics;
public ref class MyModuleTreeNode : TreeNode
{
public: ProcessModule^ MyModuleInfo;
public: MyModuleTreeNode(ProcessModule^ theModule)
{
MyModuleInfo = theModule;
TreeNode::Text=theModule->ModuleName;
}
};
/// <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::Button^ button1;
private: System::Windows::Forms::TreeView^ treeView1;
private: System::Windows::Forms::ListView^ listView1;
private: System::Windows::Forms::ColumnHeader^ columnHeader1;
private: System::Windows::Forms::ColumnHeader^ columnHeader2;
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->treeView1 = (gcnew System::Windows::Forms::TreeView());
this->listView1 = (gcnew System::Windows::Forms::ListView());
this->columnHeader1 = (gcnew System::Windows::Forms::ColumnHeader());
this->columnHeader2 = (gcnew System::Windows::Forms::ColumnHeader());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(3, 2);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(169, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"获取系统所有进程及模块";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// treeView1
//
this->treeView1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
| System::Windows::Forms::AnchorStyles::Left));
this->treeView1->Location = System::Drawing::Point(3, 30);
this->treeView1->Name = L"treeView1";
this->treeView1->Size = System::Drawing::Size(169, 181);
this->treeView1->TabIndex = 1;
this->treeView1->AfterSelect += gcnew System::Windows::Forms::TreeViewEventHandler(this, &Form1::treeView1_AfterSelect);
//
// listView1
//
this->listView1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
| System::Windows::Forms::AnchorStyles::Left)
| System::Windows::Forms::AnchorStyles::Right));
this->listView1->Columns->AddRange(gcnew cli::array< System::Windows::Forms::ColumnHeader^ >(2) {this->columnHeader1, this->columnHeader2});
this->listView1->FullRowSelect = true;
this->listView1->GridLines = true;
this->listView1->Location = System::Drawing::Point(176, 5);
this->listView1->Name = L"listView1";
this->listView1->Size = System::Drawing::Size(209, 207);
this->listView1->TabIndex = 2;
this->listView1->UseCompatibleStateImageBehavior = false;
this->listView1->View = System::Windows::Forms::View::Details;
//
// columnHeader1
//
this->columnHeader1->Text = L" 项目名称";
this->columnHeader1->Width = 100;
//
// columnHeader2
//
this->columnHeader2->Text = L" 项目信息";
this->columnHeader2->Width = 120;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(390, 214);
this->Controls->Add(this->listView1);
this->Controls->Add(this->treeView1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = L"演示获取指定进程模块信息";
this->ResumeLayout(false);
}
#pragma endregion
public: TreeNode^ MyRootNode ;
//显示模块信息.
private: void DisplayModuleInfo(MyModuleTreeNode^ ModuleNode)
{
this->listView1->Items->Clear();
this->listView1->Items->Add("模块名称")->SubItems->Add(ModuleNode->MyModuleInfo->ModuleName);
this->listView1->Items->Add("公司名称")->SubItems->Add(ModuleNode->MyModuleInfo->FileVersionInfo->CompanyName);
this->listView1->Items->Add("文件名称")->SubItems->Add(ModuleNode->MyModuleInfo->FileVersionInfo->FileName);
this->listView1->Items->Add("文件版本")->SubItems->Add(ModuleNode->MyModuleInfo->FileVersionInfo->FileVersion);
if (!ModuleNode->MyModuleInfo->FileVersionInfo->FileDescription->Equals(""))
this->listView1->Items->Add("文件说明")->SubItems->Add(ModuleNode->MyModuleInfo->FileVersionInfo->FileDescription);
this->listView1->Items->Add("文件创建时间")->SubItems->Add(File::GetCreationTime(ModuleNode->MyModuleInfo->FileVersionInfo->FileName).ToString());
//获取并显示文件大小.
FileInfo^ MyInfo = gcnew FileInfo(ModuleNode->MyModuleInfo->FileVersionInfo->FileName);
this->listView1->Items->Add("文件大小")->SubItems->Add(MyInfo->Length.ToString());
}
//获取系统所有进程及模块
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
int i = 0 ;
//清空树形视图节点.
treeView1->Nodes->Clear();
//初始化TreeView 的节点对象MYRootNode.
MyRootNode = gcnew TreeNode("当前系统所有进程") ;
//获取本机中运行的进程的列表.保存在Process类型的数组MyProcesses中.
array<Process^>^ MyProcesses = Process::GetProcesses();
//添加Process数组MyProcesses的每一个成员为MyRootNode对象的一个节点,并将与该进程关联的所有模块作为该节点的子节点添加到视图。
for each (Process^ MyProcess in MyProcesses)
{
MyRootNode->Nodes->Add(MyProcess->ProcessName);
ProcessModuleCollection^ MyModules = nullptr;
try
{
MyModules =MyProcess->Modules;
}
catch(Exception^ MyEx)
{ // 拒绝访问的进程模块.
continue;
}
//添加所有的模块为子节点.
for each (ProcessModule^ MyModule in MyModules)
MyRootNode->Nodes[i]->Nodes->Add(gcnew MyModuleTreeNode(MyModule));
i++ ;
}
//添加对象MyRootNode为treeView1的节点.
treeView1->Nodes->Add(MyRootNode);
}
// 显示进程模块信息
private: System::Void treeView1_AfterSelect(System::Object^ sender, System::Windows::Forms::TreeViewEventArgs^ e) {
if (treeView1->SelectedNode->Parent == nullptr)
{
;
}
else if(treeView1->SelectedNode->Parent == MyRootNode)
{
;
}
else
{ //显示模块信息.
DisplayModuleInfo(( MyModuleTreeNode^)treeView1->SelectedNode);
}
}
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -