我正在使用 win32 API,并且编写了一个简单的程序,每秒绘制 100 个随机像素十次。但是,当我编译并运行代码时,屏幕大约每秒刷新一次。我会对其进行调试,但我正在锁定的设备上工作,并且我的 IDE 没有调试功能。我已经更改了我能找到的所有引用计时器函数的变量,但没有任何效果。我正在使用 gcc 进行编译。这是代码:
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
const int ID_TIMER = 1;
void DrawRandom(HDC hdc);
//Double buffering implementation
static void Paint(HDC hdc, RECT* prc){
//Declares our varibles
HDC hdcMem;
HBITMAP hbmMem, hbmOld;
HBRUSH hbrBkGnd;
POINT Border;
//Creates creates compatible device context
hdcMem = CreateCompatibleDC(hdc);
//Create bitmap big enough for our display
hbmMem = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);
//Select the bitmap into the offscreen DC
hbmOld = (HBITMAP)SelectObject(hdcMem, hbmMem);
//Erase the background
hbrBkGnd = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
FillRect(hdcMem, prc, hbrBkGnd);
DeleteObject(hbrBkGnd);
//Gets the size of our border
Border.x = prc->right;
Border.y = prc->bottom;
//Draws random pixels
DrawRandom(hdcMem);
//BLT the changes to the screen dc
BitBlt(hdc, 0, 0, prc->right, prc->bottom, hdcMem, 0, 0, SRCCOPY);
//Memory manegment
SelectObject(hdcMem, hbmOld);
DeleteObject(hbmMem);
DeleteDC(hdcMem);
}
//Sets up our window
int WINAPI
WinMain (HINSTANCE hInstance, HINSTANCE hPrevInst, LPTSTR lpCmdLine, int nShowCmd) {
MSG msg;
WNDCLASSW wc = {0};
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpszClassName = L"Cube!";
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0, IDC_ARROW);
RegisterClassW(&wc);
CreateWindowW(wc.lpszClassName, L"Cube",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100, 100, 300, 250, NULL, NULL, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
//Calls our buffer and initilizes timer
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) {
switch(msg) {
//Initilizes timer
case WM_CREATE:
{
UINT ret;
ret = SetTimer(hwnd, ID_TIMER, 50, NULL);
}
break;
case WM_ERASEBKGND:
//Disables auto clear
return (LRESULT)1;
break;
//Makes sure the window closes
case WM_CLOSE:
DestroyWindow(hwnd);
break;
//This is what initilizes the contents of the window
case WM_PAINT:
{
RECT rcClient;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rcClient);
Paint(hdc, &rcClient);
EndPaint(hwnd, &ps);
}
break;
//This is where the main update loop is
case WM_TIMER:
{
RECT rcClient;
HDC hdc = GetDC(hwnd);
GetClientRect(hwnd, &rcClient);
Paint(hdc, &rcClient);
}
break;
//Kills the timer
case WM_DESTROY:
KillTimer(hwnd, ID_TIMER);
PostQuitMessage(0);
break;
//Make sures the program closes
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
void DrawRandom(HDC hdc){
srand(time(NULL));
for(int i = 0;i < 100;i++){
int x = rand() % 400;
int y = rand() % 400;
SetPixel(hdc, x, y, RGB(0,0,0));
}
}
Run Code Online (Sandbox Code Playgroud)
srand(time(NULL))为伪随机数生成器播种 播种rand() 意味着设置初始值。
由于您在函数内部调用,srand(time(NULL)) 这意味着任何时候调用的DrawRandom()后续调用都rand()将具有相同的值并返回相同的值。 为您提供秒分辨率,这就是为什么您看到更改每秒只发生一次。DrawRandom()time(NULL)time(NULL)