C#调用Windows API简单实例

2025-10-16 02:16:34

    在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简单实例

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢