WIN32 工具栏贴图背景图的实现

完整的应用代码下载
代码实现在miniwinouter.rar 的dvbhelper.c里面
LRESULT WINAPI DVBWndProc(HWND hWnd,UINT msgID,WPARAM wParam,LPARAM lParam)

{

	switch(msgID){

	case WM_CREATE:

		{

			LPCREATESTRUCT cs=(LPCREATESTRUCT)lParam;

			SetWindowLong(hWnd,GWL_DVB_OLDPROC,(LONG)cs->lpCreateParams);

			SetWindowLong(hWnd,GWL_DVB_AUTOCLOSE_TIME,MAKELONG(0,2));

			if(cs->dwExStyle&(WS_EXDVB_TIME|WS_EXDVB_AUTOCLOSE)){

				SetTimer(hWnd,IDT_DVBTIME,1000,NULL);

			}

			return 0;

		}

	case WM_NCACTIVATE:return 0;

	case WM_NCCALCSIZE:

		{

			RECT *rc=(LPRECT) lParam;

			rc->top+=40;

		}return WVR_VREDRAW|WVR_HREDRAW;

/*	case WM_ACTIVATE:

		{

			WORD fActive = LOWORD(wParam);  

			HWND hw=GetFocus();

			if(fActive==WA_INACTIVE){

				SetWindowLong(hWnd,GWL_DVB_FOCUS,hw);

			}else{

				hw=GetWindowLong(hWnd,GWL_DVB_FOCUS);

				if(hw)SetFocus(hw);

			}

		}break;*/

	case WM_NCPAINT:

		{

			RECT rc;

			HBITMAP hobmp,hbmpTitle;

			HBRUSH hbrush;

			BITMAP bmp;

			HDC hdc;

			HRGN hrgn;

			CHAR Caption[64];

			INT iCaptionLen;

			HICON hico=(HICON)GetClassLong(hWnd,GCL_HICON);

			GetClientRect(hWnd,&rc );

			MapWindowPoints(hWnd,0,(POINT*)&rc,2);

			hrgn=CreateRectRgnIndirect(&rc);

			/*if((UINT)clip>1){

				CombineRgn(hrgn,clip,hrgn,RGN_DIFF );

				hdc = GetDCEx(hWnd, (HRGN)wParam, DCX_WINDOW|DCX_INTERSECTRGN);

			}else*/

				hdc=GetDCEx( hWnd, hrgn,/* DCX_USESTYLE |*/ DCX_WINDOW | DCX_EXCLUDERGN );

			hbmpTitle=LoadBitmap(hInstDVBRes,MAKEINTRESOURCE(IDB_WINTITLE));

			DeleteObject((HGDIOBJ)hrgn);

			hbrush=CreatePatternBrush(hbmpTitle);

			GetObject((HGDIOBJ)hbmpTitle,sizeof(BITMAP),&bmp);

			GetWindowRect(hWnd,&rc);

			OffsetRect(&rc,-rc.left,-rc.top);

			rc.bottom=rc.top+40;

			FillRect(hdc,&rc,hbrush);

			iCaptionLen=GetWindowText(hWnd,Caption,64);

			rc.left+=40;

			SetBkMode(hdc,TRANSPARENT);

			//DrawIcon(hdc,4,4,hico);

			DrawIconEx(hdc,4,4,hico,32,32,0,0,DI_MASK|DI_IMAGE);

			DrawText(hdc,Caption,iCaptionLen,&rc,DT_VCENTER|DT_LEFT|DT_SINGLELINE);

			if(GetWindowLong(hWnd,GWL_EXSTYLE)&WS_EXDVB_TIME){

				char sdate[128],stime[64];

				SYSTEMTIME tnow;

				GetLocalTime(&tnow);

				GetDateFormat(LOCALE_NEUTRAL,0,&tnow,0,sdate,64);

				GetTimeFormat(LOCALE_NEUTRAL,/*TIME_NOSECONDS|*/TIME_FORCE24HOURFORMAT,&tnow," hh:mm:ss tt",stime,64);

				strcat(sdate,stime);

				rc.right-=16;

				DrawText(hdc,sdate,-1,&rc,DT_VCENTER|DT_RIGHT|DT_SINGLELINE);

			}

			DeleteObject((HGDIOBJ)hbrush); 

			DeleteObject((HGDIOBJ)hbmpTitle);

			ReleaseDC(hWnd,hdc);

		}return 0;

	case WM_TIMER:

		{

			DWORD exStyle=GetWindowLong(hWnd,GWL_EXSTYLE);

			if(wParam==IDT_DVBTIME){

				if(exStyle&WS_EXDVB_TIME)		SendMessage(hWnd,WM_NCPAINT,1,0);

				if(exStyle&WS_EXDVB_AUTOCLOSE){

					DWORD t=GetWindowLong(hWnd,GWL_DVB_AUTOCLOSE_TIME);

					if(LOWORD(t)>=HIWORD(t)){

						SendMessage(hWnd,WM_CLOSE,0,0);

						t=MAKELONG(0,HIWORD(t));

					}else

						t=MAKELONG(t+1,HIWORD(t));

					SetWindowLong(hWnd,GWL_DVB_AUTOCLOSE_TIME,t);

				}

			}else{

				WNDPROC proc=(WNDPROC)GetWindowLong(hWnd,0);

				if(proc)return proc(hWnd,msgID,wParam,lParam);

			}

		}

		break;

	case WM_NOTIFY:

		{

			NMHDR *nmhdr = (PVOID)lParam;

			if((nmhdr->code == NM_CUSTOMDRAW)&&(nmhdr->idFrom==IDC_TOOLBAR_WINHELP)) {//IDC_TOOLBAR_WINHELP为工具栏的ID

				NMLVCUSTOMDRAW *nmlvcd = (PVOID)nmhdr;

				switch(nmlvcd->nmcd.dwDrawStage) {

				case CDDS_PREPAINT://工具栏绘图输出准备阶段绘制背景

					{

						HBITMAP hbmp=LoadBitmap(hInstDVBRes,MAKEINTRESOURCE(IDB_WINTITLE));

						HBRUSH hb=CreatePatternBrush(hbmp);

						FillRect(nmlvcd->nmcd.hdc,&nmlvcd->nmcd.rc,hb);

						DeleteObject((HGDIOBJ)hb);

						DeleteObject((HGDIOBJ)hbmp);

					}

					return CDRF_SKIPDEFAULT;//因为已经绘制背景图片,告诉UI系统跳过绘制背景

				default:return CDRF_DODEFAULT;

				}

			}else{

				WNDPROC proc=(WNDPROC)GetWindowLong(hWnd,0);

				if(proc)return proc(hWnd,msgID,wParam,lParam);

				return DefWindowProc(hWnd,msgID,wParam,lParam);

			}

		}break;

	case WM_ERASEBKGND: 

		{		

			HBRUSH hbrush;  

			//画背景

			hbrush=CreateSolidBrush(RGB(61,185,197));

			FillRect((HDC)wParam,NULL,hbrush);

			DeleteObject((HGDIOBJ)hbrush);

		}

		return 1;

	case WM_KEYDOWN:

		{

			DWORD t=GetWindowLong(hWnd,GWL_DVB_AUTOCLOSE_TIME);

			t=MAKELONG(0,HIWORD(t));

			SetWindowLong(hWnd,GWL_DVB_AUTOCLOSE_TIME,t);

		}

	default:

		{

			WNDPROC proc=(WNDPROC)GetWindowLong(hWnd,0);

			if(proc)return proc(hWnd,msgID,wParam,lParam);

			return DefWindowProc(hWnd,msgID,wParam,lParam);

		}

	}

	return 0;

}
//效果图,底部为工具栏控件
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值