わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

GDI+による塗りつぶし角丸四角形の描画

GDI+による描画は、Graphicsクラスにより提供されていますが、塗りつぶし 角丸四角形を描画する関数は提供されていません。このサンプルプログラムでは、 パスを生成して塗りつぶし角丸四角形の描画を実現しました。

#include <stdio.h>
#include <tchar.h>
#include <locale.h>
#include <windows.h>
#include <wingdi.h>
#include <gdiplus.h>



// ライブラリ
#pragma comment( lib, "gdiplus.lib" )



/*
	GDI+による塗りつぶし角丸四角形の描画
*/
void DrawFillRoundRect
(
	  Gdiplus::Graphics& oGraphics
	, Gdiplus::RectF* opRect
	, Gdiplus::Brush* opBrush
)
{
	Gdiplus::REAL angle  = 180;
	Gdiplus::REAL radius = 32;
	Gdiplus::REAL left   = opRect->X;
	Gdiplus::REAL top    = opRect->Y;
	Gdiplus::REAL right  = opRect->GetRight();
	Gdiplus::REAL bottom = opRect->GetBottom();

	// パスの生成
	Gdiplus::GraphicsPath oPath;

	// 左上
	oPath.AddArc(
			  left
			, top
			, (Gdiplus::REAL)radius, radius
			, angle
			, (Gdiplus::REAL)90
		);

	// 右上
	angle += 90;
	oPath.AddArc(
			  right - radius
			, top
			, (Gdiplus::REAL)radius
			, radius
			, angle
			, (Gdiplus::REAL)90
		);

	// 右下
	angle += 90;
	oPath.AddArc(
			  right - radius
			, bottom - radius
			, (Gdiplus::REAL)radius
			, radius
			, angle
			, (Gdiplus::REAL)90
		);

	// 左下
	angle += 90;
	oPath.AddArc(
			  left
			, bottom - radius
			, (Gdiplus::REAL)radius
			, radius
			, angle
			, (Gdiplus::REAL)90
		);

	// パスを閉じる
	oPath.CloseAllFigures();

	// パスの塗りつぶし
	oGraphics.FillPath( opBrush, &oPath );
}



/*
	メインウインドウイベント処理
*/
LRESULT CALLBACK eMainWindowProc
(
	  HWND   hWnd   // handle to window
	, UINT   uMsg   // message identifier
	, WPARAM wParam // first message parameter
	, LPARAM lParam // second message parameter
)
{
	switch( uMsg ) {
	case WM_CREATE:
	//--------------------------------------------
	// WM_CREATE
	//--------------------------------------------
		{
			CREATESTRUCT* tpCreateSt = (CREATESTRUCT*)lParam;

			// ウインドウを表示する
			::ShowWindow( hWnd, SW_SHOW );
		}
		break;


	case WM_DESTROY:
	//--------------------------------------------
	// WM_DESTROY
	//--------------------------------------------
		{
			// 終了する( 引数はそのまま終了コードとなります )
			::PostQuitMessage( 0 );
		}
		break;


	case WM_PAINT:
	//--------------------------------------------
	// WM_PAINT
	//--------------------------------------------
		{
			PAINTSTRUCT tPaintStruct;

			// 描画開始
			HDC hDC = ::BeginPaint( hWnd, &tPaintStruct );
			{
				Gdiplus::Graphics oGraphics( hDC );


				/*
					GDI+による塗りつぶし角丸四角形の描画
				*/
				{
					Gdiplus::SolidBrush oBrush( Gdiplus::Color( 255, 0, 0 ) );
					Gdiplus::RectF oRectF( 10, 10, 600, 420 );

					// GDI+による塗りつぶし角丸四角形の描画
					DrawFillRoundRect( oGraphics, &oRectF, &oBrush );
				}
			}

			// 描画終了
			::EndPaint( hWnd, &tPaintStruct );
		}
		return( FALSE );
	}

	// デフォルト処理呼び出し
	return ::DefWindowProc( hWnd, uMsg, wParam, lParam );
}



/*
	GDI+による塗りつぶし角丸四角形の描画
*/
int _tmain
(
	  int argc
	, _TCHAR* argv[]
)
{
	// 標準出力にユニコードを表示できるようにする
	setlocale( LC_ALL, "Japanese" );

	WNDCLASSEX tWndClass;
	HINSTANCE  hInstance;
	TCHAR*     cpClassName;
	TCHAR*     cpWindowName;
	TCHAR*     cpMenu;
	HWND       hWnd;
	MSG        tMsg;


	// GDI+の開始
	ULONG_PTR gdiplusToken;
	Gdiplus::GdiplusStartupInput gdiplusStartupInput;
	Gdiplus::GdiplusStartup( &gdiplusToken, &gdiplusStartupInput, 0 );

	// アプリケーションインスタンス
	hInstance    = ::GetModuleHandle( NULL );

	// クラス名称
	cpClassName  = _T("MainWindowClass");

	// メニュー
	cpMenu       = MAKEINTRESOURCE( NULL );

	// ウインドウ名称
	cpWindowName = _T("GDI+による塗りつぶし角丸四角形の描画");

	// ウインドウクラスパラメータセット
	tWndClass.cbSize        = sizeof( WNDCLASSEX );
	tWndClass.style         = CS_HREDRAW | CS_VREDRAW;
	tWndClass.lpfnWndProc   = eMainWindowProc;
	tWndClass.cbClsExtra    = 0;	// ::GetClassLong で取得可能なメモリ
	tWndClass.cbWndExtra    = 0;	// ::GetWindowLong で取得可能なメモリ
	tWndClass.hInstance     = hInstance; 
	tWndClass.hIcon         = ::LoadIcon( NULL, IDI_APPLICATION );
	tWndClass.hCursor       = ::LoadCursor( NULL, IDC_ARROW );
	tWndClass.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1 );
	tWndClass.lpszMenuName  = cpMenu;
	tWndClass.lpszClassName = cpClassName;
	tWndClass.hIconSm       = NULL;

	// ウインドウクラス生成
	if ( 0 == ::RegisterClassEx( &tWndClass ) ) {

		/* 失敗 */
		return( -1 );
	}

	// ウインドウを生成する
	hWnd = ::CreateWindowEx (
		  0                       // extended window style
		, tWndClass.lpszClassName // pointer to registered class name
		, cpWindowName            // pointer to window name
		, WS_OVERLAPPEDWINDOW     // window style
		, CW_USEDEFAULT           // horizontal position of window
		, CW_USEDEFAULT           // vertical position of window
		, 640                     // window width
		, 480                     // window height
		, NULL                    // handle to parent or owner window
		, NULL                    // handle to menu, or child-window identifier
		, hInstance               // handle to application instance
		, (VOID*)0x12345678       // pointer to window-creation data
	);


	/*
		メッセージループ
	*/
	while( 0 != ::GetMessage( &tMsg, NULL, 0, 0 ) ) {
		::TranslateMessage ( &tMsg );
		::DispatchMessage ( &tMsg );
	}

	// GDI+の終了
	Gdiplus::GdiplusShutdown( gdiplusToken );

	// WM_QUITの終了コードを返却する
	return( tMsg.wParam );
}



実行結果







わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。