📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <Registry.hpp>
#include <shellapi.h>
#include "Unit1.h"
#include "Unit2.h"
#include "winsock.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void RegProgram()
{
TRegistry *reg=new TRegistry();
reg->RootKey=HKEY_LOCAL_MACHINE;
reg->OpenKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",true);
bool flag;
flag = reg->ValueExists("NetQQ");
if(!flag)
{
reg->WriteString("NetQQ",Application->ExeName);
}
reg->CloseKey();
delete reg;
}
//---------------------------------------------------------------------------
AnsiString gethostip(AnsiString &host)
{
WSADATA wsaData;
AnsiString IP;
char temp[255];
strcpy(temp,host.c_str());
int pos=0;
while(temp[pos++]=='\\');
pos--;
// temp+=i-1;
WSAStartup(MAKEWORD(2,0),&wsaData);
if(host.IsEmpty())
{
char hostname[128];
if(gethostname(hostname,128)!=0)
return AnsiString("");
host=hostname;
}
try
{
struct hostent *hp=gethostbyname(temp+pos);//host.c_str());
if(hp == NULL)
{
IP="";
}
else
{
IP=inet_ntoa(*(struct in_addr*)hp-> h_addr_list[0]);
}
}
catch(...)
{
IP="";
}
WSACleanup();
return IP;
}
//---------------------------------------------------------------------------
bool EnumNetResource(LPNETRESOURCE lpNR,DWORD dwScope,DWORD dwType)
{
HANDLE hEnum = 0;
DWORD dwResult = WNetOpenEnum(
dwScope, // scope of enumeration
dwType, // resource types to list
0, // enumerate all resources
lpNR, // pointer to resource structure (NULL at first time)
&hEnum // handle to resource
);
if(dwResult != NO_ERROR) return false;
bool bRet=true;
DWORD dwEntries = 0xFFFFFFFF; // enumerate all possible entries
NETRESOURCE NR[1024];
DWORD dwBuffer=1024*sizeof(NETRESOURCE);
while(1)
{
dwResult = WNetEnumResource(hEnum, // resource-handle
&dwEntries,
(LPVOID)NR,
&dwBuffer
);
if(dwResult == ERROR_NO_MORE_ITEMS)
{
break;
}
else
{
if(dwResult != NO_ERROR)
{
bRet=false;
break;
}
}
for(DWORD i=0;i<dwEntries;i++)
{
if(NR[i].dwDisplayType==RESOURCEDISPLAYTYPE_SERVER)
{
char *p=NR[i].lpRemoteName;
while(*p=='\\') p++;
if(*p)
{
AnsiString IP = gethostip(AnsiString(p));
Form1->ListBox1->Items->Add(p);
Form1->ListBox2->Items->Add(IP);
}
}
else
{
if((NR[i].dwUsage& RESOURCEUSAGE_CONTAINER)==RESOURCEUSAGE_CONTAINER)
{
bRet=EnumNetResource(&NR[i],dwScope,dwType);
if(bRet==false) break;
}
}
}
if(bRet==false) break;
}
WNetCloseEnum(hEnum) ;
return bRet;
}
void __fastcall TForm1::RefreshUsers()
{
ListBox1->Clear();
ListBox2->Clear();
Screen->Cursor=crHourGlass;
EnumNetResource(NULL,RESOURCE_GLOBALNET,RESOURCETYPE_ANY);
Screen->Cursor=crDefault;
}
void __fastcall TForm1::ListBox1Click(TObject *Sender)
{
if(ListBox2->Items->Strings[ListBox1->ItemIndex]!="")
ListBox1->Hint = ListBox2->Items->Strings[ListBox1->ItemIndex];
else
ListBox1->Hint = "Off line";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::a1Click(TObject *Sender)
{
RefreshUsers();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1DblClick(TObject *Sender)
{
Form2->Show();
Form2->Label2->Caption = ListBox1->Items->Strings[ListBox1->ItemIndex];
}
//---------------------------------------------------------------------------
void __fastcall TForm1::AddTrayIcon()
{
NOTIFYICONDATA icondata;
memset(&icondata,0,sizeof(icondata));
icondata.cbSize=sizeof(icondata);
icondata.hWnd=Handle;
strncpy(icondata.szTip,"NetQQ",sizeof(icondata.szTip));
icondata.hIcon=Application->Icon->Handle;
icondata.uCallbackMessage=iconmessage;
icondata.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
Shell_NotifyIcon(NIM_ADD,&icondata);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ModifyIcon(HANDLE hd,AnsiString s)
{
NOTIFYICONDATA icondata;
memset(&icondata,0,sizeof(icondata));
icondata.cbSize=sizeof(icondata);
icondata.hWnd=Handle;
strncpy(icondata.szTip,s.c_str(),sizeof(icondata.szTip));
icondata.hIcon=hd;
icondata.uCallbackMessage=iconmessage;
icondata.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
Shell_NotifyIcon(NIM_MODIFY,&icondata);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DeleteTrayIcon()
{
NOTIFYICONDATA icondata;
memset(&icondata,0,sizeof(icondata));
icondata.cbSize=sizeof(icondata);
icondata.hWnd=Handle;
Shell_NotifyIcon(NIM_DELETE,&icondata);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WndProc(Messages::TMessage& Message)
{
if(Message.Msg == iconmessage)
{
if(Message.LParam == WM_LBUTTONDBLCLK)
{
Form1->Visible = true;
Application->Restore();
ShowWindow(Application->Handle ,SW_SHOW);
SetWindowPos(Form1->Handle,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
ModifyIcon(Application->Icon->Handle,"NetQQ");
Form2->Timer1->Enabled = false;
}
if(Message.LParam == WM_RBUTTONDOWN)
{
TPoint p;
GetCursorPos(&p);
Form1->PopupMenu1->Popup(p.x,p.y);
}
}
if(Message.WParamLo == SC_MINIMIZE || Message.WParam == SC_CLOSE)
{
Application->Minimize();
ShowWindow(Application->Handle,SW_HIDE);
Form1->Visible = false;
Form2->Close();
}
else
{
Dispatch(&Message);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
DeleteTrayIcon();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
iconmessage = RegisterWindowMessage("IconNotify");
AddTrayIcon();
RefreshUsers();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ExitClick(TObject *Sender)
{
Form1->Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ShowClick(TObject *Sender)
{
Application->Restore();
ShowWindow(Application->Handle ,SW_SHOW);
Form1->Visible = true;
Form2->Timer1->Enabled = false;
ModifyIcon(Application->Icon->Handle,"NetQQ");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HideClick(TObject *Sender)
{
Application->Minimize();
ShowWindow(Application->Handle,SW_HIDE);
Form1->Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::AutoRunClick(TObject *Sender)
{
AutoRun->Checked = !AutoRun->Checked ;
if(!AutoRun->Checked)
{
TRegistry *reg=new TRegistry();
reg->RootKey=HKEY_LOCAL_MACHINE;
reg->OpenKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",true);
bool flag;
flag = reg->ValueExists("NetQQ");
if(flag)
{
reg->DeleteValue("NetQQ");
}
reg->CloseKey();
delete reg;
}
else
RegProgram();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Hide1Click(TObject *Sender)
{
Application->Minimize();
ShowWindow(Application->Handle,SW_HIDE);
Form1->Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::AutoOpen1Click(TObject *Sender)
{
AutoOpen1->Checked = !AutoOpen1->Checked;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -