📄 button.c
字号:
/*** $Id: button.c,v 1.56 2003/09/29 02:52:46 snig Exp $**** button.c: the Button Control module.**** Copyright (C) 2003 Feynman Software.** Copyright (C) 1999 ~ 2002 Wei Yongming.**** Current maintainer: Wei Yongming.**** Note:** Originally by Zhao Jianghua.**** Create date: 1999/8/23*//*** This program is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.**** This program 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 General Public License for more details.**** You should have received a copy of the GNU General Public License** along with this program; if not, write to the Free Software** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*//*** Modify records:**** Who When Where For What Status**-----------------------------------------------------------------------------** WEI Yongming 1999/10/27 Tsinghua Notify Message Fininshed** WEI Yongming 2000/02/24 Tsinghua Add MPL License Finished**** TODO:*/ #include <stdio.h>#include <stdlib.h>#include <string.h>#include "common.h"#include "minigui.h"#include "gdi.h"#include "window.h"#include "control.h"#include "cliprect.h"#include "internals.h"#include "ctrlclass.h"#ifdef _CTRL_BUTTON#include "button.h"#include "ctrlmisc.h"#ifdef _FLAT_WINDOW_STYLE#define BTN_WIDTH_BORDER 1#ifndef _GRAY_SCREEN#define DrawFlatControlFrameNC(hdc, x0, y0, x1, y1, fillc, updown) \ DrawFlatControlFrameEx(hdc, x0, y0, x1, y1, fillc, 0, updown);#define Draw3DControlFrame DrawFlatControlFrameNC#else#define Draw3DControlFrame DrawFlatControlFrame#endif#define FocusRect(hdc, x, y, w, h)#else#define BTN_WIDTH_BORDER 4#endif#define BTN_WIDTH_BMP 14#define BTN_HEIGHT_BMP 13#define BTN_INTER_BMPTEXT 2#define BUTTON_STATUS(pCtrl) (((PBUTTONDATA)(pCtrl->dwAddData2))->status)#define BUTTON_DATA(pCtrl) (((PBUTTONDATA)(pCtrl->dwAddData2))->data)static BITMAP bmp_button;#define BUTTON_BMP (&bmp_button)static int ButtonCtrlProc (HWND hWnd, int uMsg, WPARAM wParam, LPARAM lParam);BOOL RegisterButtonControl (void){ WNDCLASS WndClass; if (!LoadSystemBitmap (&bmp_button, SYSBMP_BUTTON)) return FALSE; WndClass.spClassName = CTRL_BUTTON; WndClass.dwStyle = WS_NONE; WndClass.dwExStyle = WS_EX_NONE; WndClass.hCursor = GetSystemCursor (IDC_ARROW); WndClass.iBkColor = GetWindowElementColor (BKC_CONTROL_DEF); WndClass.WinProc = ButtonCtrlProc; return AddNewControlClass (&WndClass) == ERR_OK;}#if 0void ButtonControlCleanup (void){ // do nothing.}#endifstatic void btnGetRects (HWND hWnd, DWORD dwStyle, RECT* prcClient, RECT* prcText, RECT* prcBitmap){ GetClientRect (hWnd, prcClient); prcClient->right --; prcClient->bottom --; SetRect (prcText, (prcClient->left + BTN_WIDTH_BORDER), (prcClient->top + BTN_WIDTH_BORDER), (prcClient->right - BTN_WIDTH_BORDER), (prcClient->bottom - BTN_WIDTH_BORDER)); SetRectEmpty (prcBitmap); if ( ((dwStyle & BS_TYPEMASK) < BS_CHECKBOX) || (dwStyle & BS_PUSHLIKE)) return; if (dwStyle & BS_LEFTTEXT) { SetRect (prcText, prcClient->left + 1, prcClient->top + 1, prcClient->right - BTN_WIDTH_BMP - BTN_INTER_BMPTEXT, prcClient->bottom - 1); SetRect (prcBitmap, prcClient->right - BTN_WIDTH_BMP, prcClient->top + 1, prcClient->right - 1, prcClient->bottom - 1); } else { SetRect (prcText, prcClient->left + BTN_WIDTH_BMP + BTN_INTER_BMPTEXT, prcClient->top + 1, prcClient->right - 1, prcClient->bottom - 1); SetRect (prcBitmap, prcClient->left + 1, prcClient->top + 1, prcClient->left + BTN_WIDTH_BMP, prcClient->bottom - 1); }}static void btnGetTextBoundsRect (PCONTROL pCtrl, HDC hdc, DWORD dwStyle, const RECT* prcText, RECT* prcBounds){ UINT uFormat; *prcBounds = *prcText; if (dwStyle & BS_MULTLINE) uFormat = DT_WORDBREAK; else uFormat = DT_SINGLELINE; if ((dwStyle & BS_TYPEMASK) == BS_PUSHBUTTON || (dwStyle & BS_TYPEMASK) == BS_DEFPUSHBUTTON || (dwStyle & BS_PUSHLIKE)) uFormat |= DT_CENTER | DT_VCENTER; else { uFormat = DT_TOP | DT_LEFT; if ((dwStyle & BS_ALIGNMASK) == BS_LEFT) uFormat = DT_WORDBREAK | DT_LEFT; else if ((dwStyle & BS_ALIGNMASK) == BS_RIGHT) uFormat = DT_WORDBREAK | DT_RIGHT; else if ((dwStyle & BS_ALIGNMASK) == BS_CENTER) uFormat = DT_WORDBREAK | DT_CENTER; if ((dwStyle & BS_ALIGNMASK) == BS_TOP) uFormat = DT_SINGLELINE | DT_TOP; else if ((dwStyle & BS_ALIGNMASK) == BS_BOTTOM) uFormat = DT_SINGLELINE | DT_BOTTOM; else if ((dwStyle & BS_ALIGNMASK) == BS_VCENTER) uFormat = DT_SINGLELINE | DT_VCENTER; } uFormat |= DT_CALCRECT; DrawText (hdc, pCtrl->spCaption, -1, prcBounds, uFormat);}static void btnPaintContent (PCONTROL pCtrl, HDC hdc, DWORD dwStyle, RECT* prcText){ switch (dwStyle & BS_CONTENTMASK) { case BS_TEXT: case BS_LEFTTEXT: { UINT uFormat; if (dwStyle & BS_MULTLINE) uFormat = DT_WORDBREAK; else uFormat = DT_SINGLELINE; if ((dwStyle & BS_TYPEMASK) == BS_PUSHBUTTON || (dwStyle & BS_TYPEMASK) == BS_DEFPUSHBUTTON || (dwStyle & BS_PUSHLIKE)) uFormat |= DT_CENTER | DT_VCENTER; else { uFormat = DT_TOP | DT_LEFT; if ((dwStyle & BS_ALIGNMASK) == BS_LEFT) uFormat = DT_WORDBREAK | DT_LEFT; else if ((dwStyle & BS_ALIGNMASK) == BS_RIGHT) uFormat = DT_WORDBREAK | DT_RIGHT; else if ((dwStyle & BS_ALIGNMASK) == BS_CENTER) uFormat = DT_WORDBREAK | DT_CENTER; if ((dwStyle & BS_ALIGNMASK) == BS_TOP) uFormat = DT_SINGLELINE | DT_TOP; else if ((dwStyle & BS_ALIGNMASK) == BS_BOTTOM) uFormat = DT_SINGLELINE | DT_BOTTOM; else if ((dwStyle & BS_ALIGNMASK) == BS_VCENTER) uFormat = DT_SINGLELINE | DT_VCENTER; } SetBkColor (hdc, GetWindowBkColor ((HWND)pCtrl)); if (dwStyle & WS_DISABLED) { RECT rc = *prcText; SetBkMode (hdc, BM_TRANSPARENT); SetTextColor (hdc, PIXEL_lightwhite); OffsetRect (prcText, 2, 2); DrawText (hdc, pCtrl->spCaption, -1, prcText, uFormat); SetTextColor (hdc, PIXEL_darkgray); DrawText (hdc, pCtrl->spCaption, -1, &rc, uFormat); } else { SetBkMode (hdc, BM_TRANSPARENT); SetTextColor (hdc, PIXEL_black); OffsetRect (prcText, 1, 1); DrawText (hdc, pCtrl->spCaption, -1, prcText, uFormat); } } break; case BS_BITMAP: if (BUTTON_DATA(pCtrl)) { int x = prcText->left; int y = prcText->top; int w = RECTWP (prcText); int h = RECTHP (prcText); PBITMAP bmp = (PBITMAP)(BUTTON_DATA(pCtrl)); if (dwStyle & BS_REALSIZEIMAGE) { x += (w - bmp->bmWidth) >> 1; y += (h - bmp->bmHeight) >> 1; w = h = 0; } FillBoxWithBitmap (hdc, x, y, w, h, bmp); } break; case BS_ICON: if (BUTTON_DATA(pCtrl)) { int x = prcText->left; int y = prcText->top; int w = RECTWP (prcText); int h = RECTHP (prcText); HICON icon = (HICON)(BUTTON_DATA(pCtrl)); if (dwStyle & BS_REALSIZEIMAGE) { int iconw, iconh; GetIconSize (icon, &iconw, &iconh); x += (w - iconw) >> 1; y += (h - iconh) >> 1; w = h = 0; } DrawIcon (hdc, x, y, w, h, icon); } break; }}static void btnPaintNormalButton (PCONTROL pCtrl, HDC hdc, DWORD dwStyle, RECT* prcClient, RECT* prcText, RECT* prcBitmap){ switch (dwStyle & BS_TYPEMASK) { case BS_DEFPUSHBUTTON: SetPenColor (hdc, PIXEL_black);#ifndef _FLAT_WINDOW_STYLE Rectangle (hdc, prcClient->left, prcClient->top, prcClient->right, prcClient->bottom); Draw3DControlFrame (hdc, prcClient->left + 1, prcClient->top + 1, prcClient->right - 1, prcClient->bottom - 1, GetWindowBkColor ((HWND)pCtrl), TRUE);#else Draw3DControlFrame (hdc, prcClient->left, prcClient->top, prcClient->right, prcClient->bottom, GetWindowBkColor ((HWND)pCtrl), TRUE);#endif break; case BS_PUSHBUTTON: Draw3DControlFrame (hdc, prcClient->left, prcClient->top, prcClient->right, prcClient->bottom, GetWindowBkColor ((HWND)pCtrl), TRUE); break; case BS_AUTORADIOBUTTON: case BS_RADIOBUTTON: if (dwStyle & BS_PUSHLIKE) { Draw3DControlFrame (hdc, prcClient->left, prcClient->top, prcClient->right, prcClient->bottom, GetWindowBkColor ((HWND)pCtrl), TRUE); break; } if (dwStyle & WS_DISABLED) { FillBoxWithBitmapPart (hdc, prcBitmap->left, prcBitmap->top, BTN_WIDTH_BMP, BTN_HEIGHT_BMP, 0, 0, BUTTON_BMP, (BTN_WIDTH_BMP * 2), BTN_HEIGHT_BMP); } else { FillBoxWithBitmapPart (hdc, prcBitmap->left, prcBitmap->top, BTN_WIDTH_BMP, BTN_HEIGHT_BMP, 0, 0, BUTTON_BMP, 0, BTN_HEIGHT_BMP); } break; case BS_3STATE: case BS_AUTO3STATE: case BS_AUTOCHECKBOX: case BS_CHECKBOX: if (dwStyle & BS_PUSHLIKE) { Draw3DControlFrame (hdc, prcClient->left, prcClient->top, prcClient->right, prcClient->bottom, GetWindowBkColor ((HWND)pCtrl), TRUE); break; } if (dwStyle & WS_DISABLED) { FillBoxWithBitmapPart (hdc, prcBitmap->left, prcBitmap->top, BTN_WIDTH_BMP, BTN_HEIGHT_BMP, 0, 0, BUTTON_BMP, BTN_WIDTH_BMP * 2, 0); } else { FillBoxWithBitmapPart (hdc, prcBitmap->left, prcBitmap->top, BTN_WIDTH_BMP, BTN_HEIGHT_BMP, 0, 0, BUTTON_BMP, 0, 0); } break;/* case BS_OWNERDRAW: break;*/ default: break; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -