给ThemeButton加上图标

采用 DrawThemeBackground API函数可以给ThemeButton加上图标,类似以前Delphi风格的按钮。代码及界面效果如下,不难,就不多解释了

 



LRESULT CSkinWnd::ButtonWindowProc(HWND hRealWnd, UINT msg, WPARAM wp, LPARAM lp)
{
	switch (msg) 
	{
	case WM_PAINT:
		{
			DWORD style = (UINT)GetWindowLong(hRealWnd, GWL_STYLE) & 0xff;
			if ( style != BS_PUSHBUTTON && style != BS_DEFPUSHBUTTON )
				return CSubclassWnd::WindowProc(hRealWnd, msg,wp, lp);

			CPaintDC dc(GetCWnd());
			CRect rc;
			CWnd* pWnd = CWnd::FromHandle(hRealWnd);
			pWnd->GetClientRect(rc);
			CDC xDC;
			xDC.CreateCompatibleDC(&dc);
			CBitmap xbmp;
			xbmp.CreateCompatibleBitmap(&dc, rc.Width(), rc.Height());
			xDC.SelectObject(&xbmp);

			int state = (int)::SendMessageW(hRealWnd, BM_GETSTATE, 0, 0);

			HTHEME hTheme=OpenThemeData(hRealWnd, L"Button");
			if(hTheme)
			{
				int drawState;
				if(::IsWindowEnabled(hRealWnd))
				{
					if(state & BST_PUSHED) 
						drawState = PBS_PRESSED;
					else if(state & BST_HOT) 
						drawState = PBS_HOT;
					else if(state & BST_FOCUS)
						drawState = PBS_DEFAULTED;
					else drawState = PBS_NORMAL;
				}
				else drawState = PBS_DISABLED;
				::DrawThemeParentBackground(hRealWnd, xDC.m_hDC, rc);
				DrawThemeBackground (hTheme,xDC.m_hDC, BP_PUSHBUTTON, drawState,&rc,NULL);
				CloseThemeData (hTheme);
			}
			else
			{
				int nState;
				if(!::IsWindowEnabled(hRealWnd))
					nState = DFCS_BUTTONPUSH|DFCS_INACTIVE;
				else if(state & BST_PUSHED)
					nState = DFCS_BUTTONPUSH|DFCS_PUSHED;
				else if (state & BST_HOT)
					nState = DFCS_BUTTONPUSH|DFCS_HOT;
				else
					nState = DFCS_BUTTONPUSH;

				::DrawFrameControl(xDC.m_hDC, rc, DFC_BUTTON, nState);
			}

			LONG l = ::GetWindowLong(hRealWnd, DWL_USER);

			UINT nID = ::GetDlgCtrlID(hRealWnd);
			int k  = 0, j = 0;
			if (IDOK == nID || IDYES == nID || IDCANCEL == nID || IDNO == nID)
			{

				CDC bDC;
				CBitmap bmpx;
				bmpx.LoadBitmapW((IDCANCEL == nID || IDNO == nID) ? IDB_CANCEL_ICO: IDB_OK_ICO);

				bDC.CreateCompatibleDC(&xDC);
				bDC.SelectObject(bmpx);
				BITMAP bm;
				bmpx.GetBitmap(&bm);
				int l = 4;
				int t = (rc.Height() - bm.bmHeight) / 2;
				::TransparentBlt(xDC.m_hDC, l, t, bm.bmWidth, bm.bmHeight, bDC.m_hDC, 0, 0,  bm.bmWidth, bm.bmHeight, RGB(255, 255, 255));
				k = l; j = bm.bmWidth;
			}

			if (::IsWindowEnabled(hRealWnd))
				::SetTextColor(xDC, RGB(255, 64, 64));
			else
				::SetTextColor(xDC, RGB(128, 128, 128));

			xDC.SetBkMode(TRANSPARENT);
			xDC.SelectObject(pWnd->GetFont());
			int old = rc.left;
			rc.left += k + j;
			wchar_t lpsz[256];
			::GetWindowText(hRealWnd, lpsz, 256);

			::DrawText(xDC.m_hDC,  lpsz, _tcslen(lpsz), rc, DT_CENTER | DT_VCENTER|DT_SINGLELINE);
			rc.left = old;
			::BitBlt(dc.m_hDC, rc.left, rc.top, rc.Width(), rc.Height(), xDC.m_hDC, rc.left, rc.top, SRCCOPY);
			return 0L;
		}
	}
	
	// We don't handle it: pass along
	return CSubclassWnd::WindowProc(hRealWnd, msg, wp, lp);
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Hooks 是 React 16.8 中新增的特性,它允许函数组件中使用 state 和其他 React 特性,从而使函数组件具有类组件的能力。 使用 React Hooks 需要先引入 React 的 useState、useEffect 等钩子函数,然后在函数组件中使用它们。 useState useState 是最常用的 Hook 之一,它可以让我们在函数组件中使用 state。useState 接收一个初始值作为参数,并返回一个数组,数组的第一个元素是当前 state 的值,第二个元素是更新 state 的函数。 例如,下面的代码在函数组件中使用了 useState 来保存一个计数器: ``` import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}> Click me </button> </div> ); } ``` useEffect useEffect 是另外一个常用的 Hook,它可以在函数组件中使用副作用。副作用包括数据获取、订阅或手动修改 DOM 等操作。useEffect 接收一个函数作为参数,该函数会在组件渲染完成后执行。 例如,下面的代码使用 useEffect 来更新页面标题: ``` import React, { useState, useEffect } from 'react'; function PageTitle() { const [count, setCount] = useState(0); useEffect(() => { document.title = `You clicked ${count} times`; }); return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}> Click me </button> </div> ); } ``` useContext useContext 可以让我们在函数组件中使用 Context。Context 是一种在组件树中传递数据的方法,它可以避免通过 props 层层传递数据。 例如,下面的代码使用 useContext 来获取全局的主题: ``` import React, { useContext } from 'react'; const ThemeContext = React.createContext('light'); function ThemeButton() { const theme = useContext(ThemeContext); return ( <button style={{ background: theme.background, color: theme.foreground }}> I am styled by theme context! </button> ); } ``` 使用 useContext 前需要先创建一个 Context,可以使用 React.createContext 方法来创建。 除了上述三个 Hook,还有 useReducer、useCallback、useMemo、useRef 等 Hook 可以使用。使用这些 Hook 可以让函数组件更加强大和灵活。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值