📄 grh.h
字号:
#pragma once
#include <string>
#include <windows.h>
#include "RPos.h"
#include <iostream>
class Grh
{
/*
int frame;
DWORD frmstep;
DWORD oldtime;
*/
public:
Grh(void);
virtual ~Grh(void);
Grh(HWND hwnd, const std::string& rname, const std::string& sname, DWORD nfrmstep)
: hdc(0), dcback(0), dcrole(0), dcrolemask(0), dcscene(0),sw(0),
sh(0), rw(0), rh(0), frame(0), oldtime(timeGetTime()), frmstep(nfrmstep)
{
InitializeCriticalSection(&cs);
if(!hwnd)
{
return;
}
hdc = GetDC(hwnd);
{
RECT rect = {0};
GetClientRect(hwnd, &rect);
HBITMAP hbmp = CreateBitmap(rect.right - rect.left, rect.bottom - rect.top,
1, 32, 0);
dcback = CreateCompatibleDC(hdc);
HBITMAP hobmp = (HBITMAP)SelectObject(dcback, hbmp);
DeleteObject(hobmp);
}
{
dcrole = CreateCompatibleDC(hdc);
HBITMAP hbmp = (HBITMAP)LoadImage(0, rname.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
HBITMAP hobmp = (HBITMAP)SelectObject(dcrole, hbmp);
DeleteObject(hobmp);
SetBkColor(dcrole, 0x0000ff00);
dcrolemask = CreateCompatibleDC(hdc);
BITMAP bmp = {0};
GetObject(hbmp, sizeof(bmp), &bmp);
HBITMAP hbmpm = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, 0);
rw = bmp.bmWidth / 2;
rh = bmp.bmHeight / 4;
HBITMAP hobmpm = (HBITMAP)SelectObject(dcrolemask, hbmpm);
DeleteObject(hobmpm);
BitBlt(dcrolemask, 0, 0, bmp.bmWidth, bmp.bmHeight, dcrole, 0, 0, SRCCOPY);
}
{
dcscene = CreateCompatibleDC(hdc);
HBITMAP hbmp = (HBITMAP)LoadImage(0, sname.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
HBITMAP hobmp = (HBITMAP)SelectObject(dcscene, hbmp);
BITMAP bmp = {0};
GetObject(hbmp, sizeof(bmp), &bmp);
sw = bmp.bmWidth;
sh = bmp.bmHeight;
DeleteObject(hobmp);
}
};
Grh(const Grh& grh)
: hdc(grh.hdc), dcback(grh.dcback), dcrole(grh.dcrole), dcrolemask(grh.dcrolemask),
dcscene(grh.dcscene),sw(grh.sw), sh(grh.sh), rw(grh.rw), rh(grh.rh),
frame(grh.frame), oldtime(grh.oldtime), frmstep(grh.frmstep)
{
}
void draw(void)
{
if(queue.empty())
{
BitBlt(hdc, 0, 0, sw, sh, dcback, 0, 0, SRCCOPY);
return;
}
int x = queue.front().x;
int y = queue.front().y;
int dir = queue.front().dir;
EnterCriticalSection(&cs);
queue.pop_front();
LeaveCriticalSection(&cs);
DWORD nowtm = timeGetTime();
if(nowtm - oldtime >= frmstep)
{
oldtime = nowtm;
frame = (frame + 1) % 2;
}
BitBlt(dcback, 0, 0, sw, sh, dcscene, 0, 0, SRCCOPY);
BitBlt(dcback, x - rw / 2, y - rh / 2, rw, rh, dcrole, frame * 32, dir * 32, SRCINVERT);
BitBlt(dcback, x - rw / 2, y - rh / 2, rw, rh, dcrolemask, frame * 32, dir * 32, SRCAND);
BitBlt(dcback, x - rw / 2, y - rh / 2, rw, rh, dcrole, frame * 32, dir * 32, SRCINVERT);
BitBlt(hdc, 0, 0, sw, sh, dcback, 0, 0, SRCCOPY);
//std::cout << "Draw" << std::endl;
}
void addPos(const RPosQueue& posq)
{
EnterCriticalSection(&cs);
queue.insert(queue.end(), posq.begin(), posq.end());
LeaveCriticalSection(&cs);
}
static DWORD __stdcall run(void* lp)
{
if(!lp)
{
return 1;
}
Grh* pgrh = (Grh*)lp;
for(;Grh::brun;)
{
//std::cout << "Grh" <<std::endl;
pgrh->draw();
Sleep(33);
}
return 0;
}
static bool brun;
int sw;
int sh;
int rw;
int rh;
HDC hdc;
HDC dcback;
HDC dcrole;
HDC dcrolemask;
HDC dcscene;
RPosQueue queue;
CRITICAL_SECTION cs;
int frame;
DWORD frmstep;
DWORD oldtime;
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -