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
  • 魔力宝贝手游怎么刷声望
  • 热门搜索
    哺乳期怎么减肥 爱奇艺怎么退出登录 卤菜怎么做 店铺简介怎么写 苹果手机怎么清除缓存 大便干燥怎么办 软疣是怎么引起的 忘记qq密码怎么办 马怎么叫 疝气怎么治疗