NET引用API

作者在 2010-05-21 14:39:02 发布以下内容
NET引用API
API(Application Programming Interface),我想大家不会陌生,它是我们Windows编程的常客,虽然基于.Net平台的C#有了强大的类库,但是,我们还是不能否认API在Windows编程中的重要性。大多数的编程语言都支持API编程,而.Net平台中的MFC(Microsoft Foundation Class Library)构架本身就封装了大部分的API。
using System.Runtime.InteropServices;
  接着添加下面的代码来声明一个API:
  [DllImport("User32.dll")]
  public static extern int MessageBox(int h, string m, string c, int type);
这里有一个例子,是搜索整理出来的,实现抓图的功能:
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using System.Drawing.Imaging ;
namespace dllDemo20080111
{
    public partial class Form1 : Form
    {
        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        private static extern bool BitBlt(
            IntPtr hdcDest,
            int nXDest,
            int nYDest,
            int nWidth,
            int nHeight,
            IntPtr hdcSrc,
            int nXSrc,

            int nYSrc,
            System.Int32 dwRop
            );
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Rectangle rect = new Rectangle();
            rect = Screen.GetWorkingArea(this);
            Graphics grp1 = this.CreateGraphics();
            Image myImage = new Bitmap(rect.Width, rect.Height, grp1);
            Graphics grp2 = Graphics.FromImage(myImage);
            IntPtr dc1 = grp1.GetHdc();
            IntPtr dc2 = grp2.GetHdc();
            BitBlt(dc2, 0, 0, rect.Width, rect.Height, dc1, 0, 0, 13369376);
            grp1.ReleaseHdc(dc1);
            grp2.ReleaseHdc(dc2);
            myImage.Save(@"d:\capture"+DateTime.Now.ToString("yyyyDDmmHHmmss")+".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            MessageBox.Show("get");
        }
    }
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/FollowIT/archive/2008/01/11/2037431.aspx
默认分类 | 阅读 1400 次
文章评论,共2条
源本英明C
2010-06-17 13:46
1
<img src="image/face/13.gif" class="face">
源本英明C
2010-06-21 13:13
2
hh
游客请输入验证码
浏览95100次
最新评论