這個程式看是否合你用?
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp><br />#include "Trayicon.h"
#include
#include
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TTrayIcon *TrayIcon1;
TPopupMenu *PopupMenu1;
TMenuItem *test11;
TMenuItem *test21;
TMenuItem *test31;
TListBox *ListBox1;
TMenuItem *N1;
TApplicationEvents *ApplicationEvents1;
void __fastcall FormActivate(TObject *Sender);
void __fastcall PopupMenu1Popup(TObject *Sender);
void __fastcall TrayIcon1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y);
void __fastcall test31Click(TObject *Sender);
void __fastcall test11Click(TObject *Sender);
void __fastcall TrayIcon1Minimize(TObject *Sender);
void __fastcall N1Click(TObject *Sender);
void __fastcall ListBox1DblClick(TObject *Sender);
void __fastcall ApplicationEvents1Idle(TObject *Sender,
bool &Done);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Trayicon"
#pragma resource "*.dfm"
TForm1 *Form1;
HWND LastEditHandle;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormActivate(TObject *Sender)
{
Application->Minimize();
ShowWindow(Application->Handle, SW_HIDE);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PopupMenu1Popup(TObject *Sender)
{
Form1->Visible = true;
SetForegroundWindow(this->Handle);
// ListBox1->Visible=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TrayIcon1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
POINT p;
::GetCursorPos(&p);
PopupMenu1->Popup(p.x, p.y);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::test31Click(TObject *Sender)
{
Form1->Close();
}
//---------------------------------------------------------------------------
// 呼叫 ListBox
void __fastcall TForm1::test11Click(TObject *Sender)
{
Application->Restore();
// SetForegroundWindow(this->Handle);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TrayIcon1Minimize(TObject *Sender)
{
// Form1->WindowState = wsMinimized;
// Form1->Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N1Click(TObject *Sender)
{
FormActivate(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1DblClick(TObject *Sender)
{
HWND hFocusWin;
if(!IsWindow(LastEditHandle)) return;
if( IsZoomed(LastEditHandle) )
ShowWindow(LastEditHandle, SW_SHOWMAXIMIZED); // 若是最大化, 就最大化顯示
else
ShowWindow(LastEditHandle, SW_SHOWNORMAL); // 不然就直接顯示
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, 0, SPIF_SENDCHANGE);
BringWindowToTop(LastEditHandle);
// SetForegroundWindow(LastEditHandle);
if( AttachThreadInput(GetCurrentThreadId(), GetWindowThreadProcessId(LastEditHandle, NULL), true) )
{
BringWindowToTop(LastEditHandle);
// SetForegroundWindow(LastEditHandle);
Application->ProcessMessages();
hFocusWin = GetFocus();
AttachThreadInput(GetCurrentThreadId(), GetWindowThreadProcessId(LastEditHandle, NULL), false);
if( hFocusWin != NULL )
{
int i;
Byte ch;
AnsiString str = ListBox1->Items->Strings[ListBox1->ItemIndex];
for( i = 1; i <= str.Length(); i ) // 注意: 是從 1 開始, 不是從 0 開始
{
ch = Byte(str[i]);
if( IsDBCSLeadByte(ch) ) // 若是雙位元的字, 就和下一個byte合成一個字
PostMessage(hFocusWin, WM_IME_CHAR, MAKEWORD(Byte(str[ i]), ch), 0);
else // 不然就直接送出
PostMessage(hFocusWin, WM_IME_CHAR, WORD(ch), 0);
}
}
else
ShowMessage("GetFocus() failed!");
}
else
ShowMessage("AttachThreadInput() failed!!");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ApplicationEvents1Idle(TObject *Sender, bool &Done)
{
HWND ForeWindow = ::GetForegroundWindow();
if(ForeWindow != Handle)
{
LastEditHandle = ForeWindow;
char str[255];
SendMessage(LastEditHandle, WM_GETTEXT, 255, (long)str);
ListBox1->Hint = AnsiString("傳送到 - ") str;
}
}
//---------------------------------------------------------------------------