利用Win32 API 函式 ShowWindowAsync / ShowWindow 改變視窗大小

leintor發表於2012-11-13

1.需要匯入 名稱空間

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);

        }

    }

}

VB完整範例

Imports System.Runtime.InteropServices

Public Class Form1

    'API 常數定義

    Private Const SW_HIDE As Integer = 0

    Private Const SW_NORMAL As Integer = 1

    Private Const SW_MAXIMIZE As Integer = 3

    Private Const SW_SHOWNOACTIVATE As Integer = 4

    Private Const SW_SHOW As Integer = 5

    Private Const SW_MINIMIZE As Integer = 6

    Private Const SW_RESTORE As Integer = 9

    Private Const SW_SHOWDEFAULT As Integer = 10

    "user32.dll")> Private Shared Function ShowWindowAsync(ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean

    End Function

    "user32.dll")> Public Shared Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer

    End Function

 

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click

        '最小化

        ShowWindowAsync(Me.Handle, SW_MINIMIZE)

    End Sub

 

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click

        '最大化

        ShowWindowAsync(Me.Handle, SW_MAXIMIZE)

    End Sub

 

    Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click

        '還原

        ShowWindowAsync(Me.Handle, SW_RESTORE)

    End Sub

 

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

        '最小化

        ShowWindow(CInt(Me.Handle), SW_MINIMIZE)

    End Sub

 

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        '最大化

        ShowWindow(CInt(Me.Handle), SW_MAXIMIZE)

    End Sub

 

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        '還原

        ShowWindow(CInt(Me.Handle), SW_RESTORE)

    End Sub

End Class

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/20200170/viewspace-749100/,如需轉載,請註明出處,否則將追究法律責任。

相關文章