【发布时间】:2017-11-06 19:07:36
【问题描述】:
我想在每一帧上画一个正弦波。每一帧画完后角度都会变大,想要一个平滑的波浪移动效果,但是不知道怎么实现每一帧都画。
这是我的代码:
#include <windows.h>
#include <cmath>
#include <iostream>
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HWND hwnd;
float t = 0.0f;
HANDLE tickThreadHandle;
DWORD WINAPI tickThreadProc(HANDLE handle)
{
Sleep(50);
ShowWindow(hwnd, SW_SHOW);
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT r;
GetWindowRect(hwnd, &r);
HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
HPEN hOldPen = static_cast<HPEN>(SelectObject(hdc, hPen));
int delay = 1000 / 50;
while (true)
{
for (int x = 0; x<r.right; x += 5, t += 0.3f)
{
LineTo(hdc, x, sin(t) * 50 + 200);
}
t += 0.1f;
Sleep(delay);
}
SelectObject(hdc, hOldPen);
DeleteObject(hPen);
EndPaint(hwnd, &ps);
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
LPCTSTR lpszClassName = L"WNDCLASS";
WNDCLASSEX wcex;
ZeroMemory(&wcex, sizeof(wcex));
wcex.cbSize = sizeof(wcex);
wcex.hInstance = hInstance;
wcex.lpszClassName = lpszClassName;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.style = CS_DBLCLKS;
wcex.lpfnWndProc = WndProc;
wcex.hbrBackground = CreateSolidBrush(RGB(0, 0, 150));
if (!RegisterClassEx(&wcex))
{
return -1;
}
hwnd = CreateWindowEx(
NULL,
lpszClassName,
L"WINDOW",
WS_OVERLAPPEDWINDOW,
0,
0,
400,
400,
HWND_DESKTOP,
NULL,
hInstance,
NULL
);
if (hwnd == NULL)
{
return -2;
}
//ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
//InvalidateRect(hwnd, NULL, TRUE);
MSG msg;
//SendMessage(hwnd, WM_PAINT, NULL, NULL);
while (GetMessage(&msg, hwnd, 0, 0) > 0)
{
/*RedrawWindow(hwnd, &windowRect, NULL, RDW_INTERNALPAINT);*/
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
{
tickThreadHandle = CreateThread(NULL, NULL, &tickThreadProc, NULL, NULL, NULL);
}
case WM_DESTROY:
{
PostQuitMessage(NULL);
}
//case WM_PAINT:
//{
//}
default:
{
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
return 0;
}
我尝试了很多策略,在最后一次尝试中,我试图做另一个线程,并从那里绘制,但我的窗口甚至没有显示。我也试图向我的窗口发送消息WM_PAINT,或者使用RedrawWindow 函数,但这并没有给我任何帮助。我知道我可能会以错误的方式使用它,请纠正我并提示我可以做什么。
【问题讨论】:
-
我的意思是我想以大约每秒 50 帧的速度绘制这个正弦波,所以我想在一秒钟内显示 50 个动画步骤,正如你在这一行中看到的那样:
int delay = 1000 / 50; -
所以它不会在纯WinApi上工作?如果是,那你有什么推荐的?
标签: c++ c animation winapi rendering