#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASS wc;
HWND hwnd;
MSG msg;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND + 1;
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("LOADIMAGE");
RegisterClass(&wc);
hwnd = CreateWindow(TEXT("LOADIMAGE"),
TEXT("Windows API Primer"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
NULL, NULL, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc, hMdc;
PAINTSTRUCT ps;
HBITMAP hbmp;
switch (msg)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hbmp = (HBITMAP)LoadImage(NULL, "ferrari.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
hMdc = CreateCompatibleDC(hdc);
SelectObject(hMdc, hbmp);
BitBlt (hdc, 0, 0, 300, 200, hMdc, 0, 0, SRCCOPY);
DeleteDC (hMdc);
DeleteObject(hbmp);
EndPaint (hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
表示する画像のサイズを取得して、そのサイズに合わせてウィンドウの大きさを変えても良いかもしれません。
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASS wc;
HWND hwnd;
MSG msg;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND + 1;
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("IMAGESIZE");
RegisterClass(&wc);
hwnd = CreateWindow(TEXT("IMAGESIZE"),
TEXT("Windows API Primer"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
NULL, NULL, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HBITMAP hbmp;
static BITMAP bmp;
static HDC hMdc;
HDC hdc;
RECT wr, cr;
int width, height;
PAINTSTRUCT ps;
switch (msg)
{
case WM_CREATE:
hbmp = (HBITMAP)LoadImage(NULL, "ferrari_l.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
hdc = GetDC(hwnd);
hMdc = CreateCompatibleDC(hdc);
SelectObject(hMdc, hbmp);
GetObject (hbmp, sizeof(BITMAP), &bmp);
DeleteDC (hdc);
GetWindowRect(hwnd, &wr);
GetClientRect(hwnd, &cr);
width = (wr.right - wr.left) - (cr.right - cr.left) + bmp.bmWidth;
height = (wr.bottom - wr.top ) - (cr.bottom - cr.top ) + bmp.bmHeight;
SetWindowPos(hwnd, NULL, 0, 0, width, height, SWP_NOMOVE);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
BitBlt(hdc, 0, 0, bmp.bmWidth, bmp.bmHeight, hMdc, 0, 0, SRCCOPY);
EndPaint (hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
ウィンドウのサイズに合わせて、画像が拡大・縮小するのも面白いかもしれません。
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASS wc;
HWND hwnd;
MSG msg;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND + 1;
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("STRETCHIMAGE");
RegisterClass(&wc);
hwnd = CreateWindow(TEXT("STRETCHIMAGE"),
TEXT("Windows API Primer"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
NULL, NULL, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HBITMAP hbmp;
static HDC hMdc;
static BITMAP bmp;
HDC hdc;
RECT wr, cr;
int width, height;
PAINTSTRUCT ps;
switch (msg)
{
case WM_CREATE:
hbmp = (HBITMAP)LoadImage(NULL, "ferrari_s.bmp", IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE);
hdc = GetDC(hwnd);
hMdc = CreateCompatibleDC(hdc);
SelectObject(hMdc, hbmp);
GetObject (hbmp, sizeof(BITMAP), &bmp);
DeleteDC (hdc);
GetWindowRect(hwnd, &wr);
GetClientRect(hwnd, &cr);
width = (wr.right - wr.left) - cr.right + bmp.bmWidth;
height = (wr.bottom - wr.top ) - cr.bottom + bmp.bmHeight;
SetWindowPos(hwnd, NULL, 0, 0, width, height, SWP_NOMOVE);
break;
case WM_PAINT:
GetClientRect (hwnd, &cr);
hdc = BeginPaint(hwnd, &ps);
StretchBlt(hdc, 0, 0, cr.right, cr.bottom, hMdc, 0, 0,
bmp.bmWidth, bmp.bmHeight, SRCCOPY);
EndPaint (hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}