利用Win32 API 函式 ShowWindowAsync / ShowWindow 改變視窗大小
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
End Function
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- js實現的拖動改變視窗大小功能JS
- 呼叫API函式設計ABOUT視窗 (轉)API函式
- Qt5應用改變視窗大小時出現黑影QT
- SQL KEEP 視窗函式等價改寫案例SQL函式
- 視窗函式函式
- 常用Win32 API函式簡介Win32API函式
- PostgreSQL:視窗函式SQL函式
- SQL 視窗函式SQL函式
- Mysql視窗函式MySql函式
- Hive視窗函式Hive函式
- 監聽視窗大小改變,同時根據視窗大小修改某個元素的大小
- Win32 API之Setlocale函式配置地域化資訊函式Win32API函式
- Oracle分析函式與視窗函式Oracle函式
- C# 強行鎖定 第三方 外部 應用程式窗體視窗的解析度尺寸大小 禁止滑鼠拖拽改變視窗大小C#
- hive視窗函式使用Hive函式
- 【函式】oracle視窗函式over()的理解函式Oracle
- 瀏覽器的視窗大小被改變時觸發的事件window.onresize瀏覽器事件
- Win10如何改變切換視窗開啟所有視窗Win10
- VB呼叫API函式使視窗保持在最上層 (轉)API函式
- hive視窗分析函式使用詳解系列二之分組排序視窗函式Hive函式排序
- 與SQL視窗函式相同SQL函式
- PostgreSQL>視窗函式的用法SQL函式
- MySQL視窗函式彙總MySql函式
- hive05_視窗函式Hive函式
- mysql視窗函式中的滑動視窗MySql函式
- vue 動態監聽視窗大小變化事件Vue事件
- tmux 共享視窗大小UX
- SQL視窗分析函式使用詳解系列三之偏移量類視窗函式SQL函式
- MySQL 8.0 視窗函式-筆記MySql函式筆記
- Hive視窗函式保姆級教程Hive函式
- Hive 視窗函式(Windowing Functions)Hive函式Function
- 用函式控制彈出視窗函式
- MySQL視窗函式用法總結MySql函式
- win32的建立視窗程式碼Win32
- JS彈出視窗視窗的位置和大小JS
- Win32 API CreateCompatibleDC 函式的相關應用Win32API函式
- 【MATLAB】利用size()函式輸出陣列大小Matlab函式陣列
- 設定CMD視窗大小