📄 imagelist.c
字号:
/* Unit test suite for imagelist control.
*
* Copyright 2004 Michael Stefaniuc
* Copyright 2002 Mike McCormack for CodeWeavers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <assert.h>
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include "wine/test.h"
#undef VISIBLE
#ifdef VISIBLE
#define WAIT Sleep (1000)
#define REDRAW(hwnd) RedrawWindow (hwnd, NULL, 0, RDW_UPDATENOW)
#else
#define WAIT
#define REDRAW(hwnd)
#endif
static BOOL (WINAPI *pImageList_DrawIndirect)(IMAGELISTDRAWPARAMS*) = NULL;
static HDC desktopDC;
static HINSTANCE hinst;
/* These macros build cursor/bitmap data in 4x4 pixel blocks */
#define B(x,y) ((x?0xf0:0)|(y?0xf:0))
#define ROW1(a,b,c,d,e,f,g,h) B(a,b),B(c,d),B(e,f),B(g,h)
#define ROW32(a,b,c,d,e,f,g,h) ROW1(a,b,c,d,e,f,g,h), ROW1(a,b,c,d,e,f,g,h), \
ROW1(a,b,c,d,e,f,g,h), ROW1(a,b,c,d,e,f,g,h)
#define ROW2(a,b,c,d,e,f,g,h,i,j,k,l) ROW1(a,b,c,d,e,f,g,h),B(i,j),B(k,l)
#define ROW48(a,b,c,d,e,f,g,h,i,j,k,l) ROW2(a,b,c,d,e,f,g,h,i,j,k,l), \
ROW2(a,b,c,d,e,f,g,h,i,j,k,l), ROW2(a,b,c,d,e,f,g,h,i,j,k,l), \
ROW2(a,b,c,d,e,f,g,h,i,j,k,l)
static const BYTE empty_bits[48*48/8];
static const BYTE icon_bits[32*32/8] =
{
ROW32(0,0,0,0,0,0,0,0),
ROW32(0,0,1,1,1,1,0,0),
ROW32(0,1,1,1,1,1,1,0),
ROW32(0,1,1,0,0,1,1,0),
ROW32(0,1,1,0,0,1,1,0),
ROW32(0,1,1,1,1,1,1,0),
ROW32(0,0,1,1,1,1,0,0),
ROW32(0,0,0,0,0,0,0,0)
};
static const BYTE bitmap_bits[48*48/8] =
{
ROW48(0,0,0,0,0,0,0,0,0,0,0,0),
ROW48(0,1,1,1,1,1,1,1,1,1,1,0),
ROW48(0,1,1,0,0,0,0,0,0,1,1,0),
ROW48(0,1,0,0,0,0,0,0,1,0,1,0),
ROW48(0,1,0,0,0,0,0,1,0,0,1,0),
ROW48(0,1,0,0,0,0,1,0,0,0,1,0),
ROW48(0,1,0,0,0,1,0,0,0,0,1,0),
ROW48(0,1,0,0,1,0,0,0,0,0,1,0),
ROW48(0,1,0,1,0,0,0,0,0,0,1,0),
ROW48(0,1,1,0,0,0,0,0,0,1,1,0),
ROW48(0,1,1,1,1,1,1,1,1,1,1,0),
ROW48(0,0,0,0,0,0,0,0,0,0,0,0)
};
static HIMAGELIST createImageList(int cx, int cy)
{
/* Create an ImageList and put an image into it */
HIMAGELIST himl = ImageList_Create(cx, cy, ILC_COLOR, 1, 1);
HBITMAP hbm = CreateBitmap(48, 48, 1, 1, bitmap_bits);
ImageList_Add(himl, hbm, NULL);
return himl;
}
static HWND create_a_window(void)
{
char className[] = "bmwnd";
char winName[] = "Test Bitmap";
HWND hWnd;
static int registered = 0;
if (!registered)
{
WNDCLASSA cls;
cls.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
cls.lpfnWndProc = DefWindowProcA;
cls.cbClsExtra = 0;
cls.cbWndExtra = 0;
cls.hInstance = 0;
cls.hIcon = LoadIconA (0, (LPSTR)IDI_APPLICATION);
cls.hCursor = LoadCursorA (0, (LPSTR)IDC_ARROW);
cls.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
cls.lpszMenuName = 0;
cls.lpszClassName = className;
RegisterClassA (&cls);
registered = 1;
}
/* Setup window */
hWnd = CreateWindowA (className, winName,
WS_OVERLAPPEDWINDOW ,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
0, hinst, 0);
#ifdef VISIBLE
ShowWindow (hWnd, SW_SHOW);
#endif
REDRAW(hWnd);
WAIT;
return hWnd;
}
static HDC show_image(HWND hwnd, HIMAGELIST himl, int idx, int size,
LPCSTR loc, BOOL clear)
{
HDC hdc = NULL;
#ifdef VISIBLE
if (!himl) return NULL;
SetWindowText(hwnd, loc);
hdc = GetDC(hwnd);
ImageList_Draw(himl, idx, hdc, 0, 0, ILD_TRANSPARENT);
REDRAW(hwnd);
WAIT;
if (clear)
{
BitBlt(hdc, 0, 0, size, size, hdc, size+1, size+1, SRCCOPY);
ReleaseDC(hwnd, hdc);
hdc = NULL;
}
#endif /* VISIBLE */
return hdc;
}
/* Useful for checking differences */
#if 0
static void dump_bits(const BYTE *p, const BYTE *q, int size)
{
int i, j;
size /= 8;
for (i = 0; i < size * 2; i++)
{
printf("|");
for (j = 0; j < size; j++)
printf("%c%c", p[j] & 0xf0 ? 'X' : ' ', p[j] & 0xf ? 'X' : ' ');
printf(" -- ");
for (j = 0; j < size; j++)
printf("%c%c", q[j] & 0xf0 ? 'X' : ' ', q[j] & 0xf ? 'X' : ' ');
printf("|\n");
p += size * 4;
q += size * 4;
}
printf("\n");
}
#endif
static void check_bits(HWND hwnd, HIMAGELIST himl, int idx, int size,
const BYTE *checkbits, LPCSTR loc)
{
#ifdef VISIBLE
BYTE bits[100*100/8];
COLORREF c;
HDC hdc;
int x, y, i = -1;
if (!himl) return;
memset(bits, 0, sizeof(bits));
hdc = show_image(hwnd, himl, idx, size, loc, FALSE);
c = GetPixel(hdc, 0, 0);
for (y = 0; y < size; y ++)
{
for (x = 0; x < size; x++)
{
if (!(x & 0x7)) i++;
if (GetPixel(hdc, x, y) != c) bits[i] |= (0x80 >> (x & 0x7));
}
}
BitBlt(hdc, 0, 0, size, size, hdc, size+1, size+1, SRCCOPY);
ReleaseDC(hwnd, hdc);
ok (memcmp(bits, checkbits, (size * size)/8) == 0,
"%s: bits different\n", loc);
if (memcmp(bits, checkbits, (size * size)/8))
dump_bits(bits, checkbits, size);
#endif /* VISIBLE */
}
static void testHotspot (void)
{
struct hotspot {
int dx;
int dy;
};
#define SIZEX1 47
#define SIZEY1 31
#define SIZEX2 11
#define SIZEY2 17
#define HOTSPOTS_MAX 4 /* Number of entries in hotspots */
static const struct hotspot hotspots[HOTSPOTS_MAX] = {
{ 10, 7 },
{ SIZEX1, SIZEY1 },
{ -9, -8 },
{ -7, 35 }
};
int i, j, ret;
HIMAGELIST himl1 = createImageList(SIZEX1, SIZEY1);
HIMAGELIST himl2 = createImageList(SIZEX2, SIZEY2);
HWND hwnd = create_a_window();
for (i = 0; i < HOTSPOTS_MAX; i++) {
for (j = 0; j < HOTSPOTS_MAX; j++) {
int dx1 = hotspots[i].dx;
int dy1 = hotspots[i].dy;
int dx2 = hotspots[j].dx;
int dy2 = hotspots[j].dy;
int correctx, correcty, newx, newy;
char loc[256];
HIMAGELIST himlNew;
POINT ppt;
ret = ImageList_BeginDrag(himl1, 0, dx1, dy1);
ok(ret != 0, "BeginDrag failed for { %d, %d }\n", dx1, dy1);
sprintf(loc, "BeginDrag (%d,%d)\n", i, j);
show_image(hwnd, himl1, 0, max(SIZEX1, SIZEY1), loc, TRUE);
/* check merging the dragged image with a second image */
ret = ImageList_SetDragCursorImage(himl2, 0, dx2, dy2);
ok(ret != 0, "SetDragCursorImage failed for {%d, %d}{%d, %d}\n",
dx1, dy1, dx2, dy2);
sprintf(loc, "SetDragCursorImage (%d,%d)\n", i, j);
show_image(hwnd, himl2, 0, max(SIZEX2, SIZEY2), loc, TRUE);
/* check new hotspot, it should be the same like the old one */
himlNew = ImageList_GetDragImage(NULL, &ppt);
ok(ppt.x == dx1 && ppt.y == dy1,
"Expected drag hotspot [%d,%d] got [%ld,%ld]\n",
dx1, dy1, ppt.x, ppt.y);
/* check size of new dragged image */
ImageList_GetIconSize(himlNew, &newx, &newy);
correctx = max(SIZEX1, max(SIZEX2 + dx2, SIZEX1 - dx2));
correcty = max(SIZEY1, max(SIZEY2 + dy2, SIZEY1 - dy2));
ok(newx == correctx && newy == correcty,
"Expected drag image size [%d,%d] got [%d,%d]\n",
correctx, correcty, newx, newy);
sprintf(loc, "GetDragImage (%d,%d)\n", i, j);
show_image(hwnd, himlNew, 0, max(correctx, correcty), loc, TRUE);
ImageList_EndDrag();
}
}
#undef SIZEX1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -