📄 global.cpp
字号:
#include "stdhdrs.h"
#include "Global.h"
#include "vncMenu.h"
#include "vncService.h"
#include "../../vncviewer/clientconnection.h"
#include "../../vncviewer/sessiondialog.h"
#include <fstream>
extern vncMenu *menu;
bool isQuickSupport=false;
bool isRemoteControl=false;
bool isQSCustom = false;
bool isNoInstallation=false;
bool isOutgoingConnection = false;
int ViewerAppCounter = 0;
bool DemoPresentation = false;
bool isNoSave = false;
bool DisableCaptureBlt = false;
unsigned int lastMouseMove=0;
using namespace std;
double scaleX = 1.0;
double scaleY = 1.0;
bool GetSpecialFolderPath_unsecure(int nId, char* szPath)
{
LPITEMIDLIST pidl;
if (SHGetSpecialFolderLocation(0, nId, &pidl) != NOERROR)
{
return false;
}
if (!SHGetPathFromIDList(pidl, szPath) )
{
CoTaskMemFree(pidl);
return false;
}
else
CoTaskMemFree(pidl);
return true;
}
bool GetSpecialFolderPath(int nId, string &outPath, bool doImpersonate /*= true*/)
{
//are we already impersonated, if yes we do not want to start another one
//and change back
bool bImpersonated = vncService::isImpersonated();
if (!bImpersonated && doImpersonate)
vncService::BeginImpersonation();
char path[MAX_PATH];
SecureZeroMemory(path,sizeof(path));
SetLastError(0);
outPath = "";
if (!GetSpecialFolderPath_unsecure(nId,path))
{
if (!bImpersonated && doImpersonate)
vncService::EndImpersonation();
return false;
};
outPath = path;
if (!bImpersonated && doImpersonate)
vncService::EndImpersonation();
return true;
}
void GetApplicationDirectory(string &sApplicationDirectory)
{
char appDir[_MAX_PATH]="";
char drive[_MAX_DRIVE]="";
char dir[_MAX_DIR]="";
char fname[_MAX_FNAME]="";
char ext[_MAX_EXT] = "";
//char applicationPath[MAX_PATH] ="";
string sApplicationPath;
GetApplicationPath(sApplicationPath);
_splitpath(sApplicationPath.c_str(),drive,dir,fname,ext);
strcpy(appDir,drive);
strcat(appDir,dir);
//strcpy(applicationDirectory,appDir);
sApplicationDirectory = appDir;
}
void GetApplicationPath(string &sApplicationPath)
{
char apppath[MAX_PATH]="";
GetModuleFileName(NULL,apppath,MAX_PATH);
//strcpy(applicationPath,apppath);
sApplicationPath = apppath;
}
bool FileExists(string sFileName)
{
return std::ifstream(sFileName.c_str()) != NULL;
}
bool RunningInSystemAccount()
{
const DWORD size=UNLEN+1;
char name[size];
GetUserName(name,(LPDWORD)&size);
return (strncmp(name,"SYSTEM",6)==0);
}
std::string LoadString(int id)
{
char text[256]="";
LoadString(hAppInstance, id, text, 255);
return text;
}
string Localize(int id)
{
char text[2048]="";
LoadString(hAppInstance, id, text, 2047);
return text;
}
string IntegerToString(int i)
{
stringstream stream;
stream << i;
return stream.str();
}
// If the function succeeds, the return value is nonzero.
// If the function fails, the return value is zero
BOOL RemoveNonEmptyDirectory(_TCHAR *SDirName)
{
// Make sure directory name ends with two 0-Bytes
_TCHAR String[MAX_PATH+3];
memset(String, 0, (MAX_PATH+3)*sizeof(_TCHAR));
_tcsncpy(String, SDirName, MAX_PATH+2);
SHFILEOPSTRUCT FileOp;
memset(&FileOp, 0, sizeof(FileOp));
FileOp.hwnd = 0;
FileOp.wFunc = FO_DELETE;
FileOp.pFrom = String;
FileOp.pTo = 0;
FileOp.fFlags = FOF_SILENT | FOF_NOCONFIRMATION;
FileOp.fAnyOperationsAborted = 0;
FileOp.hNameMappings = 0;
FileOp.lpszProgressTitle = 0;
return !(SHFileOperation(&FileOp));
}
// Greatest common denominator, by Euclid
int gcd(int a, int b) {
if (a < b) return gcd(b,a);
if (b == 0) return a;
return gcd(b, a % b);
}
BOOL EraseBkgnd(HWND hwnd, WPARAM wParam)
{
extern HBITMAP bmpBackgroundDialog;
HDC dcSrc, dcDest;
dcDest = (HDC)wParam;
dcSrc = CreateCompatibleDC(dcDest);
if(dcSrc)
{
SelectObject(dcSrc, bmpBackgroundDialog);
RECT r;
GetClientRect(hwnd, &r);
int height, width;
height = r.bottom-r.top;
width = r.right-r.left;
StretchBlt(dcDest, 0, 0, width, height, dcSrc, 0, 500-height, 1, height, SRCCOPY);
DeleteDC(dcSrc);
}
return TRUE;
}
BOOL CtlColorBtn(WPARAM wParam)
{
HDC hdc = (HDC)wParam;
SetBkMode(hdc, TRANSPARENT);
return (BOOL)GetStockObject(NULL_BRUSH);
}
BOOL CtlColorStatic(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
extern HBITMAP bmpBackgroundDialog;
HDC hdc = (HDC)wParam;
RECT inner, outer, client;
COLORREF col = RGB(248,248,248);
GetWindowRect(hwnd, &outer);
GetWindowRect((HWND)lParam, &inner);
GetClientRect(hwnd, &client);
// calculate y-pos of label
int hpos = (inner.bottom+inner.top)/2 - outer.top
- GetSystemMetrics(SM_CYCAPTION)
+ (500-(client.bottom-client.top));
// get color from background bitmap
HDC dcSrc;
HGDIOBJ old;
dcSrc = CreateCompatibleDC(hdc);
if(dcSrc)
{
old = SelectObject(dcSrc, bmpBackgroundDialog);
col = GetPixel(dcSrc, 0, hpos);
SelectObject(dcSrc, old);
DeleteDC(dcSrc);
}
SetBkMode(hdc, OPAQUE);
SetBkColor(hdc, col);
HBRUSH brush = SessionDialog::colorBrushes[col];
if(!brush)
{
brush = CreateSolidBrush(col);
SessionDialog::colorBrushes[col] = brush;
}
return (BOOL)brush;
}
void AddDefaultScalings(HWND hwnd)
{
int Scales[13] = { 25, 50, 75, 80, 85, 90, 95, 100, 125, 150, 200, 300, 400};
char szPer[4];
for (int i = 0; i <= 12; i++)
{
itoa(Scales[i], szPer, 10);
SendMessage(hwnd, CB_INSERTSTRING, (WPARAM)i, (LPARAM)(int FAR*)szPer);
}
}
void MeasureItem(HWND hwnd, HMENU menu, LPARAM lParam)
{
SIZE S;
HDC HMenuDC;
HFONT OldFont;
LPMEASUREITEMSTRUCT lpmis;
TCHAR buffer[255];
lpmis = (LPMEASUREITEMSTRUCT) lParam;
if(lpmis->itemID > 0) // text entry
{
HMenuDC = GetDC(hwnd);
GetMenuString(menu, lpmis->itemID, buffer, 255, 0); // get text
OldFont = (HFONT)SelectObject(HMenuDC, (HFONT) GetStockObject(DEFAULT_GUI_FONT));
GetTextExtentPoint32(HMenuDC, buffer, strlen(buffer), &S); // get the appropriate "extents"
SelectObject(HMenuDC, OldFont);
ReleaseDC(hwnd, HMenuDC);
lpmis->itemWidth = (int)(S.cx * scaleX + 10) ;
lpmis->itemHeight = (int)(S.cy * scaleY + 6);
}
else // separator
{
lpmis->itemWidth = 1;
lpmis->itemHeight = 1;
}
}
void DrawItem(LPARAM lParam)
{
HMENU sessmenu;
LPDRAWITEMSTRUCT lpdis;
HPEN pen;
MENUITEMINFO mii;
lpdis = (LPDRAWITEMSTRUCT) lParam;
memset(&mii, 0, sizeof(mii));
mii.cbSize = sizeof(mii);
mii.fMask = MIIM_STRING; // get text length of menu item
sessmenu = (HMENU)lpdis->hwndItem;
if(!GetMenuItemInfo(sessmenu, lpdis->itemID, FALSE, &mii))
return;
if(mii.cch > 0)
{
mii.cch++;
mii.fMask = MIIM_STRING;
TCHAR *buffer = new TCHAR[mii.cch];
mii.dwTypeData = buffer;
GetMenuItemInfo(sessmenu, lpdis->itemID, FALSE, &mii); // now really get the text
if (lpdis->itemState & (ODS_SELECTED|ODS_HOTLIGHT)) // is the entry selected?
SelectObject(lpdis->hDC, menuSelectedBrush);
else
SelectObject(lpdis->hDC, menuBrush);
pen = CreatePen(PS_SOLID, 1, MENU_NORMAL); // border in color of background
if(pen)
SelectObject(lpdis->hDC, pen);
if(lpdis->itemState & (ODS_DISABLED|ODS_INACTIVE)) // inactive?
SetTextColor(lpdis->hDC, RGB(192,192,192)); // textcolor grey
else
SetTextColor(lpdis->hDC, RGB(255,255,255)); // else textcolor white
SetBkMode(lpdis->hDC,TRANSPARENT); // paint background
Rectangle(lpdis->hDC,lpdis->rcItem.left,lpdis->rcItem.top,lpdis->rcItem.right,lpdis->rcItem.bottom);
lpdis->rcItem.left += 5;
lpdis->rcItem.top += 2;
DrawText(lpdis->hDC, mii.dwTypeData, mii.cch, &(lpdis->rcItem),
GetAsyncKeyState(VK_MENU) ? 0:DT_HIDEPREFIX); // and text
if(pen)
DeleteObject(pen);
delete[] buffer;
}
else // separator
{
pen = CreatePen(PS_SOLID, 1, MENU_NORMAL);
if(pen)
SelectObject(lpdis->hDC, pen);
MoveToEx(lpdis->hDC, lpdis->rcItem.left,lpdis->rcItem.top, NULL);
LineTo(lpdis->hDC, lpdis->rcItem.right, lpdis->rcItem.top); // blue line background
SelectObject(lpdis->hDC, GetStockObject(WHITE_PEN));
MoveToEx(lpdis->hDC, lpdis->rcItem.left+5, lpdis->rcItem.top, NULL);
LineTo(lpdis->hDC, lpdis->rcItem.right-5, lpdis->rcItem.top); // white line foreground
if (pen)
DeleteObject(pen);
}
}
BOOL CALLBACK HelpTextProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
WNDPROC oldProc = (WNDPROC) GetWindowLong(hwnd, GWL_USERDATA);
switch(uMsg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC dc = BeginPaint(hwnd, &ps);
char text[512];
GetWindowText(hwnd, text, 512); // get text of label
text[511] = '\0';
if(text[0])
{
HGDIOBJ oldBrush = NULL;
HBRUSH br = CreateSolidBrush(MENU_NORMAL); // orange = RGB(255, 199, 60)
SetBkColor(dc, MENU_NORMAL);
if(br)
oldBrush = SelectObject(dc, br);
RECT r;
GetClientRect(hwnd, &r);
RoundRect(dc, r.left, r.top, r.right, r.bottom, 5, 5); // paint background
SetTextColor(dc, RGB(255, 255, 255));
HFONT hf = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
HGDIOBJ oldFont = SelectObject(dc, hf);
InflateRect(&r, -3, -3);
DrawText(dc, text, strlen(text), &r, DT_WORDBREAK|DT_NOCLIP|DT_END_ELLIPSIS); // draw text
SelectObject(dc, oldBrush);
SelectObject(dc, oldFont);
if(br)
DeleteObject(br);
}
EndPaint(hwnd, &ps);
return TRUE;
}
case WM_SETTEXT:
{
LRESULT res = FALSE;
if(oldProc)
res = CallWindowProc(oldProc, hwnd, uMsg, wParam, lParam);
InvalidateRect(hwnd, NULL, TRUE); // repaint after text change
UpdateWindow(hwnd);
return res;
}
}
if(oldProc)
return CallWindowProc(oldProc, hwnd, uMsg, wParam, lParam);
else
return FALSE;
}
void InitScaling() {
HDC screen = GetDC(0);
scaleX = GetDeviceCaps(screen, LOGPIXELSX) / 96.0;
scaleY = GetDeviceCaps(screen, LOGPIXELSY) / 96.0;
ReleaseDC(0, screen);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -