1. プロジェクトのプロパティからビルドの設定を変更。
① ビルド項目を開き[構成]を[全ての構成]にする。
② [条件付きコンパイル シンボル]に[DX_USE_UNSAFE]を追加する。
③ [アンセーフコードの許可]にチェックする。
2. 以下のように動的にBitmapを作成して、CreateGraphFromMemでグラフィックを作成。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// カドがまるまった四角形のGraphicPathを作成 | |
/// </summary> | |
/// <param name="left"></param> | |
/// <param name="top"></param> | |
/// <param name="width"></param> | |
/// <param name="height"></param> | |
/// <param name="arc"></param> | |
/// <returns></returns> | |
private GraphicsPath CreatePath(int left, int top, int width, int height, int arc) | |
{ | |
int right = left + width; | |
int bottom = top + height; | |
GraphicsPath path = new GraphicsPath(); | |
path.StartFigure(); | |
path.AddArc(right - arc, top, arc, arc, 270, 90); // 右上 | |
path.AddArc(right - arc, bottom - arc, arc, arc, 0, 90); // 右下 | |
path.AddArc(left, bottom - arc, arc, arc, 90, 90); // 左下 | |
path.AddArc(left, top, arc, arc, 180, 90); // 左上 | |
path.CloseFigure(); | |
return path; | |
} | |
/// <summary> | |
/// 動的にウィンドウグラフィックを作成する | |
/// </summary> | |
/// <returns></returns> | |
private int CreateSampleWindow() | |
{ | |
int handle = -1; | |
int arc = 8; | |
int width = 400; | |
int height = 300; | |
// 黒は通過色なので擬似黒をつかう | |
Pen blackPen = new Pen(Color.FromArgb(1, 1, 1)); | |
using (Bitmap bmp = new Bitmap(width, height,PixelFormat.Format24bppRgb)) | |
{ | |
// 動的にBitmapを作成 | |
using (Graphics g = Graphics.FromImage(bmp)) | |
{ | |
g.SmoothingMode = SmoothingMode.HighQuality; | |
var path = CreatePath(0, 0, width - 1, height - 1, arc); | |
var brush = new LinearGradientBrush(new Point(0, 0), new Point(width, height), Color.White, Color.LightGray); | |
g.Clear(Color.Transparent); | |
g.FillPath(brush, path); | |
g.DrawPath(blackPen, path); | |
path = CreatePath(2, 2, width - 5, height - 5, arc); | |
brush = new LinearGradientBrush(new Point(0, 0), new Point(width, width), Color.DarkBlue, Color.Black); | |
g.FillPath(brush, path); | |
g.DrawPath(blackPen, path); | |
} | |
// 動的に作成したBitmapからグラフィックを作成 | |
using (MemoryStream ms = new MemoryStream()) | |
{ | |
bmp.Save(ms, ImageFormat.Bmp); | |
byte[] buff = ms.ToArray(); | |
unsafe | |
{ | |
fixed (byte* p = buff) | |
{ | |
DX.SetDrawValidGraphCreateFlag(DX.TRUE); | |
DX.SetDrawValidAlphaChannelGraphCreateFlag(DX.TRUE); | |
handle = DX.CreateGraphFromMem(p, buff.Length); | |
DX.SetDrawValidGraphCreateFlag(DX.FALSE); | |
DX.SetDrawValidAlphaChannelGraphCreateFlag(DX.FALSE); | |
} | |
} | |
} | |
} | |
return handle; | |
} |
サンプルプログラムをダウンロード
このコメントは投稿者によって削除されました。
返信削除