win32简单的动态创建的图片按钮,加载两幅图片,单击的时候替换。 #pragma once class ImageButton { public: ImageButton() : m_hwnd(0) , m_status(true) { } ~ImageButton() { if (m_hwnd) { ::DestroyWindow(m_hwnd); } } HWND Create(HWND parent, unsigned int id) { m_parent = parent; m_id = id; m_hwnd = ::CreateWindow(_T("BUTTON"), _T(""), WS_CHILD | WS_VISIBLE | BS_BITMAP, 0, 0, 0, 0, parent, (HMENU)id, (HINSTANCE)GetWindowLong(parent, GWL_HINSTANCE), NULL); return m_hwnd; } void LoadBitmapFromID(unsigned int id1, unsigned int id2) { HINSTANCE hInstance = (HINSTANCE)::GetWindowLong(m_parent, GWL_HINSTANCE); m_bmp1 = LoadBitmap(hInstance, MAKEINTRESOURCE(id1)); m_bmp2 = LoadBitmap(hInstance, MAKEINTRESOURCE(id2)); } void Show(RECT rc) { ::ShowWindow(m_hwnd, SW_SHOW); ::SendMessage(m_hwnd, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)m_bmp1); MoveWindow(m_hwnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, TRUE); } void OnClicked() { if (m_status) { ::SendMessage(m_hwnd, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)m_bmp2); m_status = false; } else { ::SendMessage(m_hwnd, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)m_bmp1); m_status = true; } } private: HWND m_hwnd; HWND m_parent; unsigned int m_id; HBITMAP m_bmp1; HBITMAP m_bmp2; bool m_status; };