📄 vncmenu.cpp
字号:
// Copyright (C) 2006 Teamviewer GmbH. All Rights Reserved.
// Copyright (C) 2002 Ultr@VNC Team Members. All Rights Reserved.
// Copyright (C) 2002 RealVNC Ltd. All Rights Reserved.
// Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
//
// This file is part of TeamViewer.
//
// TeamViewer is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
//
// If the source code for TeamViewer is not available from the place
// whence you received this file, check http://www.teamviewer.com
// for information on obtaining it.
// vncMenu
// Implementation of a system tray icon & menu for WinVNC
#include "stdhdrs.h"
#include "vncService.h"
#include "Localization.h"
#include "vncMenu.h"
#include "HideDesktop.h"
#include "..\..\vncViewer\SessionDialog.h"
#include "..\..\vncviewer\vncviewer.h"
#include "winvnc.h"
#include "vncTimedMsgBox.h"
// Constants
const UINT MENU_PROPERTIES_SHOW = RegisterWindowMessage("WinVNC.Properties.User.Show");
const UINT MENU_DEFAULT_PROPERTIES_SHOW = RegisterWindowMessage("WinVNC.Properties.Default.Show");
const UINT MENU_ABOUTBOX_SHOW = RegisterWindowMessage("WinVNC.AboutBox.Show");
const UINT MENU_SERVICEHELPER_MSG = RegisterWindowMessage("WinVNC.ServiceHelper.Message");
const UINT MENU_ADD_CLIENT_MSG = RegisterWindowMessage("WinVNC.AddClient.Message");
const UINT MENU_REMOVE_CLIENTS_MSG = RegisterWindowMessage("WinVNC.RemoveClients.Message"); // REalVNc 336
//const UINT FileTransferSendPacketMessage = RegisterWindowMessage("UltraVNC.Viewer.FileTransferSendPacketMessage");
const char *MENU_CLASS_NAME = "TeamViewer Tray Icon";
BOOL g_restore_ActiveDesktop = FALSE;
bool RunningAsAdministrator ();
extern bool isQuickSupport;
extern bool isRemoteControl;
vncMenu *menu=NULL;
static void
KillWallpaper()
{
HideDesktop();
}
static void
RestoreWallpaper()
{
RestoreDesktop();
}
#define DOUBLECLICK_TIMER 2
// Implementation
vncMenu::vncMenu(vncServer *server)
{
hWtsLib2 = NULL;
ports_set=false;
CoInitialize(0);
// Save the server pointer
m_server = server;
memset(&m_nid, 0, sizeof(m_nid));
if(hUser32 = LoadLibrary("user32.dll"))
m_SetMenuInfo = (SetMenuInfoFn)GetProcAddress(hUser32, "SetMenuInfo");
else
m_SetMenuInfo = NULL;
}
void vncMenu::Init()
{
// Set the initial user name to something sensible...
vncService::CurrentUser((char *)&m_username, sizeof(m_username));
vnclog.Print(LL_INTINFO, VNCLOG("username = %s"), m_username);
// Create a dummy window to handle tray icon messages
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = 0;
wndclass.lpfnWndProc = vncMenu::WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hAppInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = (const char *) NULL;
wndclass.lpszClassName = MENU_CLASS_NAME;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wndclass);
m_hwnd = CreateWindow(MENU_CLASS_NAME,
MENU_CLASS_NAME,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
200, 200,
NULL,
NULL,
hAppInstance,
NULL);
if (m_hwnd == NULL)
{
PostQuitMessage(0);
return;
}
if (!hWtsLib2) hWtsLib2 = LoadLibrary( ("wtsapi32.dll") );
if (hWtsLib2)
{
WTSREGISTERSESSIONNOTIFICATION fnWtsRegisterSessionNotification;
fnWtsRegisterSessionNotification = (WTSREGISTERSESSIONNOTIFICATION)GetProcAddress( (HINSTANCE)hWtsLib2, "WTSRegisterSessionNotification" );
if (fnWtsRegisterSessionNotification)
{
fnWtsRegisterSessionNotification( m_hwnd, NOTIFY_FOR_THIS_SESSION );
}
}
// record which client created this window
SetWindowLong(m_hwnd, GWL_USERDATA, (LONG) this);
// Ask the server object to notify us of stuff
m_server->AddNotify(m_hwnd);
// Initialise the properties dialog object
if (!m_properties.Init(m_server))
{
PostQuitMessage(0);
return;
}
#ifndef ROVNCTINY
// Only enable the timer if the tray icon will be displayed.
if ( ! m_server->GetDisableTrayIcon())
{
// Timer to trigger icon updating
SetTimer(m_hwnd, 1, 5000, NULL);
}
// sf@2002
if (!m_ListDlg.Init(m_server))
{
PostQuitMessage(0);
return;
}
#endif
// Load the icons for the tray
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(osvi);
GetVersionEx(&osvi);
if (osvi.dwPlatformId==VER_PLATFORM_WIN32_NT &&
(osvi.dwMajorVersion==5 && osvi.dwMinorVersion>=1 || // XP/2003
osvi.dwMajorVersion>=6) // Vista
)
{ // only XP and 2003 know 256 color icons
m_winvnc_icon=(HICON)LoadImage(hAppInstance, MAKEINTRESOURCE(IDI_WINVNC), IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
m_flash_icon= (HICON)LoadImage(hAppInstance, MAKEINTRESOURCE(IDI_FLASH), IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
}
else
{
m_winvnc_icon=(HICON)LoadImage(hAppInstance, MAKEINTRESOURCE(IDI_WINVNC_W2K), IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON), LR_VGACOLOR);
m_flash_icon= (HICON)LoadImage(hAppInstance, MAKEINTRESOURCE(IDI_WINVNC_W2K), IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON), LR_VGACOLOR);
}
// Load the popup menu
m_hmenu = LoadMenu(hAppInstance, MAKEINTRESOURCE(IDR_TRAYMENU));
if(isQuickSupport) // Staudenmeyer@2005
{
m_server->SetDisableTrayIcon(true); // has to be after loading the properties, because otherwise this setting would be overridden
}
// Install the tray icon!
AddTrayIcon();
}
vncMenu::~vncMenu()
{
if (hWtsLib2)
{
WTSUNREGISTERSESSIONNOTIFICATION fnWtsUnRegisterSessionNotification;
fnWtsUnRegisterSessionNotification = (WTSUNREGISTERSESSIONNOTIFICATION)GetProcAddress((HINSTANCE)hWtsLib2,"WTSUnRegisterSessionNotification" );
if (fnWtsUnRegisterSessionNotification)
{
fnWtsUnRegisterSessionNotification( m_hwnd );
}
FreeLibrary( hWtsLib2 );
hWtsLib2 = NULL;
}
// Remove the tray icon
DelTrayIcon();
// Destroy the loaded menu
if (m_hmenu != NULL)
DestroyMenu(m_hmenu);
// Tell the server to stop notifying us!
if (m_server != NULL)
{
m_server->RemNotify(m_hwnd);
if (m_server->RemoveWallpaperEnabled())
RestoreWallpaper();
}
if(hUser32)
FreeLibrary(hUser32);
}
void
vncMenu::AddTrayIcon()
{
// If the user name is non-null then we have a user!
if (strcmp(m_username, "") != 0)
{
// Make sure the server has not been configured to
// suppress the tray icon.
if ( ! m_server->GetDisableTrayIcon())
{
SendTrayMsg(NIM_ADD, FALSE);
}
}
}
void
vncMenu::DelTrayIcon()
{
SendTrayMsg(NIM_DELETE, FALSE);
}
void
vncMenu::FlashTrayIcon(BOOL flash)
{
SendTrayMsg(NIM_MODIFY, flash);
}
// Get the local ip addresses as a human-readable string.
// If more than one, then with \n between them.
// If not available, then gets a message to that effect.
void GetIPAddrString(char *buffer, int buflen) {
char namebuf[256];
CroGateway *rgw = CroGateway::Instance();
*buffer = '\0';
// First add the gateway磗 ClientID if available
if (rgw->ClientID()[0]!='0')
{
// first add the gateway磗 Client ID if available
sprintf(buffer,"%s, ",rgw->ClientID());
}
// Then add the IPs
if (gethostname(namebuf, 256) != 0) {
if (rgw->ClientID()[0]=='0')
strncpy(buffer, "Host name unavailable", buflen);
return;
};
HOSTENT *ph = gethostbyname(namebuf);
if (!ph) {
if (rgw->ClientID()[0]=='0')
strncpy(buffer, "IP address unavailable", buflen);
return;
};
char digtxt[5];
for (int i = 0; ph->h_addr_list[i]; i++) {
for (int j = 0; j < ph->h_length; j++) {
sprintf(digtxt, "%d.", (unsigned char) ph->h_addr_list[i][j]);
strncat(buffer, digtxt, (buflen-1)-strlen(buffer));
}
buffer[strlen(buffer)-1] = '\0';
if (ph->h_addr_list[i+1] != 0)
strncat(buffer, ", ", (buflen-1)-strlen(buffer));
}
}
void
vncMenu::ShowBalloonTip(char *title, char *msg, int timeoutmsecs, DWORD dwInfoFlags /*= NIIF_INFO */)
{
m_nid.cbSize=sizeof(NOTIFYICONDATA);
m_nid.uFlags = NIF_INFO;
m_nid.uTimeout = timeoutmsecs;
m_nid.dwInfoFlags = dwInfoFlags;
strcpy(m_nid.szInfo,_T(msg));
strcpy(m_nid.szInfoTitle,_T(title));
Shell_NotifyIcon(NIM_MODIFY, &m_nid);
}
void
vncMenu::SendTrayMsg(DWORD msg, BOOL flash)
{
// Create the tray icon message
m_nid.hWnd = m_hwnd;
m_nid.cbSize = sizeof(m_nid);
m_nid.uID = IDI_WINVNC; // never changes after construction
m_nid.hIcon = flash ? m_flash_icon : m_winvnc_icon;
m_nid.uFlags = NIF_ICON | NIF_MESSAGE;
m_nid.uCallbackMessage = WM_TRAYNOTIFY;
//vnclog.Print(LL_INTINFO, VNCLOG("SendTRaymesg"));
// Use resource string as tip if there is one
if (LoadString(hAppInstance, IDI_WINVNC, m_nid.szTip, sizeof(m_nid.szTip)))
{
m_nid.uFlags |= NIF_TIP;
}
// Try to add the server's IP addresses to the tip string, if possible
if (m_nid.uFlags & NIF_TIP)
{
strncat(m_nid.szTip, " - ", (sizeof(m_nid.szTip)-1)-strlen(m_nid.szTip));
if (m_server->SockConnected())
{
unsigned long tiplen = strlen(m_nid.szTip);
char *tipptr = ((char *)&m_nid.szTip) + tiplen;
GetIPAddrString(tipptr, sizeof(m_nid.szTip) - tiplen);
}
else
{
strncat(m_nid.szTip, "Not listening", (sizeof(m_nid.szTip)-1)-strlen(m_nid.szTip));
}
}
// Send the message
bool res = Shell_NotifyIcon(msg, &m_nid);
if (res)
{
// Set the enabled/disabled state of the menu items
// vnclog.Print(LL_INTINFO, VNCLOG("tray icon added ok"));
/* EnableMenuItem(m_hmenu, ID_ADMIN_PROPERTIES,
m_properties.AllowProperties() && RunningAsAdministrator() ? MF_ENABLED : MF_GRAYED); */
//EnableMenuItem(m_hmenu, ID_PROPERTIES,
//m_properties.AllowProperties() && RunningAsAdministrator() ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(m_hmenu, ID_CLOSE,
m_properties.AllowShutdown() ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(m_hmenu, ID_KILLCLIENTS,
m_properties.AllowEditClients() ? MF_ENABLED : MF_GRAYED);
//EnableMenuItem(m_hmenu, ID_OUTGOING_CONN,
//m_properties.AllowEditClients() ? MF_ENABLED : MF_GRAYED);
} else {
if (!vncService::RunningAsService())
{
if (msg == NIM_ADD)
{
// The tray icon couldn't be created, so use the Properties dialog
// as the main program window
vnclog.Print(LL_INTINFO, VNCLOG("The tray icon couldn't be created"));
// PostQuitMessage(0);
}
}
}
}
// Process window messages
LRESULT CALLBACK vncMenu::WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
static bool waitingForDoubleClick=false;
static POINT lastLeftClickPosition={0,0};
static UINT s_uTaskbarRestart;
char Setupname[128];
char Server[256]="download.teamviewer.com";
char updateUrl[256]="/download/getfile.aspx?file="; // stream file instead of download: no file extension
FILE *outfile;
bool success;
DWORD dwErrorCode;
DWORD dwError;
HINTERNET ineturl;
HINTERNET inetconn;
HINTERNET inetsession;
string sAppPath;
int count = 0;
CroGateway *rgw = CroGateway::Instance();
// This is a static method, so we don't know which instantiation we're
// dealing with. We use Allen Hadden's (ahadden@taratec.com) suggestion
// from a newsgroup to get the pseudo-this.
vncMenu *_this = (vncMenu *) GetWindowLong(hwnd, GWL_USERDATA);
switch (iMsg)
{
case WM_MEASUREITEM:
{
MeasureItem(hwnd, GetSubMenu(_this->m_hmenu, 0), lParam);
return TRUE;
}
case WM_DRAWITEM:
{
DrawItem(lParam);
return TRUE;
}
case WM_WTSSESSION_CHANGE:
{
switch( wParam )
{
case WTS_CONSOLE_CONNECT:
vnclog.Print(LL_INTERR, VNCLOG("++++++++++++++++++++++++++++++++++++WTS_CONSOLE_CONNECT"));
break;
case WTS_CONSOLE_DISCONNECT:
vnclog.Print(LL_INTERR, VNCLOG("WTS_CONSOLE_DISCONNECT"));
_this->m_server->KillAuthClients();
break;
case WTS_SESSION_LOCK:
vnclog.Print(LL_INTERR, VNCLOG("WTS_SESSION_LOCK"));
break;
case WTS_SESSION_UNLOCK:
vnclog.Print(LL_INTERR, VNCLOG("WTS_SESSION_UNLOCK"));
break;
default:
break;
}
break;
}
case WM_CREATE:
{
s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated"));
break;
}
// Every five seconds, a timer message causes the icon to update
case WM_TIMER:
if(wParam==DOUBLECLICK_TIMER)
{
if(waitingForDoubleClick)
{
waitingForDoubleClick=false;
KillTimer(hwnd,DOUBLECLICK_TIMER);
_this->ShowContextMenu(lastLeftClickPosition);
}
}
else
{
// *** HACK for running servicified
if (vncService::RunningAsService())
{
// Attempt to add the icon if it's not already there
_this->AddTrayIcon();
// Trigger a check of the current user
//The WM_USERCHANGED message is sent to all windows after the user has
//logged on or off. When the user logs on or off,
//the system updates the user-specific settings. The system
//sends this message immediately after updating the settings.
//http://msdn2.microsoft.com/en-us/library/ms632651.aspx
PostMessage(hwnd, WM_USERCHANGED, 0, 0);
}
//_this->Show(_this->m_server->GetServerMode());
// Update the icon
_this->FlashTrayIcon(_this->m_server->AuthClientCount() != 0);
}
break;
// DEAL WITH NOTIFICATIONS FROM THE SERVER:
case WM_SRV_CLIENT_AUTHENTICATED:
case WM_SRV_CLIENT_DISCONNECT:
// Adjust the icon accordingly
_this->FlashTrayIcon(_this->m_server->AuthClientCount() != 0);
if (_this->m_server->AuthClientCount() != 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -