おべんきょうwiki

プログラミング:C

最終更新:

匿名ユーザー

- view
管理者のみ編集可

プログラミング:C#


MSDNのリファレンス重過ぎない?
覚書程度に.

TIPS


手動で描画イベントハンドラの設定


IDEを使わないケース.

   private void test_paint(Object sender, PaintEventArgs e){
     Graphics g=e.Graphics;
     g.DrawHogeHoge(); //なんか描画する
   }

あとはコンストラクタで

     this.Paint+=new PaintEventHandler(test_paint);

クライアントサイズの設定とリサイズ禁止


Javaと違ってクライアントサイズが使えるのがうれしい.
リサイズ禁止属性はぱっとみ見当たらないが,
FormBorderStyleを指定するとリサイズできなくなる.
最大最小サイズを設定するだけだとリサイズは結果的にできないがドラッグできてしまう.
たぶん下のコードが正解.

      this.ClientSize=new Size(200,200);
     this.MaximumSize=this.Size;
     this.MinimumSize=this.Size;
     this.FormBorderStyle=FormBorderStyle.FixedSingle;


MIDI制御


MidiControl.csを書いた.
基本的にはwinmm.dllをDLLImportして,
Cの関数定義をトランスレートして呼び出すだけ.
ただし構造体とかめんどくさすぎてやっていない.
ポインタを使う場合はIntPtrを使う.
厄介なのはnew IntPtr(i)とかしてCの関数でポインタの中身をいじっても
C#側のiが変更されない点.

using System;
using System.Runtime.InteropServices;

namespace test{
 public class MidiControl{
   /* winmm midi interface */
   [DllImport("winmm.dll")]
   private static extern uint midiOutOpen(
     ref IntPtr lphmo,
     uint uDeviceID,
     uint dwCallback,
     uint dwCallbackInstance,
     uint dwFlags);
   
   [DllImport("winmm.dll")]
   private static extern uint midiOutClose(int hmo);

   [DllImport("winmm.dll")]
   private static extern uint midiOutGetNumDevs();

   [DllImport("winmm.dll")]
   private static extern uint midiOutShortMsg(int hmo, uint dwMsg);
   /* /winmm midi interface */

   private int mdev;
   private uint res;

   private static MidiControl inst;
   
   public static MidiControl GetInstance(){
     if(inst==null){
       inst=new MidiControl();
     }
     return inst;
   }
   
   private MidiControl(){}

   public uint Open(uint dev){
     IntPtr mdevintptr=new IntPtr();
     res=midiOutOpen(ref mdevintptr,dev,0,0,0);
     mdev=mdevintptr.ToInt32();
     return res;
   }

   public uint Close(){
     return midiOutClose(mdev);
   }

   public uint ShortMsg(uint ctrl){
     return midiOutShortMsg(mdev,ctrl);
   }
   
 }

}

ジョイパッド(Win32API)


DirectInputを使わないパターン.
Cの場合次が参考になる.


問題なのはJoyInfoEx構造体を使う必要があること.
構造体の定義はmmsystem.hを直接コピー.DWORDはuintに.
構造体の扱いは次を参考に.

@IT:.NET TIPS Win32 APIやDLL関数に構造体を渡すには? - C#
http://www.atmarkit.co.jp/fdotnet/dotnettips/026w32struct/w32struct.html

一応次のコードで動くっぽい.

using System;
using System.Runtime.InteropServices;

namespace test{

 [StructLayout(LayoutKind.Sequential)]
 public struct JoyInfoEx{
   public uint dwSize;                /* size of structure */
   public uint dwFlags;               /* flags to indicate what to return */
   public uint dwXpos;                /* x position */
   public uint dwYpos;                /* y position */
   public uint dwZpos;                /* z position */
   public uint dwRpos;                /* rudder/4th axis position */
   public uint dwUpos;                /* 5th axis position */
   public uint dwVpos;                /* 6th axis position */
   public uint dwButtons;             /* button states */
   public uint dwButtonNumber;        /* current button number pressed */
   public uint dwPOV;                 /* point of view state */
   public uint dwReserved1;           /* reserved for communication between winmm & driver */
   public uint dwReserved2;
 }

 public class JoyInfo{
   /* winmm joyinfoex interface */

   [DllImport("winmm.dll")]
   private static extern uint joyGetPosEx(uint uJoyID, IntPtr pji);
   /* /winmm joyinfoex interface */

   private IntPtr pji;
   private JoyInfoEx ji;
   private uint res;

   private static JoyInfo inst;

   public static JoyInfo GetInstance(){
     if(inst==null){
       inst=new JoyInfo();
     }
     return inst;
   }

   private JoyInfo(){
     ji=new JoyInfoEx();
     ji.dwSize=Marshal.SizeOf(ji);
     ji.dwFlags=0x00ff; // JOY_RETURNALL
     pji=Marshal.AllocHGlobal(Marshal.SizeOf(ji));
     Marshal.StructureToPtr(ji,pji,false);
   }

   ~JoyInfo(){
     Marshal.FreeHGlobal(pji);
   }

   public JoyInfoEx GetPosEx(uint jid){
     res=joyGetPosEx(jid,pji);
     return (JoyInfoEx)Marshal.PtrToStructure(pji,typeof(JoyInfoEx));
   }
   
   
 }
}
 

リンク集


MemoNyanDum : C# GDI+
http://junki.lix.jp/csgdip.html

コメントをどうぞ

  • コメント欄設置しました. -- yahirohumpty (2009-02-07 20:29:46)
名前:
コメント:
目安箱バナー