⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 C++ BUILDER精彩编程实例集锦(源码) 第一部分 界面设计 第二部分 程序设置
💻 CPP
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Main.h"
#include "About.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
//定义全局变量
HHOOK hMsgHook;//钩子句柄
int iClientHeight, iClientWidth;//待画的客户区高和宽
Graphics::TBitmap *BackBmp;
HBITMAP hFaceBitmap;//位图句柄
HWND hClientHandle, hMdiHandle;//MDI主窗口和MDI客户窗口句柄
TMainForm *MainForm;
//在指定的窗口中,画位图,填充整个用户窗口,Ture为绘制成功,false为绘制失败
BOOL DrawBitmap(HWND Handle, HBITMAP hBitmap, int iClientHeight, int iClientWidth)
{
  if(hBitmap==NULL)
     return false;
  BITMAP Bmp;
  int iBitmapH, iBitmapW;
  GetObject( hBitmap, sizeof( BITMAP), &Bmp);
  iBitmapH = Bmp.bmHeight;
  iBitmapW = Bmp.bmWidth;
  HDC hClientDC, hMemDC;
  hClientDC = GetDC(Handle);
  if(hClientDC==NULL)
    return false;
  hMemDC = CreateCompatibleDC( hClientDC );
  if(hMemDC==NULL)
  {
   DeleteDC(hClientDC);
   return false;
  }
  SelectObject(hMemDC,hBitmap);
  int x=0, y=0;
  while(x<iClientWidth )
  {
   y = 0;
   while(y<iClientHeight)
   {
     MainForm->Canvas->Draw(x, y, BackBmp);
     BitBlt(hClientDC, x, y,iBitmapW, iBitmapH, hMemDC, 0, 0,SRCCOPY );
     y = y + iBitmapH;
   }
   x = x + iBitmapW;
  }
  DeleteDC( hMemDC );
  DeleteDC( hClientDC );
  return true;
}
//钩子函数,处理系统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;
}
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent *Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::CreateMDIChild(String Name)
{
	TMDIChild *Child;

	//--- create a new MDI child window ----
	Child = new TMDIChild(Application);
	Child->Caption = Name;
	if (FileExists (Name))
		Child->Memo1->Lines->LoadFromFile(Name);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FileNew1Execute(TObject *Sender)
{
	CreateMDIChild("NONAME" + IntToStr(MDIChildCount + 1));
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FileOpen1Execute(TObject *Sender)
{
	if (OpenDialog->Execute())
		CreateMDIChild(OpenDialog->FileName);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::HelpAbout1Execute(TObject *Sender)
{
	AboutBox->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FileExit1Execute(TObject *Sender)
{
	Close();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormPaint(TObject *Sender)
{
iClientHeight = ClientHeight;
iClientWidth = ClientWidth;

}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormShow(TObject *Sender)
{//从文件中调入位图
  BackBmp = new Graphics::TBitmap();
  BackBmp->LoadFromFile("Show.bmp");
  hFaceBitmap = BackBmp->Handle;
  //保存位图句柄
  hClientHandle = ClientHandle;
  //保存窗口句柄
  hMdiHandle = Handle;
  //保存MDI主窗口句柄
  //安装截取程序消息的钩子函数
   hMsgHook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)GetMsgProc, NULL, GetCurrentThreadId());
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{//卸载钩子函数
 if ( hMsgHook != NULL)
   UnhookWindowsHookEx( hMsgHook );
 if ( BackBmp != NULL )
   delete BackBmp;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -