コード:
#include "DxLib.h"
#include <Windows.h>
WNDPROC dxWndProc;
void ChangeCallback( void *Data ){
ShowWindow(GetMainWindowHandle(),3);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
RECT rc;
switch (msg)
{
case WM_SIZE:
switch (wp)
{
case SIZE_MAXIMIZED:
case SIZE_RESTORED:
GetWindowRect(hWnd, &rc);
SendMessage(hWnd, WM_SIZING, WMSZ_LEFT, (LPARAM)&rc); // WM_SIZINGを送ることで比率を変更させる
break;
}
break;
default:
return CallWindowProc((WNDPROC)dxWndProc, hWnd, msg, wp, lp);
}
return DefWindowProc(hWnd, msg, wp, lp);
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){
SetUseASyncChangeWindowModeFunction( TRUE, ChangeCallback, NULL );
ChangeWindowMode( TRUE ) ; // ウインドウモードに変更
if( DxLib_Init() == -1 ) return -1; // DXライブラリ初期化処理 エラーが起きたら終了
HWND hWnd = GetMainWindowHandle();
dxWndProc = (WNDPROC)GetWindowLong(hWnd, GWL_WNDPROC);
SetWindowLong(hWnd, GWL_WNDPROC, (LONG)WndProc);
WaitKey() ; // キーの入力待ち(『WaitKey』を使用)
DxLib_End() ; // DXライブラリ使用の終了処理
return 0 ; // ソフトの終了
}
これでしっかりとした出力になります。
ですが、クライアント領域のアスペクト比とDXライブラリの描画領域のアスペクト比が一致していない場合には画面に表示されないところが出てきしまいます。
(DXライブラリの本家で質問した方が詳しい返答もしくは解決策を頂けるのではないでしょうか?)
[hr][追記]
もっとしっかりとした出力を目指してみました。(恐らくこれが最善かと・・・)
コード:
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
RECT rcClient, rc;
static int flagSIZE = FALSE;
switch (msg)
{
case WM_SIZE:
switch (wp)
{
case SIZE_MAXIMIZED: // 最大化
case SIZE_RESTORED: // 通常のサイズ変更
GetClientRect(hWnd, &rcClient); // クライエント領域の大きさを取得
if (rcClient.right > rcClient.bottom) // 比率を合わせる
{
rcClient.right = rcClient.bottom / 3 * 4;
}
else
{
rcClient.bottom = rcClient.right / 3 * 4;
}
// ウィンドウ領域に合わせる(タイトルバー等も考慮した大きさに)
AdjustWindowRectEx(&rcClient, GetWindowLong(hWnd, GWL_STYLE), FALSE, GetWindowLong(hWnd, GWL_EXSTYLE));
SendMessage(hWnd, WM_SIZING, WMSZ_LEFT, (LPARAM)&rcClient); // ウィンドウの比率を合わせる(rcClientのleft, topともに0ですがこのDXライブラリにおいては問題はありません)
flagSIZE = TRUE;
break;
}
break;
case WM_PAINT:
if (flagSIZE) // サイズ変更が起きていたら
{
HDC hdc;
PAINTSTRUCT ps;
HBRUSH hBrush, hOldBrush;
hdc = BeginPaint(hWnd, &ps); // クライエント領域全体を黒く塗る
{
hBrush = CreateSolidBrush(RGB(0, 0, 0));
hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
GetClientRect(hWnd, &rc);
Rectangle(hdc, 0, 0, rc.right, rc.bottom);
SelectObject(hdc, hOldBrush);
DeleteObject(hBrush);
}
EndPaint(hWnd, &ps);
flagSIZE = FALSE;
}
return CallWindowProc((WNDPROC)dxWndProc, hWnd, msg, wp, lp);
default:
return CallWindowProc((WNDPROC)dxWndProc, hWnd, msg, wp, lp);
}
return (DefWindowProc(hWnd, msg, wp, lp));
}