?? main.cpp
字號:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <jpeg.hpp>
#pragma hdrstop
#include <stdio.h>
#include "Main.h"
#include "Config.h"
#include "About.h"
#include "Socket.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
#define CM_ENTIRESCREEN 201 //捕獲全屏
#define CM_ACTIVEWINDOW 202 //捕獲當前窗口
#define CM_TCPIPMSG (WM_APP+1000)
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent *Owner)
: TForm(Owner)
{
Application->OnMinimize = Mini;
}
void __fastcall TMainForm::Mini(TObject *Sender)
{
//HWND hWnd = Application->Handle;
//SendMessage(hWnd,WM_SYSCOMMAND,SW_HIDE,0 );
Hide();
}
//---------------------------------------------------------------------------
/*
// 捕獲當前屏幕并保存到imagestream中
void CaptureImage(int options, int level, int cq, TMemoryStream* imgstream)
{
LONG width,height;
RECT capRect;
HDC DesktopDC;
switch (options) {
case CM_ENTIRESCREEN: // 捕獲整個屏幕
// 取得桌面的矩形區域范圍
GetWindowRect(GetDesktopWindow(),&capRect);
break;
case CM_ACTIVEWINDOW: // 捕獲當前窗口
HWND ForegWin;
ForegWin = GetForegroundWindow(); // 取得當前窗口句柄
if (!ForegWin)
ForegWin = GetDesktopWindow();
GetWindowRect(ForegWin,&capRect); // 取得當前窗口的矩形區域范圍
break;
}
DesktopDC = GetDC(GetDesktopWindow()); // 創建內存設備描述表
width = capRect.right - capRect.left;
height = capRect.bottom - capRect.top;
Graphics::TBitmap *bBitmap; // 定義位圖變量
try {
bBitmap = new Graphics::TBitmap(); // 創建位圖
bBitmap->Width=width;
bBitmap->Height=height;
if ((level>0)&&(level<8))
bBitmap->PixelFormat = TPixelFormat(level); // 設定色深
// 拷貝屏幕的指定區域到位圖
BitBlt(bBitmap->Canvas->Handle,0,0,width,height,DesktopDC,
capRect.left,capRect.top,SRCCOPY);
if (cq>=0) {
TJPEGImage *jpeg;
try {
jpeg = new TJPEGImage; // 創建JPEG圖象
jpeg->CompressionQuality = cq; // 設定圖象品質
jpeg->Assign(bBitmap); // 將位圖轉化為JPEG格式
jpeg->SaveToStream(imgstream); // 保存JPEG圖象信息
}
__finally {
delete jpeg; // 釋放資源
}
}
else {
bBitmap->SaveToStream(imgstream); // 保存位圖信息
}
}
__finally {
delete bBitmap; // 釋放資源
}
}
*/
// 新建觀察窗口
void __fastcall TMainForm::CreateMDIChild1(AnsiString sAddress, int CL, int CQ)
{
TMDIChild1 *Child1;
//--- create a new MDI child window ----
Child1 = new TMDIChild1(Application);
Child1->RemoteAddress = sAddress; // 主機IP
Child1->CL = CL; // 色深
Child1->CQ = CQ; // 品質
// int sock=Connect_Server(sAddress,LISTENPORT); // RemoteAddress, LISTENPORT);
// Write_Socket(sock, "2\n");
// AnsiString read=Socket_Readln(sock);
// ShowMessage(read);
Child1->Caption = "來自'" + sAddress+"'";
Child1->Capture(); // 捕獲第一屏
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FileNew1Execute(TObject *Sender)
{
ConfigForm->ShowModal();
if (ConfigForm->Execute) {
// 新建觀察窗口
CreateMDIChild1(ConfigForm->ComboBox1->Text, // 主機IP
ConfigForm->TrackBar1->Position, // 色深
ConfigForm->TrackBar2->Position*10); // 品質
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::HelpAbout1Execute(TObject *Sender)
{
AboutBox->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FileExit1Execute(TObject *Sender)
{ TrayMessage(NIM_DELETE); //發送圖標變換消息
Close();
Application->Terminate ();
}
//---------------------------------------------------------------------------
/*// 捕獲并發送自己的屏幕圖象
void __fastcall TMainForm::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
AnsiString sRecvString = Socket->ReceiveText(); // 保存接收到的字符串
AnsiString sRemoteAddress = Socket->RemoteAddress; // 保存對方IP
int CL,CQ;
u_short port;
int CmdType;
// 將接收到的字符串分解為命令類別、接收端口、色深、品質4個參數
int pos = sRecvString.Pos("\n");
// 命令類別
// 1:請求圖象發送(附加參數:接收端口、色深、品質)
// 2:請求目標機屏幕分辨率(附加參數:無)
// 3:接收鼠標信息(附加參數:鼠標命令、X坐標、Y坐標)
// 4:
CmdType=StrToIntDef(sRecvString.SubString(1,pos-1),0);
sRecvString = sRecvString.SubString(pos+1,sRecvString.Length()-pos);
pos = sRecvString.Pos("\n");
switch(CmdType)
{
case 1://請求圖象發送
// 接收端口
port = u_short(StrToIntDef(sRecvString.SubString(1,pos-1),0));
sRecvString = sRecvString.SubString(pos+1,sRecvString.Length()-pos);
pos = sRecvString.Pos("\n");
// 色深
CL = StrToIntDef(sRecvString.SubString(1,pos-1),0);
sRecvString = sRecvString.SubString(pos+1,sRecvString.Length()-pos);
pos = sRecvString.Pos("\n");
// 品質
CQ = StrToIntDef(sRecvString.SubString(1,pos-1),0);
if (port) {
TMemoryStream *ImageStream; // 定義數據流
try {
ImageStream = new TMemoryStream; // 分配內存
// 捕獲當前屏幕并保存到ImageStream中
CaptureImage(CM_ENTIRESCREEN, CL, CQ, ImageStream);
// 發送ImageStream到接收端口
if (!SendStream(sRemoteAddress, port, ImageStream))
MessageBox(0,"發送數據流失敗","鴨嘴獸提示",MB_ICONERROR);
}
__finally {
delete ImageStream; // 釋放資源
}
}
break;
case 2://請求目標機屏幕分辨率
{
char str[24];
int Width=GetSystemMetrics(SM_CXSCREEN);
int Height=GetSystemMetrics(SM_CYSCREEN);
sprintf(str,"%d*%d\n",Width,Height);
Socket->SendText(str);
}
break;
case 3://
{
int MouseCmd=StrToIntDef(sRecvString.SubString(1,pos-1),0);
sRecvString = sRecvString.SubString(pos+1,sRecvString.Length()-pos);
pos = sRecvString.Pos("\n");
int px=StrToIntDef(sRecvString.SubString(1,pos-1),0);
sRecvString = sRecvString.SubString(pos+1,sRecvString.Length()-pos);
pos = sRecvString.Pos("\n");
int py=StrToIntDef(sRecvString.SubString(1,pos-1),0);
switch(MouseCmd)
{
case 1://WM_MOUSEMOVE
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE,px,py,0,0);
break;
case 2://WM_LBUTTONDOWN
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,px,py,0,0);
break;
case 3://WM_LBUTTONUP
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP,px,py,0,0);
break;
case 4://WM_RBUTTONDOWN
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTDOWN,px,py,0,0);
break;
case 5://WM_RBUTTONUP
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTUP,px,py,0,0);
break;
case 6://WM_MIDBUTTONDOWN
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MIDDLEDOWN,px,py,0,0);
break;
case 7://WM_MIDBUTTONUP
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MIDDLEUP,px,py,0,0);
break;
default:
break;
}
}
break;
default://
break;
}
}
//---------------------------------------------------------------------------
*/
/*void __fastcall TMainForm::ServerSocket1ClientError(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
ErrorCode = 0;
MessageBox(0,"遠程連接出錯","鴨嘴獸提示",MB_ICONERROR); // 顯示出錯信息
}
//---------------------------------------------------------------------------
*/
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
// ServerSocket1->Port = LISTENPORT; // 指定監聽端口
// ServerSocket1->Open(); // 開始監聽
TrayMessage(NIM_ADD);
TrayMessage(NIM_MODIFY);
Ns=1;
Application->OnMinimize = Mini;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::N2Click(TObject *Sender)
{
// 是否打開端口允許別人抓屏
// N2->Checked = !N2->Checked;
// ServerSocket1->Active = N2->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FileCloseItemClick(TObject *Sender)
{
TrayMessage(NIM_DELETE); //發送圖標變換消息
Close();
}
//---------------------------------------------------------------------------
bool __fastcall TMainForm::TrayMessage(DWORD dwMessage)
{
NOTIFYICONDATA tnd;
PSTR pszTip;
pszTip = "鴨嘴獸遠程監視器";//TipText();
tnd.cbSize= sizeof(NOTIFYICONDATA); //結構的大小
tnd.uCallbackMessage = CM_TCPIPMSG; //自定義回調消息,在頭文件中聲明
tnd.hWnd= Handle; //接受回調消息的窗口句柄
lstrcpyn(tnd.szTip,pszTip,sizeof(tnd.szTip));
tnd.uID = CM_TCPIPMSG; //圖標標志號
tnd.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP; //指定以下三個參數哪個包含有效數據
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 TMainForm::IconHandle(void)
{
if (Ns==1)
{
return (Image1->Picture->Icon->Handle);
//n是全局變量,1為顯示Image1,0為Image2
}
else
{
return (Image2->Picture->Icon->Handle);
}
}
//--------------------------------------------------------------------------
void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
if (Ns==1)//n為圖標句柄鎖,是全局變量, 1為顯示Image1,0為Image2
{
Ns=0;
}
else
{
Ns=1;
}
TrayMessage(NIM_MODIFY); //發送圖標變換消息
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::TcpipMan( TMessage &Msg )
{
// CTcpsrServer *v1file;
// TCommand *v1cmd;
int new_sockid;
/**********************************************************/
POINT MousePos;
switch(Msg.LParam)
{
case WM_RBUTTONUP:
if (GetCursorPos(&MousePos))
{ //PopupMenu1->PopupComponent = Fmadm;
// MainMenu1->PopulateOle2Menu = MainForm;
SetForegroundWindow(Handle);
// PopupMenu1->Popup(MousePos.x, MousePos.y);
// MainMenu1->Main(MousePos.x, MousePos.y);
}
break;
case WM_LBUTTONDOWN:
Show();
break;
default:
break;
}
}
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
TrayMessage(NIM_DELETE);
}
//---------------------------------------------------------------------------
/*
void __fastcall TMainForm::FormKeyUp(TObject *Sender, WORD &Key,
TShiftState Shift)
{ char text[32];
sprintf(text,"4\n%c\n",Key);
int sock=Connect_Server(RemoteAddress, LISTENPORT); //連接
Write_Socket(sock, text); //發送
closesocket(sock);
}
*///---------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -