程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 利用C++Builder在Windows“開始”按鈕上繪圖

利用C++Builder在Windows“開始”按鈕上繪圖

編輯:C++入門知識

  熟悉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 <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++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();
  }  
  
 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved