将自己的程序加入到windows的托盘通知区的示意图
1 form窗体设计

2 加入托盘区源码
//----------------------------------------------------------------------------
#include <vcl.h>
#include <shellapi.h>
#include<Clipbrd.hpp>
#pragma hdrstop
#include "traymain1x.h"
#include "plmsunit19.h"
#include "reg23x.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::DrawItem(TMessage& Msg)
{
IconDrawItem((LPDRAWITEMSTRUCT)Msg.LParam);
TForm::Dispatch(&Msg);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::MyNotify(TMessage& Msg)
{
POINT MousePos;
switch(Msg.LParam)
{
case WM_RBUTTONUP:
if (GetCursorPos(&MousePos))
{
PopupMenu1->PopupComponent = FormMain;
if(IsWindowVisible(Handle))
SetForegroundWindow(Handle);
PopupMenu1->Popup(MousePos.x, MousePos.y);
}
else
Show();
break;
case WM_LBUTTONDOWN:
break;
case WM_LBUTTONUP:
if (GetCursorPos(&MousePos))
{
PopupMenu1->PopupComponent = FormMain;
if(IsWindowVisible(Handle))
SetForegroundWindow(Handle);
PopupMenu1->Popup(MousePos.x, MousePos.y);
}
else
Show();
// ToggleState();
break;
default:
break;
}
TForm::Dispatch(&Msg);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormDestroy(TObject *Sender)
{
TrayMessage(NIM_DELETE);
}
//---------------------------------------------------------------------------
bool __fastcall TFormMain::TrayMessage(DWORD dwMessage)
{
NOTIFYICONDATA tnd;
PSTR pszTip;
pszTip = TipText();
tnd.cbSize = sizeof(NOTIFYICONDATA);
tnd.hWnd = Handle;
tnd.uID = IDC_MYICON;
tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
tnd.uCallbackMessage = MYWM_NOTIFY;
if (dwMessage == NIM_MODIFY)
{
tnd.hIcon = (HICON)IconHandle();
if (pszTip)
lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
else
tnd.szTip[0] = '\0';
}
else
{
tnd.hIcon = NULL;
tnd.szTip[0] = '\0';
}
return (Shell_NotifyIcon(dwMessage, &tnd));
}
//---------------------------------------------------------------------------
HICON __fastcall TFormMain::IconHandle(void)
{
if (RadioButton1->Checked)
return (Image1->Picture->Icon->Handle);
else
return (Image2->Picture->Icon->Handle);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ToggleState(void)
{
if (RadioButton1->Checked)
{
RadioButton1->Checked = false;
RadioButton2->Checked = true;
}
else
{
RadioButton2->Checked = false;
RadioButton1->Checked = true;
}
TrayMessage(NIM_MODIFY);
}
//---------------------------------------------------------------------------
PSTR __fastcall TFormMain::TipText(void)
{
if (RadioButton1->Checked)
return (Edit1->Text.c_str());
else
return (Edit2->Text.c_str());
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::CheckBox1Click(TObject *Sender)
{
if (CheckBox1->Checked)
{
TrayMessage(NIM_ADD);
TrayMessage(NIM_MODIFY);
}
else
TrayMessage(NIM_DELETE);
Button1->Enabled = CheckBox1->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Button1Click(TObject *Sender)
{
Hide();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::RadioButtonClick(TObject *Sender)
{
if (!CheckBox1->Checked)
return;
TrayMessage(NIM_MODIFY);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::EditKeyUp(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if (!CheckBox1->Checked)
return;
TrayMessage(NIM_MODIFY);
}
//---------------------------------------------------------------------------
LRESULT IconDrawItem(LPDRAWITEMSTRUCT lpdi)
{
HICON hIcon;
hIcon = (HICON)LoadImage(g_hinst, MAKEINTRESOURCE(lpdi->CtlID), IMAGE_ICON,
16, 16, 0);
if (!hIcon)
return(FALSE);
DrawIconEx(lpdi->hDC, lpdi->rcItem.left, lpdi->rcItem.top, hIcon,
16, 16, 0, NULL, DI_NORMAL);
return(TRUE);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Properties1Click(TObject *Sender)
{
lifeformx->Label5->OnClick(Sender);
// Show();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ToggleState1Click(TObject *Sender)
{
lifeformx->stopsrfbl->Checked=!lifeformx->stopsrfbl->Checked;
// ToggleState();
if(lifeformx->stopsrfbl->Checked)
lifeformx->BitBtn3->Click();
else
lifeformx->Panel32->OnClick(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Shutdown1Click(TObject *Sender)
{
lifeformx->Close();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormCreate(TObject *Sender)
{
// return;
if (CheckBox1->Checked)
{
TrayMessage(NIM_ADD);
TrayMessage(NIM_MODIFY);
}
else
TrayMessage(NIM_DELETE);
FormMain->Visible=false;
FormMain->Left=-2000;
Button1->Enabled = CheckBox1->Checked;
Edit4->Text=regformx->Label1->Caption;
Hide();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::N1Click(TObject *Sender)
{
/* if(lifeformx->Panel32->Color==clGreen)
{
Button2->Enabled=false;
}
FormMain->Show();*/
lifeformx->Button39->Click();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Button2Click(TObject *Sender)
{
regformx->Edit3->Text=Edit3->Text;
regformx->Button1->Click();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Button4Click(TObject *Sender)
{
if(lifeformx->Panel32->Color==clGreen)
{
Button2->Enabled=false;
}
else
Edit3->Text=Clipboard()->AsText;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Button3Click(TObject *Sender)
{
Clipboard()->AsText=Edit4->Text;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::N2Click(TObject *Sender)
{
lifeformx->BitBtn4->Click();
}
//---------------------------------------------------------------------------
Windows程序实现托盘区图标及通知功能
这篇博客详细介绍了如何将一个Windows应用程序的图标添加到系统托盘区域,并实现右键菜单和提示信息的功能。通过使用VCL库、ShellAPI和Clipbrd组件,实现了窗体隐藏、图标显示、鼠标点击事件处理等操作。
1127

被折叠的 条评论
为什么被折叠?



