⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.h

📁 这是传统的 COM 知识
💻 H
字号:
#pragma once

#include "MyTime.h"

namespace My
{
	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;

//最普通的调用
typedef IntPtr HWND;//就是void *
[DllImport("user32", EntryPoint="MessageBoxA")]
extern "C" int MsgBox(HWND hWnd,
                      String*  pText,
                      String*  pCaption,
                      unsigned int uType);

/*
对于结构,比如,User32.dll中的这个函数
BOOL PtInRect(const RECT *lprc, POINT pt);
RECT和POINT是两个结构
*/
[StructLayout(LayoutKind::Sequential)]
public __value struct Point {
    public:
		int x;
		int y;
};
//Explicit表示对象的成员按照FieldOffset(即FieldOffsetAttribute属性类)指定的位置进行内存布局
[StructLayout(LayoutKind::Explicit)]
public __gc struct Rect {
	public:
    [FieldOffset(0)] int left;		//FieldOffset()中的数字是内存布局,
    [FieldOffset(4)] int top;		//一定不能写了,这里都是int,所以每次都+4
    [FieldOffset(8)] int right;
    [FieldOffset(12)] int bottom;
};
[DllImport("User32.dll")]
extern "C" bool PtInRect(const Rect& r, Point p);

//类传递
[DllImport("Kernel32.dll")]
extern "C" void GetSystemTime(MySystemTime& st);	//MySystemTime是一个类

//回调函数
using namespace System::Runtime::InteropServices;
__delegate bool CallBack(int hwnd, int lParam);		//定义一个委托
[DllImport("user32")] 
extern "C" int EnumWindows(CallBack* x, int y); 	//参数CallBack从函数指针变成了委托,其实它们大同小异


	public __gc class Form1 : public System::Windows::Forms::Form
	{	
	public:
		Form1(void)
		{
			InitializeComponent();
		}


	protected:
		void Dispose(Boolean disposing)
		{
			if (disposing && components)
			{
				components->Dispose();
			}
			__super::Dispose(disposing);
		}
	private: System::Windows::Forms::Button *  button1;
	private: System::Windows::Forms::GroupBox *  groupBox1;
	private: System::Windows::Forms::Button *  button2;
	private: System::Windows::Forms::Button *  button3;

	private: 


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

		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = new System::Windows::Forms::Button();
			this->groupBox1 = new System::Windows::Forms::GroupBox();
			this->button2 = new System::Windows::Forms::Button();
			this->button3 = new System::Windows::Forms::Button();
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(96, 112);
			this->button1->Name = S"button1";
			this->button1->Size = System::Drawing::Size(96, 32);
			this->button1->TabIndex = 0;
			this->button1->Text = S"显示MsgBox";
			this->button1->Click += new System::EventHandler(this, button1_Click);
			// 
			// groupBox1
			// 
			this->groupBox1->Dock = System::Windows::Forms::DockStyle::Top;
			this->groupBox1->Location = System::Drawing::Point(0, 0);
			this->groupBox1->Name = S"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(292, 96);
			this->groupBox1->TabIndex = 1;
			this->groupBox1->TabStop = false;
			this->groupBox1->Text = S"PtInRect";
			this->groupBox1->MouseDown += new System::Windows::Forms::MouseEventHandler(this, groupBox1_MouseHover);
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(96, 152);
			this->button2->Name = S"button2";
			this->button2->Size = System::Drawing::Size(96, 32);
			this->button2->TabIndex = 0;
			this->button2->Text = S"GetTime";
			this->button2->Click += new System::EventHandler(this, button2_Click);
			// 
			// button3
			// 
			this->button3->Location = System::Drawing::Point(98, 200);
			this->button3->Name = S"button3";
			this->button3->Size = System::Drawing::Size(96, 32);
			this->button3->TabIndex = 0;
			this->button3->Text = S"回调函数";
			this->button3->Click += new System::EventHandler(this, button3_Click);
			// 
			// Form1
			// 
			this->AutoScaleBaseSize = System::Drawing::Size(6, 14);
			this->ClientSize = System::Drawing::Size(292, 266);
			this->Controls->Add(this->groupBox1);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button3);
			this->Name = S"Form1";
			this->Text = S"Form1";
			this->MouseDown += new System::Windows::Forms::MouseEventHandler(this, Form1_MouseDown);
			this->ResumeLayout(false);

		}	
	private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
			 {//显示MsgBox
				 MsgBox(this->Handle,"hello","hi",0);
			 }
	private: System::Void Form1_MouseDown(System::Object *  sender, System::Windows::Forms::MouseEventArgs *  e)
			 {//按下鼠标判断是否在groupBox1内部,呵呵没有调整pt的相对值,所以groupBox只能在最上面:)
				 Point pt;
				 pt.x=e->X;
				 pt.y=e->Y;
				 Rect *rect=new Rect;
				 rect->bottom=groupBox1->Bottom;
				 rect->left=groupBox1->Left;
				 rect->right=groupBox1->Right;
				 rect->top=groupBox1->Top;

				 if(PtInRect(*rect,pt)) MsgBox(this->Handle,"点击在groupBox内部了","hi",0);
				 else MsgBox(this->Handle,"点击在groupBox外部了","hi",0);
			 }
	private: System::Void groupBox1_MouseHover(System::Object *  sender, System::Windows::Forms::MouseEventArgs * e)
			 {
				 Form1_MouseDown(sender,e);
			 }

	private: System::Void button2_Click(System::Object *  sender, System::EventArgs *  e)
			 {//GetTime
				 MySystemTime *time=new MySystemTime;
				 GetSystemTime(*time);
				 String *temp;
				 temp="Windows已运行了:";
				 temp=String::Concat(temp,time->wHour.ToString(),":",time->wMinute.ToString());
				 MsgBox(this->Handle,temp,"hi",0);
			 }

//回调函数,在调试窗口显式窗口句柄
static String *MyHwnd="Window handle is: \n";
static int count=0;
private: bool Report(int hwnd, int lParam)
		 {
			 MyHwnd=String::Concat(MyHwnd,hwnd.ToString(),"\t");
			 count++;
			 System::Diagnostics::Trace::WriteLine(
				 String::Concat(hwnd.ToString(),",count=",count.ToString()),"Window handle is:");
			 return true;
		 }
private: System::Void button3_Click(System::Object *  sender, System::EventArgs *  e)
		 {//回调函数
			 MyHwnd="Window handle is: \n";
			 count=0;
			 CallBack* myCallBack = new CallBack(this, Report);
			 if(EnumWindows(myCallBack, 0)) 	//将函数指针(实例化的委托)传给COM服务器
				MsgBox(this->Handle,MyHwnd,count.ToString(),0);//不加if()也没事,单线程的,主程序被COM阻塞了,不完成谁也别想动
		 }
};
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -