Skip to content

Instantly share code, notes, and snippets.

@hirosof
Created July 30, 2014 19:13
Show Gist options
  • Save hirosof/2177918215f6c118e311 to your computer and use it in GitHub Desktop.
Save hirosof/2177918215f6c118e311 to your computer and use it in GitHub Desktop.
/*----------------------------------------------------------------------------
プログラム名:
バージョン:
プログラム概要:
開発言語:Microsoft Visual C++ 2012
統合開発環境:Microsoft Visual Studio 2012 Ultimate Edition (DreamSpark Premium)
開発開始日:
最終更新日:
-------------------------------------------------------------------------------
更新日一覧
--------------------------------------------------------------------------------
*/
//インクルード
#include <windows.h>
#include <d2d1.h>
#pragma comment(lib,"d2d1.lib")
//マニフェスト登録
#pragma comment(linker,"\"/manifestdependency:type='win32' " \
"name='Microsoft.Windows.Common-Controls' " \
"version='6.0.0.0' " \
"processorArchitecture='*' " \
"publicKeyToken='6595b64144ccf1df' " \
"language='*'\"")
//プロトタイプ
ATOM RegistWindowClass(HINSTANCE); /* ウィンドウクラスの登録 */
BOOL CreateMainWindow(HINSTANCE, int); /* ウィンドウの作成 */
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); /* ウィンドウプロシージャー */
void OnPaint(ID2D1HwndRenderTarget *pRender);
//マクロ部
#define PRONAME "D2DTest" //プログラム名
#define PROVER "1.0.1.0" //プログラムバージョン
#define PRONAME_TXT TEXT(PRONAME) //プログラム名(テキスト)
#define PROVER_TXT TEXT(PROVER) //プログラムバージョン(テキスト)
//定数部
const SIZE WindowClientSize = { 640, 480 };
const bool WindowSizeChangeByUserFlag = true;
const COLORREF WindowBackgroundColor = RGB(255, 255, 255);
//グローバル変数部
TCHAR szClassName[] = PRONAME_TXT; //ウィンドウクラス名
HINSTANCE hInstance = NULL;
int Angle = 0;
const D2D1_RENDER_TARGET_PROPERTIES Direct2DRenderProp = D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT,
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM,
D2D1_ALPHA_MODE_IGNORE),0,0,
D2D1_RENDER_TARGET_USAGE_NONE,
D2D1_FEATURE_LEVEL_DEFAULT);
//メイン関数
int WINAPI WinMain(HINSTANCE hCurInst, HINSTANCE hPrevInst, LPSTR lpsCmdLine, int nCmdShow) {
MSG msg;
BOOL bRet;
//COM初期化
CoInitialize(nullptr);
//インスタンスハンドルをグローバル変数にコピー
hInstance = hCurInst;
//ウィンドウクラスの登録
if (!RegistWindowClass(hCurInst)) return FALSE;
//ウィンドウ作成
if (!CreateMainWindow(hCurInst, nCmdShow)) return FALSE;
//メッセージループ
while ((bRet = GetMessage(&msg, NULL, 0, 0))) {
if (bRet == -1) {
MessageBox(NULL, TEXT("GetMessageエラー"), TEXT("Error"), MB_OK);
break;
}
else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//COM 解放
CoUninitialize();
return (int)msg.wParam;
}
//ウィンドウクラス登録関数
ATOM RegistWindowClass(HINSTANCE hInst) {
WNDCLASSEX wc; //WNDCLASSEX構造体
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = WndProc; //プロシージャ名
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst; //インスタンス
wc.hIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
wc.hbrBackground = CreateSolidBrush(WindowBackgroundColor);
wc.lpszMenuName = NULL; //メニュー名
wc.lpszClassName = szClassName;
wc.hIconSm = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
return RegisterClassEx(&wc);
}
//メインウィンドウを作る関数
BOOL CreateMainWindow(HINSTANCE hInst, int nCmdShow) {
HWND hwnd;
int WindowStyle;
//ウィンドウスタイル
WindowStyle = WS_OVERLAPPEDWINDOW;
//ウィンドウサイズ固定ならばオーバーラップウィンドウから
//サイズ変更境界と最大化ボタンを無効化にする
if (WindowSizeChangeByUserFlag == 0)
WindowStyle &= ~WS_THICKFRAME & ~WS_MAXIMIZEBOX;
//ウィンドウサイズを計算
RECT windowRect = { 0 };
windowRect.right = WindowClientSize.cx;
windowRect.bottom = WindowClientSize.cy;
if (AdjustWindowRectEx(&windowRect, WindowStyle, FALSE, NULL) == FALSE)return FALSE;
windowRect.right += abs(windowRect.left);
windowRect.bottom += abs(windowRect.top);
//ウィンドウ作成
hwnd =
CreateWindowEx(NULL,
szClassName,
//タイトルバーにこの文字列が表示されます
PRONAME_TXT,
WindowStyle,//ウィンドウの種類
CW_USEDEFAULT, //x座標
CW_USEDEFAULT,//y座標
windowRect.right, //幅
windowRect.bottom,//高さ
NULL,//親ウィンドウのハンドル、親を作るときはNULL
NULL,//メニューハンドル、クラスメニューを使うときはNULL
hInst,//インスタンスハンドル
NULL);
if (!hwnd)return FALSE;
//ウィンドウ表示
ShowWindow(hwnd, nCmdShow);
//ウィンドウ更新
UpdateWindow(hwnd);
return TRUE;
}
//ウィンドウプロシージャ
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
//クライアントサイズ保管場所
RECT rcClientSize;
//クライアントサイズ取得
GetClientRect(hwnd, &rcClientSize);
static ID2D1Factory* pD2Factory = NULL;
static ID2D1HwndRenderTarget *pD2Render = NULL;
HDC hdc;
PAINTSTRUCT ps;
HRESULT hr;
//メッセージごとの処理
switch (msg) {
case WM_CREATE:
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED , &pD2Factory);
if (FAILED(hr)) {
MessageBox(hwnd, TEXT("描画エンジン(Direct2D)の初期化に失敗しました"), PRONAME_TXT, MB_OK | MB_ICONERROR);
return -1;
} else {
D2D1_HWND_RENDER_TARGET_PROPERTIES prop;
prop.hwnd = hwnd;
prop.pixelSize = D2D1::SizeU(rcClientSize.right,rcClientSize.bottom);
prop.presentOptions = D2D1_PRESENT_OPTIONS_NONE;
hr = pD2Factory->CreateHwndRenderTarget(Direct2DRenderProp , prop,&pD2Render);
if (FAILED(hr)) {
MessageBox(hwnd, TEXT("描画エンジン(Direct2D)の初期化に失敗しました"), PRONAME_TXT, MB_OK | MB_ICONERROR);
return -1;
}
SetTimer(hwnd,100 , 1 , NULL);
}
break;
case WM_ERASEBKGND:
return FALSE;
case WM_PAINT:
hdc = BeginPaint(hwnd , &ps);
OnPaint(pD2Render);
EndPaint(hwnd ,&ps);
break;
case WM_SIZE:
if(pD2Render)pD2Render->Resize(D2D1::SizeU(rcClientSize.right,rcClientSize.bottom));
break;
case WM_TIMER:
if(wp == 100){
Angle = (Angle + 2) %360;
InvalidateRect(hwnd , NULL ,FALSE);
}
break;
case WM_DESTROY:
KillTimer(hwnd , 100);
if(pD2Render)pD2Render->Release();
if(pD2Factory)pD2Factory->Release();
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wp, lp);
}
return 0;
}
void OnPaint(ID2D1HwndRenderTarget *pRender){
pRender->BeginDraw();
pRender->Clear(D2D1::ColorF(D2D1::ColorF::White));
D2D1_SIZE_F size = pRender->GetSize();
D2D1_POINT_2F center = D2D1::Point2F(size.width/2,size.height/2);
pRender->SetTransform(D2D1::Matrix3x2F::Rotation(Angle * 1.0f,center));
ID2D1SolidColorBrush *pBrush;
pRender->CreateSolidColorBrush(D2D1::ColorF(0.0f,0.25f ,0.0f,1.0f) ,&pBrush);
pRender->FillRectangle(D2D1::RectF(center.x,center.y , center.x + 100 , center.y + 100), pBrush);
pBrush->SetColor(D2D1::ColorF(0.0f,0.25f ,1.0f,0.5f));
pRender->FillRectangle(D2D1::RectF(center.x - 50,center.y - 50 , center.x + 50 , center.y + 50) , pBrush);
pBrush->Release();
pRender->EndDraw();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment