#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <vector>
#include <algorithm>
std::[[vector]]<unsigned short> g_array;
void OnPaint(HWND hWnd)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rc;
::GetClientRect(hWnd, &rc);
int nPts = static_cast<int>(g_array.size());
int iWinW = rc.right - rc.left;
int iWinH = rc.bottom - rc.top;
int iMaxY = *(std::max_element(g_array.begin(), g_array.end()));
double iGapX = iWinW / (nPts - 1);
double iGapY = iWinH / iMaxY;
int nCurX = 0;
hdc = BeginPaint(hWnd, &ps);
while(nCurX < nPts) {
int x = static_cast<int>(nCurX * iGapX);
int y = static_cast<int>(g_array.at(nCurX) * iGapY);
::LineTo(hdc, x, y);
nCurX++;
}
EndPaint(hWnd, &ps);
}
void OnSize(HWND hWnd)
{
::InvalidateRect(hWnd, NULL, TRUE);
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_PAINT:
OnPaint(hWnd);
break;
case WM_SIZE:
OnSize(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return(::DefWindowProc(hWnd, uMsg, wParam, lParam));
}
return (0L);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
int ar[10] = {5, 9, 4, 3, 8, 1, 1, 0, 9, 6};
g_array.assign(ar, ar + 10);
HWND hWnd;
MSG msg;
WNDCLASSEX wndClass;
const wchar_t* szClassName = L"Win32PaintSample";
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = (WNDPROC)WindowProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = NULL;
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = szClassName;
wndClass.hIconSm = NULL;
if(::RegisterClassEx(&wndClass) == 0) { return FALSE; }
hWnd = ::CreateWindowEx(WS_EX_APPWINDOW, szClassName, szClassName,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
::ShowWindow(hWnd, nCmdShow);
::UpdateWindow(hWnd);
while(::GetMessage(&msg, NULL, 0, 0)) {
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return (int)msg.wParam;
}
最終更新:2008年05月27日 00:45