Merge pull request #1340 from oioitff/control-patch

Use native code instead of ATL::CImage
This commit is contained in:
Henrik Rydgård 2013-04-19 04:21:28 -07:00
commit 06999ae649

View file

@ -2,7 +2,6 @@
#include <windows.h> #include <windows.h>
#include <tchar.h> #include <tchar.h>
#include <atlimage.h>
#include "base/NativeApp.h" #include "base/NativeApp.h"
#include "Globals.h" #include "Globals.h"
@ -34,6 +33,7 @@
#include "Windows/W32Util/Misc.h" #include "Windows/W32Util/Misc.h"
#include "GPU/GPUInterface.h" #include "GPU/GPUInterface.h"
#include "GPU/GPUState.h" #include "GPU/GPUState.h"
#include "native/image/png_load.h"
#ifdef THEMES #ifdef THEMES
#include "XPTheme.h" #include "XPTheme.h"
@ -870,7 +870,7 @@ namespace MainWindow
return 1; return 1;
} }
BOOL LoadImageFromResource(CImage *image, HINSTANCE hInstance, LPCTSTR pszResourceName, LPCTSTR lpType) HBITMAP LoadImageFromResource(HINSTANCE hInstance,LPCTSTR pszResourceName, LPCTSTR lpType)
{ {
HRSRC hrsrc = FindResource(hInstance, pszResourceName, lpType); HRSRC hrsrc = FindResource(hInstance, pszResourceName, lpType);
if (!hrsrc) if (!hrsrc)
@ -879,31 +879,37 @@ namespace MainWindow
BYTE *lpRsrc = (BYTE*)LoadResource(hInstance, hrsrc); BYTE *lpRsrc = (BYTE*)LoadResource(hInstance, hrsrc);
if (!lpRsrc) if (!lpRsrc)
return FALSE; return FALSE;
HGLOBAL hMem = GlobalAlloc(GMEM_FIXED, dwlen); int width, height;
BYTE* pmem = (BYTE*)GlobalLock(hMem); unsigned char *image_data = 0;
memcpy(pmem, lpRsrc, dwlen); bool bResult = pngLoadPtr(lpRsrc, dwlen, &width, &height, &image_data, false);
GlobalUnlock(hMem);
IStream* pstm;
CreateStreamOnHGlobal(hMem, FALSE, &pstm);
BOOL bResult = (image->Load(pstm) == S_OK);
pstm->Release();
GlobalFree(hMem);
FreeResource(lpRsrc); FreeResource(lpRsrc);
return bResult; if (!bResult)
return 0;
HBITMAP hbm = CreateBitmap(width, height, 1, 32, image_data);
free(image_data);
return hbm;
}
void BitBlt(HBITMAP hbm, HDC dstDC, int dstX, int dstY, int width, int height, int srcX, int srcY)
{
HDC hCompDC = CreateCompatibleDC(dstDC);
HBITMAP oldbm = (HBITMAP)SelectObject(hCompDC, hbm);
BitBlt(dstDC, dstX, dstY, width, height, hCompDC, srcX, srcY, SRCCOPY);
SelectObject(hCompDC, oldbm);
DeleteObject(hCompDC);
} }
// Message handler for control box. // Message handler for control box.
LRESULT CALLBACK Controls(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) LRESULT CALLBACK Controls(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{ {
static CImage image; static HBITMAP hbm = 0;
switch (message) switch (message)
{ {
case WM_INITDIALOG: case WM_INITDIALOG:
W32Util::CenterWindow(hDlg); W32Util::CenterWindow(hDlg);
{ {
// TODO: connect to keyboard device instead // TODO: connect to keyboard device instead
if (image.IsNull()) if (!hbm)
LoadImageFromResource(&image, hInst, MAKEINTRESOURCE(IDB_IMAGE_PSP), "IMAGE"); hbm = LoadImageFromResource(hInst, MAKEINTRESOURCE(IDB_IMAGE_PSP), "IMAGE");
int key_pad_size = (IDC_EDIT_KEYRIGHT - IDC_EDIT_KEY_TURBO + 1); int key_pad_size = (IDC_EDIT_KEYRIGHT - IDC_EDIT_KEY_TURBO + 1);
for (u32 i = 0; i <= IDC_EDIT_KEY_ANALOG_RIGHT - IDC_EDIT_KEY_TURBO; i++) { for (u32 i = 0; i <= IDC_EDIT_KEY_ANALOG_RIGHT - IDC_EDIT_KEY_TURBO; i++) {
HWND hEdit = GetDlgItem(hDlg, IDC_EDIT_KEY_TURBO + i); HWND hEdit = GetDlgItem(hDlg, IDC_EDIT_KEY_TURBO + i);
@ -932,9 +938,11 @@ namespace MainWindow
{ {
PAINTSTRUCT pst; PAINTSTRUCT pst;
HDC hdc = BeginPaint(hDlg, &pst); HDC hdc = BeginPaint(hDlg, &pst);
int width = image.GetWidth(); BITMAP bm;
int height = image.GetHeight(); GetObject(hbm, sizeof(BITMAP), &bm);
image.BitBlt(hdc, 0, 0, width, height, 0 , 0); int width = bm.bmWidth;
int height = bm.bmHeight;
BitBlt(hbm, hdc, 0, 0, width, height, 0 , 0);
EndPaint(hDlg, &pst); EndPaint(hDlg, &pst);
return TRUE; return TRUE;
} }
@ -955,7 +963,7 @@ namespace MainWindow
RECT rc = getRedrawRect(hEdit); RECT rc = getRedrawRect(hEdit);
RECT clientrc; RECT clientrc;
GetClientRect(hEdit, &clientrc); GetClientRect(hEdit, &clientrc);
image.BitBlt(hdc, 0, 0, rc.right - rc.left, rc.bottom - rc.top, rc.left, rc.top); BitBlt(hbm, hdc, 0, 0, rc.right - rc.left, rc.bottom - rc.top, rc.left, rc.top);
char str[11]; char str[11];
GetWindowTextA(hEdit, str, 10); GetWindowTextA(hEdit, str, 10);
DrawTextA(hdc, str, strlen(str), &clientrc, DT_CENTER|DT_SINGLELINE); DrawTextA(hdc, str, strlen(str), &clientrc, DT_CENTER|DT_SINGLELINE);
@ -977,6 +985,10 @@ namespace MainWindow
} }
UnhookWindowsHookEx(pKeydownHook); UnhookWindowsHookEx(pKeydownHook);
EndDialog(hDlg, LOWORD(wParam)); EndDialog(hDlg, LOWORD(wParam));
if (hbm) {
DeleteObject(hbm);
hbm = 0;
}
return TRUE; return TRUE;
} }
break; break;