📄 20010312004.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>利用C++Builder在Windows“开始”按钮上绘图</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<META content="Microsoft FrontPage 4.0" name=GENERATOR></HEAD>
<BODY aLink=#ff0000 bgColor=#ffffff leftMargin=0 link=#187800 topMargin=0
vLink=#990099>
<DIV align=center>
<TABLE border=0 cellPadding=0 cellSpacing=0 height=76 width=744>
<TBODY>
<TR>
<TD>
<P align=center><SPAN class=name00><FONT color=#ff3300></FONT></SPAN></P>
<H2 align=center><FONT size=3>利用C++Builder在Windows“开始”按钮上绘图</FONT></H2>
<DIV align=center><BR><B>安徽合肥智能机械研究所 张建军</B> </DIV>
<DIV align=center>
<DIV align=center>
<DIV>
<DIV align=left><BR>熟悉Windows操作系统的软件设计人员知道,在Win95/98/NT/2000中有一任务栏(Task
Bar)程序,路径为:C:\WINDOWS\SYSTEM\SYSTRAY.EXE(假设你的Windows安装在系统默认路径C:\WINDOWS)。从系统功能角度分析,任务栏由几个不同的子区域组成,从左至右依次是:开始(Start)按钮、应用程序切换区(Application
Switch Bar)、任务栏通知区(Notification
Area)以及任务栏时钟。从程序编制角度分析,任务栏程序(SYSTRAY.EXE)与其它Windows应用程序相同,由几个不同的窗体组成,这些窗体具有各自窗口类名、句柄、显示方式等信息。
<BR><B>一.要点说明</B> <BR>1、任务栏、开始按钮的窗口信息:<BR>◆Tray
Bar的窗口类名:Shell_TrayWnd<BR>◆开始按钮的窗口类名:Button
<BR>2、调用FindWindow函数获得任务栏窗口句柄。<BR>3、调用FindWindowEx函数获得开始按钮窗口句柄。<BR>4、调用GetDC函数获得开始按钮设备和桌面窗口上下文关系。<BR>5、调用GetDeskTopWindow桌面窗口句柄。<BR>6、调用GetCursorPos函数获得当前鼠标位置。<BR>7、调用StretchBlt函数将鼠标背景绘制在开始按钮上<BR>8、调用ReleaseDC释放开始按钮和桌面窗口上下文关系<BR><B>二.实例</B><BR>1、在C++
Builder 5.0 IDE 中新建工程Project1,Project1中包含Form1,窗体如下图所示: <BR><IMG
height=121 src="images/01_3_1_5a.jpeg" width=319
tppabs="http://www.info365.com.cn/develop/bcb/img/01_3_1_5a.jpeg">
<BR>2、定义变量 <BR>HWND wnd;<BR>HDC hdcButton, hdcDesktop;<BR>TPoint
pt;<BR>3、Form1的FormCreate 过程代码如下<BR>void __fastcall
TForm1::FormCreate(TObject *Sender)<BR>{
<BR>Application->MessageBox("利用C++Builder在Windows开始按钮上绘图演示程序", "特别说明",
MB_OK + MB_DEFBUTTON1);<BR>wnd = FindWindow("Shell_TrayWnd", NULL);<BR>wnd
= FindWindowEx(wnd, 0, "Button", NULL);<BR>hdcButton = GetDC(wnd); <BR>wnd
= GetDesktopWindow();<BR>hdcDesktop = GetDC(wnd);<BR>Timer1->Enabled =
False;<BR>Timer1->Interval = 1; <BR>BitBtn1->Tag =
0;//开始绘图<BR>}<BR><BR>4、Form1的BitBtn1Click过程代码如下:<BR>void __fastcall
TForm1::BitBtn1Click(TObject *Sender)<BR>{<BR>if (BitBtn1->Tag ==
0)<BR>Timer1->Enabled = True;<BR>BitBtn1->Caption =
"结束绘图";<BR>BitBtn1->Tag = 1;<BR>} <BR>else<BR>Close();<BR>}
<BR>5、Form1的Timer1Timer过程代码如下:<BR>void __fastcall
TForm1::Timer1Timer(TObject *Sender)<BR>{
<BR>GetCursorPos(&pt);<BR>StretchBlt(hdcButton, 0, 0, 60, 25,
hdcDesktop, pt.x - 30, pt.y - 12, 60, 25, SRCCOPY);<BR>}
<BR>7、按F9运行程序。以上程序在C++ Builder 5.0、Windows95/98/NT/2000简体中文版环境下调试通过。
<BR><BR><B>三.程序清单</B><BR>#include <vcl.h><BR>#pragma hdrstop
<BR>#include "Unit1.h" <BR>#pragma package(smart_init) <BR>#pragma
resource "*.dfm" <BR>TForm1 *Form1; <BR>HWND wnd; <BR>HDC hdcButton,
hdcDesktop; <BR>TPoint pt; <BR>__fastcall TForm1::TForm1(TComponent*
Owner) : TForm(Owner) <BR>{ <BR>} <BR>void __fastcall
TForm1::Timer1Timer(TObject *Sender)
<BR>{<BR>GetCursorPos(&pt);<BR>StretchBlt(hdcButton, 0, 0, 60, 25,
hdcDesktop, pt.x - 30, pt.y - 12, 60, 25, SRCCOPY);<BR>} <BR>void
__fastcall TForm1::FormCreate(TObject *Sender)<BR>{
<BR>Application->MessageBox("利用C++Builder在Windows开始按钮上绘图演示程序", "特别说明",
MB_OK + MB_DEFBUTTON1);<BR>wnd = FindWindow("Shell_TrayWnd", NULL);
<BR>wnd = FindWindowEx(wnd, 0, "Button", NULL); <BR>hdcButton =
GetDC(wnd); <BR>wnd = GetDesktopWindow(); <BR>hdcDesktop = GetDC(wnd);
<BR>Timer1->Enabled = False; <BR>Timer1->Interval = 1;
<BR>BitBtn1->Tag = 0;//开始绘图 <BR>} <BR>void __fastcall
TForm1::BitBtn1Click(TObject *Sender)<BR>{ <BR>if (BitBtn1->Tag == 0)
<BR>Timer1->Enabled = True; <BR>BitBtn1->Caption = "结束绘图";
<BR>BitBtn1->Tag = 1; <BR>else <BR>Close(); <BR>}
</DIV></DIV></DIV></DIV></TD></TR></TBODY></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -