C#调用Windows API简单实例

 时间:2026-02-14 11:38:16

    在WinForm中也可以调用Windows API,这里的简单例子,只是做一个简单的演示:

方法/步骤

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace WinAPISample

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void btnClear_Click(object sender, EventArgs e)

        {

            SHEmptyRecycleBin(Form.ActiveForm.Handle, "", 0x00000000);

        }

        private void btnMax_Click(object sender, EventArgs e)

        {

            ShowWindow(Form.ActiveForm.Handle, 3);

        }

        private void btnMin_Click(object sender, EventArgs e)

        {

            ShowWindow(Form.ActiveForm.Handle, 2);

        }

        private void btnNormal_Click(object sender, EventArgs e)

        {

            ShowWindow(Form.ActiveForm.Handle, 1);

        }

        private void btnBrowser_Click(object sender, EventArgs e)

        {

            ShellExecute(Form.ActiveForm.Handle, "Open", this.tbURL.Text, "", "", 1);

        }

        /// <summary>

        /// 清空回收站

        /// </summary>

        [DllImport("shell32.dll", EntryPoint = "SHEmptyRecycleBin", CharSet = CharSet.Auto)]

        public static extern long SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, long dwFlags);

        /// <summary>

        /// 打开浏览器

        /// </summary>

        [DllImport("shell32.dll", EntryPoint = "ShellExecute", CharSet = CharSet.Auto)]

        public static extern int ShellExecute(IntPtr hwnd, string lpOperation, string lpFile,string lpParameters, string lpDirectory, int nShowCmd);

        /// <summary>

        /// 最大化窗口,最小窗口,正常大小窗口

        /// </summary>

        [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]

        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

    }

}

C#调用Windows API简单实例

  • 用七巧板怎样拼一把插地上的大刀?
  • 怎样画南瓜灯的简笔画教程
  • 手工制作收纳桶
  • 怎么样玩七巧板拼数字1
  • 魔力宝贝手游怎么刷声望
  • 热门搜索
    安全知识手抄报图片 女人喝蒲公英水的作用 晟念什么 什么是创业板股票 葫芦的风水作用 教育教学知识与能力 1943年属什么 什么牌子的蜂蜜好 全国大学生网络安全知识竞赛 校园安全知识手抄报