发布时间:2002-02-20 20:10:40
来源:中国计算机报
作者:金君飞
//定义全局变量 HHOOK hMsgHook; //钩子句柄 int iClientHeight, iClientWidth; //待画的客户区高和宽 Graphics::TBitmap Face; // 从文件调用位图的控件 HBITMAP hFaceBitmap; //位图句柄 HWND hClientHandle, hMdiHandle; //MDI主窗口和MDI客户窗口句柄 LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam ) ; void __fastcall TMainForm::FormPaint(TObject Sender) { iClientHeight = ClientHeight; iClientWidth = ClientWidth; } //设置系统时, 在状态条上显示 void __fastcall TMainForm::FormShow(TObject Sender) { //从文件中调入位图 Face = new Graphics::TBitmap(); Face->LoadFromFile(“d:\\temp\\face.bmp”); hFaceBitmap = Face->Handle; //保存位图句柄 hClientHandle = ClientHandle; //保存窗口句柄 hMdiHandle = Handle; //保存MDI主窗口句柄 //安装截取程序消息的钩子函数 hMsgHook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)GetMsgProc, NULL, GetCurrentThreadId() ); } //钩子函数,处理系统WM_PAINT和WM_ERASEBKGND消息 LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam ) { LRESULT lReturn=0; MSG cwMessage; cwMessage = (MSG)lParam; if ( cwMessage->hwnd == hClientHandle || cwMessage->hwnd == hMdiHandle) //是发送给子窗口的消息则处理 {if ( cwMessage->message == WM_PAINT || cwMessage->message == WM_ERASEBKGND ) { //重画用户窗口 DrawBitmap(hClientHandle, hFaceBitmap, iClientHeight, iClientWidth); } } if ( hMsgHook != NULL) //将消息继续下传 lReturn = CallNextHookEx(hMsgHook, nCode, wParam, lParam ); return lReturn; } //卸载钩子函数 void __fastcall TMainForm::FormClose(TObject Sender, TCloseAction &&Action) { if ( hMsgHook != NULL) UnhookWindowsHookEx( hMsgHook ); if ( Face != NULL ) delete Face; } //在指定的窗口中,画位图,填充整个用户窗口 //Ture为绘制成功,false为绘制失败 BOOL DrawBitmap(HWND Handle, HBITMAP hBitmap, int iClientHeight, int iClientWidth) { if ( hBitmap == NULL ) return false; BITMAP b; int iBitmapH, iBitmapW; GetObject( hBitmap, sizeof( BITMAP), &&b); iBitmapH = b.bmHeight; iBitmapW = b.bmWidth; int x, y; HDC hClientDC, hMemDC; hClientDC = GetDC(Handle); if ( hClientDC == NULL ) return false; hMemDC = CreateCompatibleDC( hClientDC ); if ( hMemDC == NULL ) { DeleteDC( hClientDC ); return false; } SelectObject( hMemDC, hBitmap ); x = 0; while ( x < iClientWidth ) { y = 0; while ( y < iClientHeight ) {ClientCanvas->Draw(x, y, Face); BitBlt( hClientDC, x, y,iBitmapW, iBitmapH, hMemDC, 0, 0,SRCCOPY ); y = y + iBitmapH; } x = x + iBitmapW; } DeleteDC( hMemDC ); DeleteDC( hClientDC ); return true; } |