龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

利用C++Builder在Windows“开始”按钮上绘图

时间:2009-12-22 15:42来源:未知 作者:admin 点击:
分享到:
熟悉Windows操作系统的软件设计人员知道,在Win95/98/NT/2000中有一任务栏(Task Bar)程序,路径为:C:WINDOWSSYSTEMSYSTRAY.EXE(假设你的Windows安装在系统默认路径C:WINDOWS)。从系统功能角度分析

  熟悉Windows操作系统的软件设计人员知道,在Win95/98/NT/2000中有一任务栏(Task Bar)程序,路径为:C:WINDOWSSYSTEMSYSTRAY.EXE(假设你的Windows安装在系统默认路径C:WINDOWS)。从系统功能角度分析,任务栏由几个不同的子区域组成,从左至右依次是:开始(Start)按钮、应用程序切换区(Application Switch Bar)、任务栏通知区(Notification Area)以及任务栏时钟。从程序编制角度分析,任务栏程序(SYSTRAY.EXE)与其它Windows应用程序相同,由几个不同的窗体组成,这些窗体具有各自窗口类名、句柄、显示方式等信息。

  一.要点说明

  1、任务栏、开始按钮的窗口信息:

  ◆Tray Bar的窗口类名:Shell_TrayWnd

  ◆开始按钮的窗口类名:Button

  2、调用FindWindow函数获得任务栏窗口句柄。

  3、调用FindWindowEx函数获得开始按钮窗口句柄。

  4、调用GetDC函数获得开始按钮设备和桌面窗口上下文关系。

  5、调用GetDeskTopWindow桌面窗口句柄。

  6、调用GetCursorPos函数获得当前鼠标位置。

  7、调用StretchBlt函数将鼠标背景绘制在开始按钮上

  8、调用ReleaseDC释放开始按钮和桌面窗口上下文关系

  二.实例

  1、在C++ Builder 5.0 IDE 中新建工程Project1,Project1中包含Form1,窗体如下图所示:

  

  2、定义变量

  HWND wnd;

  HDC hdcButton, hdcDesktop;

  TPoint pt;

  3、Form1的FormCreate 过程代码如下

  void __fastcall TForm1::FormCreate(TObject *Sender)

  {

  Application->MessageBox("利用C++Builder在Windows开始按钮上绘图演示程序", "非凡说明", MB_OK + MB_DEFBUTTON1);

  wnd = FindWindow("Shell_TrayWnd", NULL);

  wnd = FindWindowEx(wnd, 0, "Button", NULL);

  hdcButton = GetDC(wnd);

  wnd = GetDesktopWindow();

  hdcDesktop = GetDC(wnd);

  Timer1->Enabled = False;

  Timer1->Interval = 1;

  BitBTn1->Tag = 0;//开始绘图

  }

  

  4、Form1的BitBtn1Click过程代码如下:

  void __fastcall TForm1::BitBtn1Click(TObject *Sender)

  {

  if (BitBtn1->Tag == 0)

  Timer1->Enabled = True;

  BitBtn1->Caption = "结束绘图";

  BitBtn1->Tag = 1;

  }

  else

  Close();

  }

  5、Form1的Timer1Timer过程代码如下:

  void __fastcall TForm1::Timer1Timer(TObject *Sender)

  {

  GetCursorPos(&pt);

  StretchBlt(hdcButton, 0, 0, 60, 25, hdcDesktop, pt.x - 30, pt.y - 12, 60, 25, SRCCOPY);

  }

  7、按F9运行程序。以上程序在C++ Builder 5.0、Windows95/98/NT/2000简体中文版环境下调试通过。

  

  三.程序清单

  #include

  #pragma hdrstop

  #include "Unit1.h"

  #pragma package(smart_init)

  #pragma resource "*.dfm"

  TForm1 *Form1;

  HWND wnd;

  HDC hdcButton, hdcDesktop;

  TPoint pt;

  __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)

  {

  }

  void __fastcall TForm1::Timer1Timer(TObject *Sender)

  {

  GetCursorPos(&pt);

  StretchBlt(hdcButton, 0, 0, 60, 25, hdcDesktop, pt.x - 30, pt.y - 12, 60, 25, SRCCOPY);

  }

  void __fastcall TForm1::FormCreate(TObject *Sender)

  {

  Application->MessageBox("利用C++Builder在Windows开始按钮上绘图演示程序", "非凡说明", MB_OK + MB_DEFBUTTON1);

  wnd = FindWindow("Shell_TrayWnd", NULL);

  wnd = FindWindowEx(wnd, 0, "Button", NULL);

  hdcButton = GetDC(wnd);

  wnd = GetDesktopWindow();

  hdcDesktop = GetDC(wnd);

  Timer1->Enabled = False;

  Timer1->Interval = 1;

  BitBtn1->Tag = 0;//开始绘图

  }

  void __fastcall TForm1::BitBtn1Click(TObject *Sender)

  {

  if (BitBtn1->Tag == 0)

  Timer1->Enabled = True;

  BitBtn1->Caption = "结束绘图";

  BitBtn1->Tag = 1;

  else

  Close();

  }

  

  

精彩图集

赞助商链接