PHotKey.cpp //--------------------------------------------------------------------------- #include #pragma hdrstop //--------------------------------------------------------------------------- USEFORM("UHotKey.cpp", FMain); USEFORM("UClose.cpp", FClose); USEFORM("UChange.cpp", FChange); //--------------------------------------------------------------------------- WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { try { CreateMutex(NULL,true,"MyMutex"); int Res = GetLastError(); if(Res == ERROR_ALREADY_EXISTS || Res == ERROR_INVALID_HANDLE) Application->Terminate(); Application->Initialize(); Application->Title = "Горячие клавиши"; Application->CreateForm(__classid(TFMain), &FMain); Application->Run(); } catch (Exception &exception) { Application->ShowException(&exception); } catch (...) { try { throw Exception(""); } catch (Exception &exception) { Application->ShowException(&exception); } } return 0; } //--------------------------------------------------------------------------- UHotKey.h //--------------------------------------------------------------------------- #ifndef UHotKeyH #define UHotKeyH //--------------------------------------------------------------------------- #include #include #include #include #include #include #include #include //--------------------------------------------------------------------------- const MyTrayIcon = WM_USER + 1; class TFMain : public TForm { __published: // IDE-managed Components TListBox *ListBox1; TButton *BNew; TButton *BChange; TButton *BDelete; TEdit *Edit1; TLabel *Label1; TLabel *Label2; TPopupMenu *PopupMenu1; TMenuItem *MOptions; TMenuItem *N2; TMenuItem *MClose; TMenuItem *N1; TGroupBox *GroupBox1; TLabel *Label3; TEdit *Edit2; TCheckBox *CheckBox1; TApplicationEvents *ApplicationEvents1; void __fastcall FormCreate(TObject *Sender); void __fastcall BNewClick(TObject *Sender); void __fastcall ListBox1Click(TObject *Sender); void __fastcall BDeleteClick(TObject *Sender); void __fastcall BChangeClick(TObject *Sender); void __fastcall FormDestroy(TObject *Sender); void __fastcall FormCloseQuery(TObject *Sender, bool &CanClose); void __fastcall MCloseClick(TObject *Sender); void __fastcall MOptionsClick(TObject *Sender); void __fastcall ApplicationEvents1ShowHint(AnsiString &HintStr, bool &CanShow, THintInfo &HintInfo); private: TNotifyIconData NID; void __fastcall WMHotKey(TWMHotKey & a); void __fastcall CreateSystemIcon(); void __fastcall MTIcon(TMessage & a); public: // User declarations __fastcall TFMain(TComponent* Owner); void __fastcall RegHotKey(int Index); TStringList *HKValues; TStringList *HKProgs; protected: BEGIN_MESSAGE_MAP VCL_MESSAGE_HANDLER(WM_HOTKEY, TWMHotKey, WMHotKey) VCL_MESSAGE_HANDLER(MyTrayIcon, TMessage, MTIcon) END_MESSAGE_MAP(TForm) }; //--------------------------------------------------------------------------- extern PACKAGE TFMain *FMain; //--------------------------------------------------------------------------- #endif //--------------------------------------------------------------------------- UHotKey.cpp //--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "UHotKey.h" #include "UClose.h" #include "UChange.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TFMain *FMain; //--------------------------------------------------------------------------- __fastcall TFMain::TFMain(TComponent* Owner) : TForm(Owner) { HKValues = new TStringList; HKProgs = new TStringList; } //--------------------------------------------------------------------------- void __fastcall TFMain::FormCreate(TObject *Sender) { TRegistry *reg = new TRegistry; TStringList *HKNames = new TStringList; AnsiString prog,name; CreateSystemIcon(); reg->RootKey = HKEY_LOCAL_MACHINE; reg->OpenKey("SOFTWARE\\Maximus\\HotKey",true); reg->GetValueNames(HKNames); ListBox1->Clear(); for(int i = 0;i < HKNames->Count;i++) { prog = HKNames->Strings[i].SetLength(HKNames->Strings[i].Pos("|") - 1); name = HKNames->Strings[i].SubString(prog.Length() + 2,HKNames->Strings[i].Length()); HKProgs->Add(prog); ListBox1->Items->Add(name); Edit1->Text = prog; Edit2->Text = reg->ReadString(HKNames->Strings[i]); RegHotKey(HKValues->Add(Edit2->Text)); } if(ListBox1->Count != 0) { ListBox1->ItemIndex = 0; BChange->Enabled = true; BDelete->Enabled = true; } HKNames->Free(); reg->CloseKey(); reg->Free(); reg = new TRegistry; reg->RootKey = HKEY_LOCAL_MACHINE; reg->OpenKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",false); if(reg->ValueExists(Application->Title)) CheckBox1->Checked = true; else CheckBox1->Checked = false; reg->CloseKey(); reg->Free(); ListBox1Click(Sender); } //--------------------------------------------------------------------------- void __fastcall TFMain::BNewClick(TObject *Sender) { Application->CreateForm(__classid(TFChange), &FChange); FChange->Caption = "Добавить"; if(FChange->ShowModal() == 1) { BChange->Enabled = true; BDelete->Enabled = true; ListBox1->ItemIndex = ListBox1->Count - 1; } ListBox1Click(Sender); } //--------------------------------------------------------------------------- void __fastcall TFMain::RegHotKey(int Index) { AnsiString SHotKey; Word atom,fsModifiers,vk; SHotKey = HKValues->Strings[Index]; atom = GlobalAddAtom(HKProgs->Strings[Index].c_str()); HKValues->Objects[Index] = (TObject *)atom; fsModifiers = 0; if(SHotKey.Pos("Alt") > 0) fsModifiers = fsModifiers | MOD_ALT; if(SHotKey.Pos("Ctrl") > 0) fsModifiers = fsModifiers | MOD_CONTROL; if(SHotKey.Pos("Shift") > 0) fsModifiers = fsModifiers | MOD_SHIFT; vk = (int)SHotKey[SHotKey.Length()]; if(SHotKey[SHotKey.Length() - 1] == 'F') vk += 63; else if(SHotKey[SHotKey.Length() - 2] == 'F') vk += 73; RegisterHotKey(Handle,atom,fsModifiers,vk); } //--------------------------------------------------------------------------- void __fastcall TFMain::ListBox1Click(TObject *Sender) { if(ListBox1->Count != 0) { Edit1->Text = HKProgs->Strings[ListBox1->ItemIndex]; Edit2->Text = HKValues->Strings[ListBox1->ItemIndex]; } else { Edit1->Text = ""; Edit2->Text = ""; } } //--------------------------------------------------------------------------- void __fastcall TFMain::BDeleteClick(TObject *Sender) { if(Application->MessageBox("Действительно хотите удалить текущую запись?", "Удаление",MB_YESNO + MB_ICONQUESTION) == IDYES) { TRegistry *reg = new TRegistry; UnregisterHotKey(Handle,(Word)(HKValues->Objects[ListBox1->ItemIndex])); reg->RootKey = HKEY_LOCAL_MACHINE; reg->OpenKey("SOFTWARE\\Maximus\\HotKey",false); reg->DeleteValue(HKProgs->Strings[ListBox1->ItemIndex] + "|" + ListBox1->Items->Strings[ListBox1->ItemIndex]); reg->CloseKey(); reg->Free(); HKProgs->Delete(ListBox1->ItemIndex); HKValues->Delete(ListBox1->ItemIndex); ListBox1->Items->Delete(ListBox1->ItemIndex); if(ListBox1->Count == 0) { BChange->Enabled = false; BDelete->Enabled = false; } else ListBox1->ItemIndex = 0; ListBox1Click(Sender); } } //--------------------------------------------------------------------------- void __fastcall TFMain::BChangeClick(TObject *Sender) { Application->CreateForm(__classid(TFChange), &FChange); FChange->Caption = "Изменить"; FChange->ShowModal(); ListBox1Click(Sender); } //--------------------------------------------------------------------------- void __fastcall TFMain::WMHotKey(TWMHotKey & a) { for(Word i = 0;i < HKValues->Count;i++) if((Word)(HKValues->Objects[i]) == a.HotKey) ShellExecute(Handle,NULL,HKProgs->Strings[i].c_str(), NULL,NULL,SW_RESTORE); } //--------------------------------------------------------------------------- void __fastcall TFMain::CreateSystemIcon() { Application->ShowMainForm = false; NID.cbSize = sizeof(TNotifyIconData); NID.hWnd = Handle; NID.uID = 1; NID.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; NID.uCallbackMessage = MyTrayIcon; NID.hIcon = Application->Icon->Handle; strcpy(NID.szTip,"Горячие клавиши"); Shell_NotifyIcon(NIM_ADD,&NID); } //--------------------------------------------------------------------------- void __fastcall TFMain::FormDestroy(TObject *Sender) { TRegistry *reg = new TRegistry; reg->RootKey = HKEY_LOCAL_MACHINE; reg->OpenKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",false); if(CheckBox1->Checked == true) reg->WriteString(Application->Title,ParamStr(0)); else reg->DeleteValue(Application->Title); reg->CloseKey(); reg->Free(); Shell_NotifyIcon(NIM_DELETE,&NID); HKValues->Free(); HKProgs->Free(); } //--------------------------------------------------------------------------- void __fastcall TFMain::MTIcon(TMessage & a) { POINT P; switch(a.LParam) { case 515 : Show(); SetForegroundWindow(Handle); break; case 516 : GetCursorPos(&P); PopupMenu1->Popup(P.x,P.y); } } //--------------------------------------------------------------------------- void __fastcall TFMain::FormCloseQuery(TObject *Sender, bool &CanClose) { if(Visible == true) { TRegistry *reg = new TRegistry; AnsiString CloseFlag; reg->RootKey = HKEY_LOCAL_MACHINE; reg->OpenKey("SOFTWARE\\Maximus\\HotKey\\AppOptions",true); if(! reg->ValueExists("CloseForm")) { reg->WriteString("ShowForm","1"); reg->WriteString("CloseForm","0"); CloseFlag = "1"; } else CloseFlag = reg->ReadString("ShowForm"); if(CloseFlag == "1") { Application->CreateForm(__classid(TFClose), &FClose); switch(FClose->ShowModal()) { case 2 : CanClose = false; break; case 11 : break; case 12 : CanClose = false; Visible = false; } } else if(reg->ReadString("CloseForm") == 0) { CanClose = false; Visible = false; } } } //--------------------------------------------------------------------------- void __fastcall TFMain::MCloseClick(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- void __fastcall TFMain::MOptionsClick(TObject *Sender) { Visible = true; } //--------------------------------------------------------------------------- void __fastcall TFMain::ApplicationEvents1ShowHint(AnsiString &HintStr, bool &CanShow, THintInfo &HintInfo) { if(HintInfo.HintControl->Name == "Edit1" && Canvas->TextWidth(Edit1->Text) > Edit1->ClientWidth) HintStr = Edit1->Text; ApplicationEvents1->CancelDispatch(); } //--------------------------------------------------------------------------- UChange.h //--------------------------------------------------------------------------- #ifndef UChangeH #define UChangeH //--------------------------------------------------------------------------- #include #include #include #include #include #include #include //--------------------------------------------------------------------------- class TFChange : public TForm { __published: // IDE-managed Components TGroupBox *GroupBox1; TLabel *Label1; TEdit *Edit1; TLabel *Label2; TEdit *Edit2; TButton *BFind; TLabel *Label3; THotKey *HotKey1; TOpenDialog *OpenDialog1; TButton *BOK; TButton *BCancel; TApplicationEvents *ApplicationEvents1; void __fastcall BOKClick(TObject *Sender); void __fastcall BCancelClick(TObject *Sender); void __fastcall BFindClick(TObject *Sender); void __fastcall FormShow(TObject *Sender); void __fastcall ApplicationEvents1ShowHint(AnsiString &HintStr, bool &CanShow, THintInfo &HintInfo); private: // User declarations public: // User declarations __fastcall TFChange(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TFChange *FChange; //--------------------------------------------------------------------------- #endif //--------------------------------------------------------------------------- UChange.cpp //--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "UChange.h" #include "UHotKey.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TFChange *FChange; //--------------------------------------------------------------------------- __fastcall TFChange::TFChange(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TFChange::BOKClick(TObject *Sender) { AnsiString S; if(HotKey1->HotKey == 0) { ShowMessage("Вы не задали комбинацию горячих клавиш"); return; } S = ShortCutToText(HotKey1->HotKey); if((Caption == "Добавить") || ((Caption == "Изменить") && (S != FMain->HKValues->Strings[FMain->ListBox1->ItemIndex]))) if(FMain->HKValues->IndexOf(S) != -1) { ShowMessage("Заданная комбинация горячих клавиш уже зарегестрирована"); return; } if(Edit2->Text == "") { ShowMessage("Вы не задали пути к файлу"); return; } if(! FileExists(Edit2->Text)) { ShowMessage("Неверно указан путь к запускаемому файлу"); return; } S = Edit2->Text; if((Caption == "Добавить") || ((Caption == "Изменить") && (S != FMain->HKProgs->Strings[FMain->ListBox1->ItemIndex]))) if(FMain->HKProgs->IndexOf(S) != -1) { ShowMessage("Для указанного файла данная комбинация" "горячих клавиш уже зарегестрирована"); return; } if(Edit1->Text == "") { ShowMessage("Вы не задали имя запускаемого действия"); return; } S = Edit1->Text; if((Caption == "Добавить") || ((Caption == "Изменить") && (S != FMain->ListBox1->Items->Strings[FMain->ListBox1->ItemIndex]))) if(FMain->ListBox1->Items->IndexOf(S) != -1) { ShowMessage("Для указанного имени данная комбинация" "горячих клавиш уже зарегестрирована"); return; } TRegistry *reg = new TRegistry; if(Caption == "Добавить") { FMain->ListBox1->Items->Add(Edit1->Text); FMain->HKProgs->Add(Edit2->Text); reg->RootKey = HKEY_LOCAL_MACHINE; reg->OpenKey("SOFTWARE\\Maximus\\HotKey",false); reg->WriteString(Edit2->Text + "|" + Edit1->Text, ShortCutToText(HotKey1->HotKey)); reg->CloseKey(); reg->Free(); FMain->RegHotKey(FMain->HKValues->Add(ShortCutToText(HotKey1->HotKey))); } else { UnregisterHotKey(FMain->Handle, (Word)(FMain->HKValues->Objects[FMain->ListBox1->ItemIndex])); reg->RootKey = HKEY_LOCAL_MACHINE; reg->OpenKey("SOFTWARE\\Maximus\\HotKey",false); reg->DeleteValue(FMain->HKProgs->Strings[FMain->ListBox1->ItemIndex] + "|" + FMain->ListBox1->Items->Strings[FMain->ListBox1->ItemIndex]); FMain->ListBox1->Items->Strings[FMain->ListBox1->ItemIndex] = Edit1->Text; FMain->HKProgs->Strings[FMain->ListBox1->ItemIndex] = Edit2->Text; reg->WriteString(Edit2->Text + "|" + Edit1->Text, ShortCutToText(HotKey1->HotKey)); reg->CloseKey(); reg->Free(); FMain->HKValues->Strings[FMain->ListBox1->ItemIndex] = ShortCutToText(HotKey1->HotKey); FMain->RegHotKey(FMain->ListBox1->ItemIndex); } ModalResult = 1; } //--------------------------------------------------------------------------- void __fastcall TFChange::BCancelClick(TObject *Sender) { ModalResult = 2; } //--------------------------------------------------------------------------- void __fastcall TFChange::BFindClick(TObject *Sender) { if(OpenDialog1->Execute()) Edit2->Text = OpenDialog1->FileName; } //--------------------------------------------------------------------------- void __fastcall TFChange::FormShow(TObject *Sender) { if(Caption == "Добавить") { Edit1->Text = ""; Edit2->Text = ""; HotKey1->HotKey = 0; } else { Edit1->Text = FMain->ListBox1->Items->Strings[FMain->ListBox1->ItemIndex]; Edit2->Text = FMain->HKProgs->Strings[FMain->ListBox1->ItemIndex]; HotKey1->HotKey = TextToShortCut(FMain->HKValues-> Strings[FMain->ListBox1->ItemIndex]); } } //--------------------------------------------------------------------------- void __fastcall TFChange::ApplicationEvents1ShowHint(AnsiString &HintStr, bool &CanShow, THintInfo &HintInfo) { if(HintInfo.HintControl->Name == "Edit2" && Canvas->TextWidth(Edit2->Text) > Edit2->ClientWidth) HintStr = Edit2->Text; ApplicationEvents1->CancelDispatch(); } //--------------------------------------------------------------------------- UClose.h //--------------------------------------------------------------------------- #ifndef UCloseH #define UCloseH //--------------------------------------------------------------------------- #include #include #include #include //--------------------------------------------------------------------------- class TFClose : public TForm { __published: // IDE-managed Components TGroupBox *GroupBox1; TLabel *Label1; TRadioButton *RadioButton1; TRadioButton *RadioButton2; TCheckBox *CheckBox1; TButton *BOK; TButton *BCancel; void __fastcall BOKClick(TObject *Sender); void __fastcall BCancelClick(TObject *Sender); void __fastcall FormClose(TObject *Sender, TCloseAction &Action); private: // User declarations public: // User declarations __fastcall TFClose(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TFClose *FClose; //--------------------------------------------------------------------------- #endif //--------------------------------------------------------------------------- UClose.cpp //--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "UClose.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TFClose *FClose; //--------------------------------------------------------------------------- __fastcall TFClose::TFClose(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TFClose::BOKClick(TObject *Sender) { if(RadioButton1->Checked == true) ModalResult = 11; else ModalResult = 12; } //--------------------------------------------------------------------------- void __fastcall TFClose::BCancelClick(TObject *Sender) { ModalResult = 2; } //--------------------------------------------------------------------------- void __fastcall TFClose::FormClose(TObject *Sender, TCloseAction &Action) { TRegistry *reg = new TRegistry; reg->RootKey = HKEY_LOCAL_MACHINE; reg->OpenKey("SOFTWARE\\Maximus\\HotKey\\AppOptions",false); if(CheckBox1->Checked == true) reg->WriteString("ShowForm","0"); else reg->WriteString("ShowForm","1"); if(RadioButton1->Checked == true) reg->WriteString("CloseForm","1"); else reg->WriteString("CloseForm","0"); } //---------------------------------------------------------------------------