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

c# 使用api函数 ShowWindowAsync 控制窗体的更多相关文章

  1. C# 窗体常用API函数 应用程序窗体查找

    常用的处理窗体的API函数如下(注意:API函数必须放在窗体中...): 使用C#语言,要引用DllImport,必须要添加using System.Runtime.InteropServices命名 ...

  2. Delphi使用Windows API函数AnimateWindow实现窗体特效

    {**********************************************************************API函数 AnimateWindow 使用:函数功能:窗 ...

  3. 通过API函数来控制SQLite数据库增删改查

    person类属性有Intenger id,String name,Intenger  age,相应的构造方法和set get方法. package com.xh.tx.dao; import and ...

  4. Delphi 窗体函数 ShowScrollBar 控制滚动条

    API函数 函数来源:FUNCTION ulong ShowScrollBar(ulong hwnd,ulong wBar,ulong bShow) LIBRARY "user32.dll& ...

  5. Windows API 函数列表 附帮助手册

    所有Windows API函数列表,为了方便查询,也为了大家查找,所以整理一下贡献出来了. 帮助手册:700多个Windows API的函数手册 免费下载 API之网络函数 API之消息函数 API之 ...

  6. API函数

    1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一个网络资源的连接 WNetAddConnection3 创建同 ...

  7. WINDOWS API 函数(超长,值得学习)

    一.隐藏和显示光标 函数: int ShowCursor ( BOOL bShow );  参数 bshow,为布尔型,bShow的值为False时隐藏光标,为True时显示光标:该函数的返回值为整型 ...

  8. Delphi 常用API 函数(好多都没见过)

    AdjustWindowRect 给定一种窗口样式,计算获得目标客户区矩形所需的窗口大小AnyPopup 判断屏幕上是否存在任何弹出式窗口ArrangeIconicWindows 排列一个父窗口的最小 ...

  9. Delphi 常用API 函数

    Delphi 常用API 函数 AdjustWindowRect 给定一种窗口样式,计算获得目标客户区矩形所需的窗口大小 AnyPopup 判断屏幕上是否存在任何弹出式窗口 ArrangeIconic ...

随机推荐

  1. BOM知识整理

    1.窗口位置: 1-1.window,screenLeft获取窗口距离屏幕左边的距离 1-2.window.screenTop获取窗口距离屏幕顶端的距离 1-3.window.screenX和wind ...

  2. (16) go 面向对象

    一.封装 二.继承 1. 2. 3. 4. 5 6. 7.多重继承 三.接口

  3. .htaccess文件

    前言 看了几篇文章,发现自己对于如何维护普通的服务器安全完全不会,先从简单的.htaccess来研究吧 .htaccess文件的作用,就是更改httpd.ini文件中的配置,但作用范围仅限当前文件夹 ...

  4. AppDomain.CurrentDomain.BaseDirectory是什么

    AppDomain.CurrentDomain.BaseDirectory 是获取基目录,它由程序集冲突解决程序用来探测程序集.由显示的路径可以看出,它代表的是程序集所在的目录,它具有读取和写入的属性 ...

  5. 【BZOJ 3308】 3308: 九月的咖啡店 (费用流|二分图最大权匹配)

    3308: 九月的咖啡店 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 244  Solved: 86 Description 深绘里在九份开了一家咖 ...

  6. [BJOI2011]禁忌 --- AC自动机 + 矩阵优化 + 期望

    bzoj 2553 [BJOI2011]禁忌 题目描述: Magic Land上的人们总是提起那个传说:他们的祖先John在那个东方岛屿帮助Koishi与其姐姐Satori最终战平.而后,Koishi ...

  7. NYOJ 298 点的变换 矩阵乘法

    http://acm.nyist.net/JudgeOnline/problem.php?pid=298 最好还是自己手推一下矩阵式子..不算太难..但是有一些小知识.... 首先当然是矩阵的细节.. ...

  8. nginx-404与fastcgi_intercept_errors指令

    nginx-404与fastcgi_intercept_errors指令 fastcgi_intercept_errors语法:fastcgi_intercept_errors on|off 默认值: ...

  9. bzoj 3120 矩阵优化DP

    我的第一道需要程序建矩阵的矩阵优化DP. 题目可以将不同的p分开处理. 对于p==0 || p==1 直接是0或1 对于p>1,就要DP了.这里以p==3为例: 设dp[i][s1][s2][r ...

  10. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E. Bear and Forgotten Tree 2 bfs set 反图的生成树

    E. Bear and Forgotten Tree 2 题目连接: http://www.codeforces.com/contest/653/problem/E Description A tre ...