Customize a BitmapButton class

zhaozj2021-02-17  49

This article describes a production method of a homemade a BitmapButton class. This sub-defined BitmapButton class has a function similar to the MFC CBitmapButton class.

This BitmapButton is a bitmap that is in four bitmaps, general state, presses, focus status, and invalid status. The object of the BitmapButton class can accept messages to deliver messages to the main window. Imitrate most of the capabilities of the CBitmapButton class.

Here are the stated and realization of this class.

// filename: BitmapButton.h: Interface for the bitmapButton Class.// Envionment: Visual C 6.0, Windows 9x / 2k //// Author: kaitty // email: kaitty@x263.net or nsi10102 @ nsi // Date: 2002.2.10

// ******************************************************** ********************************** // Description: This is a custom BitmapButton class, the function is A Button is associated with four // bitmaps. @ Interface: HWND CreateBitmapButtonWindow (LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, // int x, int y, int nWidth, int nHeight, // HWND hwndParent, HMENU hmenu, HINSTANCE hInstance, // UINT upBitmap, UINT downBitmap, UINT Focusbitmap, uint disablebitmap; // Create a BitmapButton; // int setBitmap (HWND HWND, UINT UPTMAP, UINT DOWNBITMAP, UINT FOCUSBITMAP, UINT Disablebitmap); // Change the bitmap of the associated Button. // Bool EnableBitmapButton (HWND HWND, BOOL BENABLE); // makes Button valid or invalid. // int getWindowState (hwnd hwnd); // Get the Button status; the return value is one of 0, 1, 2, and 3. *********************************************************** ***************************************

#define wm_bitmap wm_user 999 ourselves to define messages to turn Button invalid status

struct PrivateInfo {HBITMAP hbitmap0; HBITMAP hbitmap1; HBITMAP hbitmap2; HBITMAP hbitmap3; int state;}; class BitmapButton {private: HWND m_hwnd; public // window handle of the object: virtual ~ BitmapButton (); BitmapButton (LPCTSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hwndParent, HMENU hmenu, hINSTANCE hInstance, UINT upBitmap, UINT downBitmap, UINT focusBitmap, UINT disableBitmap); // constructor BOOL EnableBitmapButton (HWND hwnd, BOOL bEnable) ; B 无 得 位 位 位 位 位 位 位 位 位 位 位 位 位 位 位 位 位 位 位 位 位 位 位

}; LRESULT CALLBACK ButtonProc (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM); // Window Function

*********************************************************** ***************************************** / / FileName: BitmapButton.cpp: Implementation File // Envionment: Visual C 6.0 Windows 9x / 2k //// Author: kaitty // email: kaitty@x263.net or nsi10102 @ nsi // Date: 2002.2.10 ***************** *********************************************************** ****************

#include "stdafx.h" #include "bitmapbutton.h" #include "resource.h"

///

BitmapButton :: ~ BitmapButton () {

}

/

BitmapButton :: BitmapButton (LPCTSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hwndParent, HMENU hmenu, HINSTANCE hInstance, UINT upBitmap, UINT downBitmap, UINT focusBitmap, UINT disableBitmap) {WNDCLASS wnd ; wnd.cbClsExtra = 0; wnd.cbWndExtra = 0; wnd.hbrBackground = NULL; wnd.hCursor = LoadCursor (NULL, IDC_ARROW); wnd.hIcon = NULL; wnd.hInstance = (HINSTANCE) GetWindowLong (hwndParent, GWL_HINSTANCE); wnd.lpfnWndProc = (WNDPROC) ButtonProc; wnd.lpszClassName = lpClassName; wnd.lpszMenuName = NULL; wnd.style = CS_HREDRAW | CS_VREDRAW; if (RegisterClass (& wnd)!) return; HWND hwnd; hwnd = CreateWindow (lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hwndParent, hmenu, hInstance, NULL); PrivateInfo * pPrivateInfo = new PrivateInfo; pPrivateInfo-> state = 0; pPrivateInfo-> hbitmap0 = LoadBitmap (hInstance, MAKEINTRESOURCE (upBitmap)); pPrivateInfo- > hbitmap1 = loadingbitmap (Hinstance, MakeintResource (DownBitmap); P PrivateInfo-> hbitmap2 = LoadBitmap (hInstance, MAKEINTRESOURCE (focusBitmap)); pPrivateInfo-> hbitmap3 = LoadBitmap (hInstance, MAKEINTRESOURCE (disableBitmap)); SetWindowLong (hwnd, GWL_USERDATA, (long) pPrivateInfo); ShowWindow (hwnd, SW_SHOW); UpdateWindow (hwnd); m_hwnd = hwnd;}

Lresult Callback ButtonProc (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM) {Int Xpos, Ypos; PrivateInfo * Pprivate; HRGN HRGN; HDC HDC, HDCMEM; PAINTSTRUCT PS; Rect Rect;

GetWindowRect (hWnd, & rect); hrgn = CreateRectRgn (0,0, rect.right-rect.left, rect.bottom-rect.top); pPrivate = (PrivateInfo *) GetWindowLong (hWnd, GWL_USERDATA); xPos = LOWORD (lParam ); YPOS = HiWord (LPARAM); Switch (Message) {CASE WM_CREATE: BREAK; Case WM_Command: Break; Case WM_Paint: Rect RT; HDC = Beginpaint (HWND, & PS); // Todo: Add Any Drawing Code Here .. . GetClientRect (hWnd, & rt); BITMAP bm; hdcmem = CreateCompatibleDC (hdc); switch (pPrivate-> state) {case 0: SelectObject (hdcmem, pPrivate-> hbitmap0); GetObject (pPrivate-> hbitmap0, sizeof (BITMAP) , & bm); break; case 1: SelectObject (hdcmem, pPrivate-> hbitmap1); GetObject (pPrivate-> hbitmap1, sizeof (BITMAP), & bm); break; case 2: SelectObject (hdcmem, pPrivate-> hbitmap2); GetObject (pPrivate-> hbitmap2, sizeof (BITMAP), & bm); break; case 3: SelectObject (hdcmem, pPrivate-> hbitmap3); GetObject (pPrivate-> hbitmap3, sizeof (BITMAP), & bm); break;} StretchBlt (hdc , 0, 0, Rect.right-Rect.Left, Rect.Bottom -rect.top, hdcmem, 0,0, bm.bmWidth, bm.bmHeight, SRCCOPY); DeleteDC (hdcmem); EndPaint (hWnd, & ps); break; case WM_DESTROY: DeleteObject (pPrivate-> hbitmap0); DeleteObject (pPrivate -> hbitmap1); DeleteObject (pPrivate-> hbitmap2); DeleteObject (pPrivate-> hbitmap3); DestroyWindow (hWnd); break; case WM_LBUTTONDOWN: if (pPrivate-> state = 3) {pPrivate-> state = 2; if! (! ())) Setcapture (hwnd); invalidateRect (hwnd, null, 0);}; break; case wm_lbuttonup: if (pprivate-> state! = 3) {ReleaseCapture (); pprivate-> STATE = 0;

IF (PtinRegion (HRGN, XPOS, YPOS)) SendMessage (GetParent (HWND), WM_COMMAND, GETWINDOWLON (HWND, GWL_ID), 0), 0);}; Break; Case WM_MouseMove: hcursor hcur; if (pprivate-> state! = 3) {if (! (GetCapture () == hWnd)) SetCapture (hWnd); hcur = LoadCursor ((HINSTANCE) GetWindowLong (GetParent (hWnd), GWL_HINSTANCE), MAKEINTRESOURCE (IDC_HAND)); SetCursor (hcur); if ((GetCapture () == hWnd) && (! PtinRegion (hrgn, xpos, ypos))) {Pprivate-> State = 0; ReleaseCapture ();} else pprivate-> state = 1; invalidateERECT (hwnd, null, 0);} Break; Case WM_bitmap: IF (wparam == 1) Pprivate-> State = 0; Else Pprivate-> State = 3; InvalidateRect (hwnd, null, 0); Break; Default: Return DefWindowProc (Hwnd, Message, WPARAM, LPARAM);} Return 0;} //

Bool BitmapButton :: EnableBitmapButton (HWND HWND, BOOL BENABLE) {SendMessage (hwnd, wm_bitmap, benable, 0); Return True;}

///

INT BitmapButton :: getWindowState (hwnd hwnd) {privateInfo * pprivate; pprivate = (privateInfo *) getWindowlong (hwnd, gwl_userdata); return pprivate-> state;}

//

转载请注明原文地址:https://www.9cbs.com/read-29368.html

New Post(0)