//新手,代码可能有点乱,请谅解..
#define ThreadMutexLockDeleay 3500 //这里等待3.5秒绘制线程销毁. 如果设置最大值,就会直接进入最大值假死无限等待.
//启动绘制线程;
void WINAPI RunDrawD3D() {
DrawThread = TRUE;
CloseHandle(G_lpDrawThread);
G_lpDrawThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)DrawD3D, NULL, NULL, NULL);
if (!G_lpDrawThread) { MessageBox(NULL, L"绘制线程启动失败!", NULL, MB_OK); ExitProcess(NULL); }
}
//关闭绘制线程;
void WINAPI CloseDrawD3D() {
//通知绘制线程退出
DrawThread = FALSE;
//等待线程退出
DWORD WaitObject = WaitForSingleObject(G_lpDrawThread, ThreadMutexLockDeleay);
//如果等待线程超出时长,强制终止线程;
if (WaitObject != WAIT_OBJECT_0) {
DWORD ExitCode;
GetExitCodeThread(G_lpDrawThread, &ExitCode);
TerminateThread(G_lpDrawThread, ExitCode);
}
}
//删除当前选中图片;
void WINAPI DeleteImageInfo() {
if (ImageInfo.MaxImage <= 0)return;
CloseDrawD3D();
if (ImageInfo.Image[SelectIndex].Texture)
ImageInfo.Image[SelectIndex].Texture->Release();
for (UINT i = SelectIndex; i < ImageInfo.MaxImage - 1; i++) {
memcpy(&ImageInfo.Image[i], &ImageInfo.Image[i + 1], sizeof(ImageTexturInfo));
}
ImageInfo.MaxImage--;
if (ImageInfo.MaxImage <= 0) {
LocalFree(ImageInfo.Image);
ImageInfo = { 0 };
}
RunDrawD3D();
}
//这是绘制线程; 审视了好多遍了, 自认为没有什么地方会使绘制线程无限等待销毁的代码.....
void WINAPI DrawD3D() {
char Biit[50];
srand(GetTickCount());
sprintf(Biit, "线程成功创建!=-%d--\n", rand() % 1000);
OutputDebugStringA(Biit);
while (DrawThread) {
LPDIRECT3DDEVICE9 D3DDevice9 = D2Dx9.GetD3Devicex9();
if (!D3DDevice9)continue;
sprintf(Biit, "线程:%d", DrawThread);
SetWindowTextA(GetDlgItem(G_hWnd, IDC_CHECK_PlayAnimate), Biit);
D3DDevice9->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
if (SUCCEEDED(D3DDevice9->BeginScene())) {
UINT X = (ClRect.left - GlRect.left) - 10, Y = (ClRect.top - GlRect.top) - 32;
for (UINT i = 0; i < ImageInfo.MaxImage; i++) {
if(!ImageInfo.Image)break;
//判断是动画帧还是背景图片;
if (ImageInfo.Image[i].ImgLoadType == Image) {
if (MousePoint.x - X > ImageInfo.Image[i].x && MousePoint.x + X < ImageInfo.Image[i].x + ImageInfo.Image[i].Width
&& MousePoint.y - Y > ImageInfo.Image[i].y && MousePoint.y + Y < ImageInfo.Image[i].y + ImageInfo.Image[i].Height) {
if (!ISMove)CurrencyIndex = i;
}
}
//如果是动画帧
else if (ImageInfo.Image[i].ImgLoadType == Animate) {
PAnimateImage pAnimat = &ImageInfo.Image[i].Animate[ImageInfo.Image[i].AnimateTickIndex];
if (MousePoint.x - X > pAnimat->x && MousePoint.x - X < pAnimat->x + pAnimat->Width
&& MousePoint.y - Y > pAnimat->y && MousePoint.y - Y < pAnimat->y + pAnimat->Height) {
if (!ISMove)CurrencyIndex = i;
}
}
}
//判断是否在图片选区中
if (ImageInfo.MaxImage > CurrencyIndex) {
if (!ImageInfo.Image)continue;
if (ImageInfo.Image[CurrencyIndex].ImgLoadType == Image) {
if (MousePoint.x - X > ImageInfo.Image[CurrencyIndex].x && MousePoint.x - X < ImageInfo.Image[CurrencyIndex].x + ImageInfo.Image[CurrencyIndex].Width
&& MousePoint.y - Y > ImageInfo.Image[CurrencyIndex].y && MousePoint.y - Y < ImageInfo.Image[CurrencyIndex].y + ImageInfo.Image[CurrencyIndex].Height) {
if (MousePoint.KeyCode == MK_LBUTTON && MousePoint.KeyState == WM_LBUTTONDOWN) {
if (SelectIndex != CurrencyIndex) {
//是否高亮选中图片;
if (SelectIndex < ImageInfo.MaxImage) {
ImageInfo.Image[SelectIndex].RectAngleColor = D3DCOLOR_XRGB(255, 255, 255);
ImageInfo.Image[SelectIndex].HeightLight = FALSE;
}
ImageInfo.Image[CurrencyIndex].HeightLight = TRUE;
ImageInfo.Image[CurrencyIndex].RectAngleColor = D3DCOLOR_XRGB(255, 0, 0);
SelectIndex = CurrencyIndex;
IsTip = TRUE;//是否提示一次当前选中图片信息;
}
ISMove = TRUE;//是否选中一个图片
//记录鼠标选中是的坐标点
if (BOMouse.OldTick == 0xF841F || BOMouse.OldTick == 0) {
BOMouse.x = MousePoint.x - (int)ImageInfo.Image[CurrencyIndex].x;
BOMouse.y = MousePoint.y - (int)ImageInfo.Image[CurrencyIndex].y;
BOMouse.OldTick = 0x6F414;
}
//根据鼠标移动移动选中图片坐标x;
ImageInfo.Image[CurrencyIndex].x = (float)MousePoint.x - BOMouse.x;
//限制图片不超出绘制区
if (ImageInfo.Image[CurrencyIndex].x < 0)ImageInfo.Image[CurrencyIndex].x = 0;
else if (ImageInfo.Image[CurrencyIndex].x > (lRect.right - lRect.left) - ImageInfo.Image[CurrencyIndex].Width)
ImageInfo.Image[CurrencyIndex].x = (float)(lRect.right - lRect.left) - ImageInfo.Image[CurrencyIndex].Width;
//根据鼠标移动移动选中图片坐标y;
ImageInfo.Image[CurrencyIndex].y = (float)MousePoint.y - BOMouse.y;
//限制图片不超出绘制区
if (ImageInfo.Image[CurrencyIndex].y < 0)ImageInfo.Image[CurrencyIndex].y = 0;
else if (ImageInfo.Image[CurrencyIndex].y > (lRect.bottom - lRect.top) - ImageInfo.Image[CurrencyIndex].Height)
ImageInfo.Image[CurrencyIndex].y = (float)(lRect.bottom - lRect.top) - ImageInfo.Image[CurrencyIndex].Height;
}
if (MousePoint.KeyCode == MK_RBUTTON && MousePoint.KeyState == WM_RBUTTONDOWN) {
if (SelectIndex != CurrencyIndex) {
//是否高亮选中图片;
if (SelectIndex < ImageInfo.MaxImage) {
ImageInfo.Image[SelectIndex].RectAngleColor = D3DCOLOR_XRGB(255, 255, 255);
ImageInfo.Image[SelectIndex].HeightLight = FALSE;
}
ImageInfo.Image[CurrencyIndex].HeightLight = TRUE;
ImageInfo.Image[CurrencyIndex].RectAngleColor = D3DCOLOR_XRGB(255, 0, 0);
SelectIndex = CurrencyIndex;
IsTip = TRUE;//是否提示一次当前选中图片信息;
}
}
}
}
else if (ImageInfo.Image[CurrencyIndex].ImgLoadType == Animate) {
AnimateImage pAnimat = ImageInfo.Image[CurrencyIndex].Animate[ImageInfo.Image[CurrencyIndex].AnimateTickIndex];
if (MousePoint.x - X > pAnimat.x && MousePoint.x - X < pAnimat.x + pAnimat.Width
&& MousePoint.y - Y > pAnimat.y && MousePoint.y - Y < pAnimat.y + pAnimat.Height) {
if (MousePoint.KeyCode == MK_LBUTTON && MousePoint.KeyState == WM_LBUTTONDOWN) {
if (SelectIndex != CurrencyIndex|| SelectAnimateIndex != ImageInfo.Image[CurrencyIndex].AnimateTickIndex) {
//是否高亮选中图片;
if (SelectIndex < ImageInfo.MaxImage) {
ImageInfo.Image[SelectIndex].RectAngleColor = D3DCOLOR_XRGB(255, 255, 255);
ImageInfo.Image[SelectIndex].HeightLight = FALSE;
}
ImageInfo.Image[CurrencyIndex].HeightLight = TRUE;
ImageInfo.Image[CurrencyIndex].RectAngleColor = D3DCOLOR_XRGB(255, 0, 0);
SelectIndex = CurrencyIndex;
SelectAnimateIndex = ImageInfo.Image[CurrencyIndex].AnimateTickIndex;
IsTip = TRUE;//是否提示一次当前选中图片信息;
}
ISMove = TRUE;//是否选中一个图片
//记录鼠标选中是的坐标点
if (BOMouse.OldTick == 0xF841F || BOMouse.OldTick == 0) {
BOMouse.x = MousePoint.x - (int)pAnimat.x;
BOMouse.y = MousePoint.y - (int)pAnimat.y;
BOMouse.OldTick = 0x6F414;
}
//根据鼠标移动移动选中图片坐标x;
PAnimateImage pAnimat2 = &ImageInfo.Image[CurrencyIndex].Animate[ImageInfo.Image[CurrencyIndex].AnimateTickIndex];
pAnimat2->x = (float)MousePoint.x - BOMouse.x;
//限制图片不超出绘制区
if (pAnimat2->x < 0) {
pAnimat2->x = 0;
}
else if (pAnimat2->x > (lRect.right - lRect.left) - pAnimat2->Width)
pAnimat2->x = (float)(lRect.right - lRect.left) - pAnimat2->Width;
//根据鼠标移动移动选中图片坐标y;
pAnimat2->y = (float)MousePoint.y - BOMouse.y;
//限制图片不超出绘制区
if (pAnimat2->y < 0)pAnimat2->y = 0;
else if (pAnimat2->y > (lRect.bottom - lRect.top) - pAnimat2->Height)
pAnimat2->y = (float)(lRect.bottom - lRect.top) - pAnimat2->Height;
}
if (MousePoint.KeyCode == MK_RBUTTON && MousePoint.KeyState == WM_RBUTTONDOWN) {
if (SelectIndex != CurrencyIndex) {
//是否高亮选中图片;
if (SelectIndex < ImageInfo.MaxImage) {
ImageInfo.Image[SelectIndex].RectAngleColor = D3DCOLOR_XRGB(255, 255, 255);
ImageInfo.Image[SelectIndex].HeightLight = FALSE;
}
ImageInfo.Image[CurrencyIndex].HeightLight = TRUE;
ImageInfo.Image[CurrencyIndex].RectAngleColor = D3DCOLOR_XRGB(255, 0, 0);
SelectIndex = CurrencyIndex;
IsTip = TRUE;//是否提示一次当前选中图片信息;
}
}
if (MousePoint.KeyCode == MK_MBUTTON && MousePoint.KeyState == WM_MBUTTONDOWN) {
//记录鼠标选中是的坐标点
if (BOMouse.OldTick == 0xF841F || BOMouse.OldTick == 0) {
BOMouse.x = MousePoint.x - (int)pAnimat.x;
BOMouse.y = MousePoint.y - (int)pAnimat.y;
BOMouse.OldTick = 0x6F414;
}
for (UINT i = 0; i < ImageInfo.Image[CurrencyIndex].AnimateMaxCout; i++) {
//根据鼠标移动移动选中图片坐标x;
PAnimateImage pAnimat2 = &ImageInfo.Image[CurrencyIndex].Animate[i];
pAnimat2->x = (float)MousePoint.x - BOMouse.x;
//限制图片不超出绘制区
if (pAnimat2->x < 0) {
pAnimat2->x = 0;
}
else if (pAnimat2->x > (lRect.right - lRect.left) - pAnimat2->Width)
pAnimat2->x = (float)(lRect.right - lRect.left) - pAnimat2->Width;
//根据鼠标移动移动选中图片坐标y;
pAnimat2->y = (float)MousePoint.y - BOMouse.y;
//限制图片不超出绘制区
if (pAnimat2->y < 0)pAnimat2->y = 0;
else if (pAnimat2->y > (lRect.bottom - lRect.top) - pAnimat2->Height)
pAnimat2->y = (float)(lRect.bottom - lRect.top) - pAnimat2->Height;
}
}
}
}
}
//判断是否释放选中图片
if (MousePoint.KeyState == WM_LBUTTONUP || MousePoint.KeyState == WM_RBUTTONUP|| MousePoint.KeyState == WM_MBUTTONUP) {
ISMove = FALSE;
BOMouse.x = 0;
BOMouse.y = 0;
BOMouse.OldTick = 0xF841F;
}
//绘制所有图片
for (UINT i = 0; i < ImageInfo.MaxImage; i++) {
if (ImageInfo.Image != nullptr) {
if (ImageInfo.Image[i].ImgLoadType == Image) {
//选中图片是否高亮显示
if (SendMessage(GetDlgItem(G_hWnd, IDC_CHECK_HeightLight), BM_GETCHECK, 0, 0) == BST_CHECKED) {
if (ImageInfo.Image[i].HeightLight)
D2Dx9.DrawTexture(ImageInfo.Image[i].Texture, ImageInfo.Image[i].x, ImageInfo.Image[i].y, ImageInfo.Image[i].Width, ImageInfo.Image[i].Height, ImageInfo.Image[i].Scale, 0.0f, D3DCOLOR_XRGB(255, 0, 0));
else D2Dx9.DrawTexture(ImageInfo.Image[i].Texture, ImageInfo.Image[i].x, ImageInfo.Image[i].y, ImageInfo.Image[i].Width, ImageInfo.Image[i].Height, ImageInfo.Image[i].Scale);
}
else D2Dx9.DrawTexture(ImageInfo.Image[i].Texture, ImageInfo.Image[i].x, ImageInfo.Image[i].y, ImageInfo.Image[i].Width, ImageInfo.Image[i].Height, ImageInfo.Image[i].Scale);
//选中图片边框是否显示
if (SendMessage(GetDlgItem(G_hWnd, IDC_CHECK_IsRectangle), BM_GETCHECK, 0, 0) == BST_CHECKED)
D2Dx9.DrawRectagle(ImageInfo.Image[i].x, ImageInfo.Image[i].y, ImageInfo.Image[i].x + ImageInfo.Image[i].Width, ImageInfo.Image[i].y + ImageInfo.Image[i].Height,
1.0f, ImageInfo.Image[i].RectAngleColor);
}
else if (ImageInfo.Image[i].ImgLoadType == Animate) {
if (SendMessage(GetDlgItem(G_hWnd, IDC_CHECK_PlayAnimate), BM_GETCHECK, 0, 0) == BST_CHECKED) {
if (GetTickCount() - ImageInfo.Image[i].OldAnimateTick > ImageInfo.Image[i].AnimateDelay) {
ImageInfo.Image[i].OldAnimateTick = GetTickCount();
ImageInfo.Image[i].AnimateTickIndex++;
}
}
if (ImageInfo.Image[i].AnimateMaxCout <= ImageInfo.Image[i].AnimateTickIndex) {
ImageInfo.Image[i].AnimateTickIndex = 0;
}
AnimateImage pAnimat = ImageInfo.Image[i].Animate[ImageInfo.Image[i].AnimateTickIndex];
//选中图片是否高亮显示
if (SendMessage(GetDlgItem(G_hWnd, IDC_CHECK_HeightLight), BM_GETCHECK, 0, 0) == BST_CHECKED) {
if (ImageInfo.Image[i].HeightLight) {
D2Dx9.DrawTexture(pAnimat.Texture, pAnimat.x, pAnimat.y, pAnimat.Width, pAnimat.Height, pAnimat.Scale, 0.0f, D3DCOLOR_XRGB(255, 0, 0));
}
else D2Dx9.DrawTexture(pAnimat.Texture, pAnimat.x, pAnimat.y, pAnimat.Width, pAnimat.Height, pAnimat.Scale);
}
else D2Dx9.DrawTexture(pAnimat.Texture, pAnimat.x, pAnimat.y, pAnimat.Width, pAnimat.Height, pAnimat.Scale);
//选中图片边框是否显示
if (SendMessage(GetDlgItem(G_hWnd, IDC_CHECK_IsRectangle), BM_GETCHECK, 0, 0) == BST_CHECKED)
D2Dx9.DrawRectagle(pAnimat.x, pAnimat.y, pAnimat.x + pAnimat.Width, pAnimat.y + pAnimat.Height, 1.0f, ImageInfo.Image[i].RectAngleColor);
}
}
}
//显示绘制帧数;
if (OldFpsTick == 0 || GetTickCount() - OldFpsTick > 1100) {
OldFpsTick = GetTickCount();
NewFPSCount = FPSCount;
FPSCount = 0;
}
else FPSCount++;
char FPSOut[50];
sprintf(FPSOut, "FPS:%d,X:%dY:%d", NewFPSCount, MousePoint.x, MousePoint.y);
D2Dx9.DrawFont(FPSOut, 12, NULL, FALSE, "隶书", &lRect, D3DCOLOR_XRGB(255, 0, 0));
D3DDevice9->EndScene();
D3DDevice9->Present(NULL, NULL, NULL, NULL);
if (IsTip) {
char ImgInfo[256] = { 0 };
if (ImageInfo.Image[SelectIndex].ImgLoadType == Image) {
sprintf(ImgInfo, "图片X:%.2f\t\t图片Y:%.2f\r\n\r\n图片高:%d\t\t图片宽:%d\r\n\r\n图片索引:%d\t\t图片总数:%d\r\n", ImageInfo.Image[SelectIndex].x, ImageInfo.Image[SelectIndex].y,
ImageInfo.Image[SelectIndex].Width, ImageInfo.Image[SelectIndex].Height, CurrencyIndex, ImageInfo.MaxImage);
SetWindowTextA(GetDlgItem(G_hWnd, IDC_Label_Tip), ImgInfo);
HWND EditHwnd = GetDlgItem(G_hWnd, IDC_EDIT_Path);
SetWindowText(EditHwnd, ImageInfo.Image[SelectIndex].ImageFile);
UINT Strlen = wcslen(ImageInfo.Image[SelectIndex].ImageFile);
SendMessage(EditHwnd, EM_SETSEL, Strlen, Strlen);
sprintf(ImgInfo, "%.2f", ImageInfo.Image[SelectIndex].Scale);
SetWindowTextA(GetDlgItem(G_hWnd, IDC_EDIT_Scale), ImgInfo);
SendMessage(GetDlgItem(G_hWnd, IDC_COMBO_LoadStyle), CB_SETCURSEL, 1, NULL);
}
else if (ImageInfo.Image[SelectIndex].ImgLoadType == Animate) {
AnimateImage pAnimat = ImageInfo.Image[SelectIndex].Animate[ImageInfo.Image[SelectIndex].AnimateTickIndex];
sprintf(ImgInfo, "图片X:%.2f\t\t图片Y:%.2f\r\n\r\n图片高:%d\t\t图片宽:%d\r\n\r\n动画索引:%d\t\t动画总帧:%d\r\n\r\n动画帧时:%dms/ts\t", pAnimat.x, pAnimat.y,
pAnimat.Width, pAnimat.Height, ImageInfo.Image[SelectIndex].AnimateTickIndex, ImageInfo.Image[SelectIndex].AnimateMaxCout, ImageInfo.Image[SelectIndex].AnimateDelay);
SetWindowTextA(GetDlgItem(G_hWnd, IDC_Label_Tip), ImgInfo);
HWND EditHwnd = GetDlgItem(G_hWnd, IDC_EDIT_Path);
SetWindowText(EditHwnd, pAnimat.ImageFile);
UINT Strlen = wcslen(pAnimat.ImageFile);
SendMessage(EditHwnd, EM_SETSEL, Strlen, Strlen);
sprintf(ImgInfo, "%.2f", pAnimat.Scale);
SetWindowTextA(GetDlgItem(G_hWnd, IDC_EDIT_Scale), ImgInfo);
SendMessage(GetDlgItem(G_hWnd, IDC_COMBO_LoadStyle), CB_SETCURSEL, 0, NULL);
}
/*BYTE *Rgb = (BYTE*)&ImageInfo.Image[SelectIndex].RectAngleColor;
sprintf(ImgInfo, "%d", Rgb[2]);
SetWindowTextA(GetDlgItem(G_hWnd, IDC_EDIT_ColorR), ImgInfo);
sprintf(ImgInfo, "%d", Rgb[1]);
SetWindowTextA(GetDlgItem(G_hWnd, IDC_EDIT_ColorG), ImgInfo);
sprintf(ImgInfo, "%d", Rgb[0]);
SetWindowTextA(GetDlgItem(G_hWnd, IDC_EDIT_ColorB), ImgInfo);*/
IsTip = FALSE;
}
}
else D3DDevice9->EndScene(); //因为有时会因为上一次开始绘制没有执行结束绘制就被强制销毁了所以加上了这句代码
Sleep(5);
}
sprintf(Biit, "线程成功退出!=-%d--\n", rand() % 1000);
OutputDebugStringA(Biit);
}