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. Codeforces Round #278 (Div. 1) D - Conveyor Belts 分块+dp

    D - Conveyor Belts 思路:分块dp, 对于修改将对应的块再dp一次. #include<bits/stdc++.h> #define LL long long #defi ...

  2. 宝塔面板php扩展安装

    yum install libmcrypt libmcrypt-devel mcrypt mhash wget http://pecl.php.net/get/mcrypt-1.0.1.tgz tar ...

  3. CodeForces 772A Voltage Keepsake

    二分答案,验证. 二分到一个答案,比他小的时间都需要补充到这个时间,计算所需的量,然后和能提供的量进行比较. #include <cstdio> #include <cmath> ...

  4. Bzoj2152/洛谷P2634 聪聪可可(点分治)

    题面 Bzoj 洛谷 题解 点分治套路走一波,考虑\(calc\)函数怎么写,存一下每条路径在\(\%3\)意义下的路径总数,假设为\(tot[i]\)即\(\equiv i(mod\ 3)\),这时 ...

  5. 【BZOJ 3659】 3659: Which Dreamed It (Matrix-Tree&BEST theorem )

    3659: Which Dreamed It Time Limit: 20 Sec  Memory Limit: 1024 MBSubmit: 134  Solved: 41 Description ...

  6. POJ 3974 Palindrome 字符串 Manacher算法

    http://poj.org/problem?id=3974 模板题,Manacher算法主要利用了已匹配回文串的对称性,对前面已匹配的回文串进行利用,使时间复杂度从O(n^2)变为O(n). htt ...

  7. Luogu P3962 [TJOI2013]数字根 st

    题面 我先对数字根打了个表,然后得到了一个结论:\(a\)的数字根=\((a-1)mod 9+1\) 我在询问大佬后,大佬给出了一个简单的证明: \(\because 10^n\equiv 1(mod ...

  8. 【推导】【贪心】【高精度】Gym - 101194E - Bet

    题意:每个队伍有个赔率pi,如果你往他身上押x元,它赢了,那么你得到x+(1/pi)x元,否则你一分都得不到.问你最多选几支队伍去押,使得存在一种押的方案,不论你押的那几支队伍谁赢,你都能赚得到钱. ...

  9. [ZHOJ1131]Find K Min

    题目大意: 给你一个数列,求其中第K大的数. 思路: 类似于快速排序的思想,每次可以确定出当前的的x在数组中的位置. 然后根据位置选择该往左找还是往右找. #pragma GCC optimize(3 ...

  10. django之cookie、session和ajax

    1 Cookie cookie是什么? 保存在客户端浏览器上的键值对   {k:v} cookie依附在请求头或响应头中出现 发送请求时,会自动携带自己网站的cookie 应用: 实现登录 投票 1. ...