#include "stdafx.h" ULONG_PTR token; HIMAGELIST imagelist; INT imageindex = 0; /* Declare Windows Procedures */ LRESULT CALLBACK MainWindowProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK ImageButtonProc(HWND, UINT, WPARAM, LPARAM); #define MAIN_WINDOW_CLASS L"MainWindow" #define IMAGE_BUTTON_CLASS L"ImageButton" bool RegisterMainWindowClass() { WNDCLASS wc; memset(&wc, 0, sizeof(WNDCLASS)); wc.style = CS_DBLCLKS; wc.lpfnWndProc = (WNDPROC)MainWindowProc; wc.hInstance = NULL; wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszClassName = MAIN_WINDOW_CLASS; wc.lpszMenuName = NULL; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.cbWndExtra = NULL; if(!RegisterClass(&wc)) return FALSE; return TRUE; } bool RegisterImageButtonClass() { WNDCLASS wc; memset(&wc, 0, sizeof(WNDCLASS)); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)ImageButtonProc; wc.hInstance = NULL; wc.hbrBackground = NULL; wc.lpszClassName = IMAGE_BUTTON_CLASS; wc.lpszMenuName = NULL; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = NULL; wc.cbWndExtra = DLGWINDOWEXTRA; if(!RegisterClass(&wc)) return FALSE; return TRUE; } int _tmain(int argc, _TCHAR* argv[]) { using namespace Gdiplus; HWND hwnd; MSG messages; GdiplusStartupInput input; /* Register the MainWindow class */ if (!RegisterMainWindowClass()) { MessageBox(NULL, L"Unable to register MainWindow class!", L"Error", MB_OK | MB_ICONSTOP); return 0; } /* Register the ImageButton class */ if (!RegisterImageButtonClass()) { MessageBox(NULL, L"Unable to register ImageButton class!", L"Error", MB_OK | MB_ICONSTOP); return 0; } /* Initialize GDI+ */ GdiplusStartup(&token, &input, NULL); /* Create the main window */ hwnd = CreateWindow(MAIN_WINDOW_CLASS, L"Windows API Test", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, HWND_DESKTOP, NULL, NULL, NULL); /* Create the image button */ CreateWindow(IMAGE_BUTTON_CLASS, L"", WS_CHILD | WS_VISIBLE, 0, 0, 20, 20, hwnd, NULL, NULL, NULL); /* Load the image into the imagelist */ HBITMAP bitmap; Bitmap image(L"findnext.png"); image.GetHBITMAP(NULL, &bitmap); imagelist = ImageList_Create(20, 20, ILC_COLOR32, 4, 0); ImageList_Add(imagelist, bitmap, NULL); ShowWindow(hwnd, true); while(GetMessage(&messages, NULL, 0, 0)) { TranslateMessage(&messages); DispatchMessage(&messages); } return (INT)messages.wParam; } LRESULT CALLBACK ImageButtonProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { switch (message) { case WM_PAINT: { HDC hdc; PAINTSTRUCT ps; RECT rc; POINT pt; hdc = BeginPaint(hwnd, &ps); GetCursorPos(&pt); GetWindowRect(hwnd, &rc); if (PtInRect(&rc, pt)) { if(imageindex != 2) imageindex = 1; } else imageindex = 0; ImageList_Draw(imagelist, imageindex, hdc, 0, 0, ILD_NORMAL); EndPaint(hwnd, &ps); } return 0; case WM_LBUTTONDOWN: { imageindex = 2; // Pressed InvalidateRect(hwnd, NULL, TRUE); UpdateWindow(hwnd); // Force an update SetCapture(hwnd); } break; case WM_LBUTTONUP: { imageindex = 0; // Released InvalidateRect(hwnd, NULL, TRUE); ReleaseCapture(); } break; } return DefWindowProc(hwnd, message, wparam, lparam); } LRESULT CALLBACK MainWindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { switch (message) { case WM_DESTROY: { PostQuitMessage(0); } break; } return DefWindowProc(hwnd, message, wparam, lparam); }