用C++Builder在Windows开始按钮上绘图制作方法

熟悉Windows操作系统的软件设计人员知道,在Win95/98/NT/2000中有一任务栏(Task Bar)程序,路径为:C:\WINDOWS\SYSTEM\SYSTRAY.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 中新建工程Project1Project1中包含Form1,窗体如下图所示:

  2、定义变量

  HWND wnd;

HDC hdcButton, hdcDesktop;

TPoint pt;

  3Form1FormCreate 过程代码如下

  void __fastcall TForm1::FormCreate(TObject *Sender)

{

Application->MessageBox("利用C++BuilderWindows开始按钮上绘图演示程序", "特别说明", 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;//开始绘图---www.bianceng.cn

}

  4Form1BitBtn1Click过程代码如下:

  void __fastcall TForm1::BitBtn1Click(TObject *Sender)

{

if (BitBtn1->Tag == 0)

Timer1->Enabled = True;

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

BitBtn1->Tag = 1;

}

else

Close();

}

  5Form1Timer1Timer过程代码如下:

  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.0Windows95/98/NT/2000简体中文版环境下调试通过。

  三.程序清单

  #include <vcl.h>

#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++BuilderWindows开始按钮上绘图演示程序", "特别说明", 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;//开始绘图---www.bianceng.cn

}

void __fastcall TForm1::BitBtn1Click(TObject *Sender)

{

if (BitBtn1->Tag == 0)

Timer1->Enabled = True;

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

BitBtn1->Tag = 1;

else

Close();

}

 

转载于:https://www.cnblogs.com/blogpro/p/11445986.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值