本来是想实现控制台程序运行时自动全屏,但是只找到VC下的实现方法(http://www.vckbase.com/bbs/prime/viewprime.asp?id=347)。

其中要使用两个未公开的Win32 API函数来存取控制台窗口,这就需要使用动态调用的方法,动态调用中使用的Windows API函数主要有三个,即:Loadlibrary,GetProcAddress和Freelibrary。步骤如下:

1.   Loadlibrary: 装载指定DLL动态库

2.   GetProcAddress:获得函数的入口地址

3.   Freelibrary: 从内存中卸载动态库

但是C#中是没有函数指针,无法直接使用GetProcAddress返回的入口地址。后来找到资料,其实.NET 2.0新增了Marshal.GetDelegateForFunctionPointer 方法可以满足这个要求,MSDN里的解释是:将非托管函数指针转换为委托。

后面的事就简单啦,我把它编成了一个类来方便调用。

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace feiyun0112.cnblogs.com
{
    public class DllInvoke
    {

Win API#region Win API
        [DllImport("kernel32.dll")]
        private extern static IntPtr LoadLibrary(string path);

[DllImport("kernel32.dll")]
        private extern static IntPtr GetProcAddress(IntPtr lib, string funcName);

[DllImport("kernel32.dll")]
        private extern static bool FreeLibrary(IntPtr lib);
        #endregion

private IntPtr hLib;        
        public DllInvoke(String DLLPath)
        {
            hLib = LoadLibrary(DLLPath);
        }

~DllInvoke()
        {
            FreeLibrary(hLib);            
        }

//将要执行的函数转换为委托
        public Delegate Invoke (string APIName,Type t)  
        {
            IntPtr api = GetProcAddress(hLib, APIName);
            return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);
        }

}
}

下面是使用的例子:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using feiyun0112.cnblogs.com;

namespace ConsoleApplication1
{
    class Program
    {
        Win API#region Win API
        [DllImport("kernel32.dll")]
        public static extern IntPtr GetStdHandle(int nStdHandle);
        const int STD_OUTPUT_HANDLE = -11;
        #endregion

public delegate bool SetConsoleDisplayMode(IntPtr hOut, int dwNewMode, out int lpdwOldMode);

static void Main(string[] args)
        {
            DllInvoke dll = new DllInvoke("kernel32.dll");
            
            int dwOldMode;

//标准输出句柄
            IntPtr hOut = GetStdHandle(STD_OUTPUT_HANDLE);
            
            //调用Win API,设置屏幕最大化
            SetConsoleDisplayMode s = (SetConsoleDisplayMode)dll.Invoke("SetConsoleDisplayMode", typeof(SetConsoleDisplayMode));
            s(hOut, 1, out dwOldMode);

Console.WriteLine("********************Full Screen Mode********************");
            Console.ReadLine();
           
        }
    }
}

最新文章

  1. web api9
  2. Devexpress Ribbon
  3. Codeforces Round #341 Div.2 B. Wet Shark and Bishops
  4. 2017/1/8 C语言程序练习d
  5. JAVA本地方法详解,什么是JAVA本地方法?
  6. SQL Server数据库(作业讲解和复习)
  7. ibatis基础
  8. 用angular实时获取本地localStorage数据,实现一个模拟后台数据登入的效果
  9. VirtualBox NAT方式与主机互相通信
  10. Objective-c学习笔记3
  11. 怎么监控apache运行状态和页面统计
  12. NancyFx 2.0的开源框架的使用-ModelBinding(实现绑定)
  13. http get(swift and oc)
  14. context.go
  15. LeetCode 单链表专题 (一)
  16. topic的leader显示为none的解决办法
  17. cnblogs latex公式
  18. C#:TextBox数据绑定
  19. div+CSS实现页面的布局要点记录
  20. JS笔记—03(DOM编程)

热门文章

  1. Firefox 43无法安装xpi的问题
  2. PreparedStatementSQLException
  3. MinGW-编译器
  4. Git代码行统计命令集
  5. TSQL--可以在触发器中使用COMMIT吗?
  6. Redis!
  7. jQuery判断是否选中
  8. 【转】ListBox Dock Fill 总是有空隙的问题
  9. [ActionScript 3.0] 结合FMS实现简单视频录制
  10. CF765F Souvenirs 离线+线段树+主席树