c# 使用api函数 ShowWindowAsync 控制窗体

1.需要匯入 System.Runtime.InteropServices 命名空間

2.宣告 ShowWindowAsync 函數

        [DllImport("user32.dll")]

        private static extern bool ShowWindowAsync(

            IntPtr hWnd,

            int nCmdShow

        );

3.宣告 ShowWindow函數

        [DllImport("user32.dll")]

        public static extern int ShowWindow(

            int hwnd,

            int nCmdShow

        );

4.宣告API常數定義

        //API 常數定義

        private const int SW_HIDE = 0;

        private const int SW_NORMAL = 1;

        private const int SW_MAXIMIZE = 3;

        private const int SW_SHOWNOACTIVATE = 4;

        private const int SW_SHOW = 5;

        private const int SW_MINIMIZE = 6;

        private const int SW_RESTORE = 9;

        private const int SW_SHOWDEFAULT = 10;

5.上述函數功能相同,都是用來設定視窗大小,不同的是宣告的型態不一樣需轉型。

ShowWindowAsync(this.Handle, SW_MINIMIZE);

ShowWindow((int)this.Handle, SW_MINIMIZE);

 

6.若是把int 改成IntPtr ,使用ShowWindow就不用轉型,所以在宣告時就可以考慮資料型態,必免轉型所耗的資源。

        [DllImport("user32.dll")]

        public static extern int ShowWindow(

            int hwnd,

            int nCmdShow

        );

 

C#完整範例

using System;

using System.Windows.Forms;

 

using System.Runtime.InteropServices;

 

namespace CS_WindowsResize

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        //API 常數定義

        private const int SW_HIDE = 0;

        private const int SW_NORMAL = 1;

        private const int SW_MAXIMIZE = 3;

        private const int SW_SHOWNOACTIVATE = 4;

        private const int SW_SHOW = 5;

        private const int SW_MINIMIZE = 6;

        private const int SW_RESTORE = 9;

        private const int SW_SHOWDEFAULT = 10;

 

        [DllImport("user32.dll")]

        private static extern bool ShowWindowAsync(

            IntPtr hWnd,

            int nCmdShow

        );

 

        [DllImport("user32.dll")]

        public static extern int ShowWindow(

            int hwnd,

            int nCmdShow

        );

        private void button1_Click(object sender, EventArgs e)

        {

            //最小化

            ShowWindowAsync(this.Handle, SW_MINIMIZE);

        }

        private void button2_Click(object sender, EventArgs e)

        {

            //最大化

            ShowWindowAsync(this.Handle, SW_MAXIMIZE);

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            //還原

            ShowWindowAsync(this.Handle, SW_RESTORE);

        }

 

        private void button4_Click(object sender, EventArgs e)

        {

            //最小化

            ShowWindow((int)this.Handle, SW_MINIMIZE);

        }

 

        private void button5_Click(object sender, EventArgs e)

        {

            //最大化

            ShowWindow((int)this.Handle, SW_MAXIMIZE);

        }

 

        private void button6_Click(object sender, EventArgs e)

        {

            //還原

            ShowWindow((int)this.Handle, SW_RESTORE);

        }

    }

}

 

转载自:http://www.dotblogs.com.tw/yc421206/archive/2009/07/06/9140.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值