我试图创建一个应用程序,使用以下代码在 10 秒内将手的小图片旋转 360 度:
#include <windows.h>
#include <tgmath.h>
void rotatebmp (HDC hdc_x, float q, float x0, float y0)
{
q = (q * 0.01745333055);
XFORM blah;
blah.eM11 = cos(q);
blah.eM12 = sin(q);
blah.eM21 = -sin(q);
blah.eM22 = cos(q);
blah.eDx = x0 - cos(q)*x0 + sin(q)*y0;
blah.eDy = y0 - cos(q)*y0 - sin(q)*x0;
SetWorldTransform(hdc_x, &blah);
return;
}
int main()
{
float q = 0;
HDC hdc = CreateCompatibleDC(NULL);
HBITMAP hand = (HBITMAP)LoadImage(NULL, ("C:\\Documents and Settings\\Death\\My Documents\\45Hand.bmp") ,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
HBITMAP handmask = (HBITMAP)LoadImage(NULL, ("C:\\Documents and Settings\\Death\\My Documents\\45Hand2.bmp") ,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
while (1)
{
q = (q + 3.6);
HDC hdc_x = GetDC(HWND_DESKTOP);
SetGraphicsMode(hdc_x, GM_ADVANCED);
SelectObject(hdc, handmask);
rotatebmp (hdc_x, q, 850, 375);
BitBlt(hdc_x,550,0,600,527,hdc,0,0, SRCAND);
ReleaseDC(HWND_DESKTOP,hdc_x);
hdc_x = GetDC(HWND_DESKTOP);
SetGraphicsMode(hdc_x, GM_ADVANCED);
SelectObject(hdc, hand);
rotatebmp (hdc_x, q, 850, 375);
BitBlt(hdc_x,550,0,600,527,hdc,0,0, SRCPAINT);
ReleaseDC(HWND_DESKTOP,hdc_x);
Sleep(100);
}
return 0;
}
我遇到的一个问题是执行缓慢,一次循环运行可能需要 0.06 秒Sleep(100)
,因为我对总数进行了计时,结果是 16 秒而不是预期的 10 秒。如何确定从循环开始运行到现在已经过去了多少时间?我认为唯一的方法是使用线程,在非多核处理器上,我认为这可能会使循环运行得更慢,因为它会分割资源,对吗?像 Sleep() 这样的命令如何跟踪时间?它检查的计算机上是否存在内部时钟?如果是这样,我如何访问该时钟?