[VC2005] APIを宣言する方法

◆概要

この資料は、Microsoft(R) Visual C# 2005で APIを宣言する方法について記述しています。

C#でWindows APIを使うにはusing System.Runtime.InteropServicesを使います。

◆使用例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; // API 関数を呼び出すため

namespace Saver
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
    class StructsAndFunctions
    {
        //APIの宣言
        [DllImport("user32.dll")]
        private static extern bool GetClientRect(
            IntPtr hWnd, ref RECT rect
            );


        public bool GetClientRectApi(IntPtr hWnd, ref RECT rect)
        {
            return GetClientRect(hWnd, ref rect);
        }
        /// <summary>
        ///このstruct(構造体)のインスタンスは、
        /// プレビューウインドウの大きさを得るために、
        /// GetClientRect APIに渡されます。
        ///スクリーン領域のために場所とサイズ情報を含む整数型の構造体。
        /// メンバーが領域を満たすことができるように、
        /// 参照変数としてGetClientRect API呼び出しに渡されます。
        /// </summary>
        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;

            public RECT(int l, int t, int r, int b)
            {
                left = l;
                top = t;
                right = r;
                bottom = b;
            }
        }
    }
}

▼ページトップへ