📄 common.hpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#pragma once
#ifndef __COMMON_HPP__
#define __COMMON_HPP__
#include <windows.h>
#include "GDICache.hpp"
#include "ControlDefinitions.h"
#include <auto_xxx.hxx>
#include "VoipNotify.hpp"
//Global data
struct GlobalData_t
{
static HINSTANCE s_ModuleInstance;
static GDICache_t s_GDICacheObject;
static HBRUSH s_ListBoxBrush;
};
#define RECTWIDTH(rc) ((rc).right - (rc).left)
#define RECTHEIGHT(rc) ((rc).bottom - (rc).top)
namespace CommonUtilities_t
{
enum ControlType_e
{
ControlTypeEdit,
ControlTypeListbox,
ControlTypeMenuBar,
ControlTypeMenuButton,
ControlTypePopupMenu,
ControlTypeTrackbar,
UnknownControl, // MUST be last in the enumeration
};
//get control type
static
ControlType_e
GetControlType(
HWND hwnd
)
{
ControlType_e Type = UnknownControl;
WCHAR ClassName[MAX_PATH] = L"";
// Get the class name of the potential control
if (!GetClassName(hwnd, ClassName, _countof(ClassName)))
{
return Type;
}
if (wcscmp(WNDCLASS_EDIT, ClassName) == 0)
{
Type = ControlTypeEdit;
}
else if (wcscmp(WNDCLASS_MENUBUTTON, ClassName) == 0)
{
Type = ControlTypeMenuButton;
}
else if (wcscmp(WNDCLASS_LISTBOX, ClassName) == 0)
{
Type = ControlTypeListbox;
}
else if (wcscmp(WNDCLASS_MENUBAR, ClassName) == 0)
{
Type = ControlTypeMenuBar;
}
else if (wcscmp(WNDCLASS_POPUPMENU, ClassName) == 0)
{
Type = ControlTypePopupMenu;
}
else if (wcscmp(WNDCLASS_TRACKBAR, ClassName) == 0)
{
Type = ControlTypeTrackbar;
}
return Type;
}
/*------------------------------------------------------------------------------
GetErrorFromWin32
Helper function that maps WIN32's LastError into a HRESULT.
NOTE: maps NO_ERROR to E_UNEXPECTED, to enforce failure
------------------------------------------------------------------------------*/
static
inline
HRESULT
GetErrorFromWin32(
HRESULT DefaultError = E_FAIL
)
{
HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
if (SUCCEEDED(hr))
{
hr = DefaultError;
}
return hr;
}
/*------------------------------------------------------------------------------
ToHR
Designed to transform a Win32 call to an HRESULT. If the call fails,
ToHR will get the last Win32 error and transform it.
Parameters:
fCallRes: The result of a Win32 call. Use it as:
hr = ToHR(Win32Method());
------------------------------------------------------------------------------*/
inline
HRESULT
ToHR(
BOOL CallResult
)
{
if(!CallResult)
{
return GetErrorFromWin32();
}
else
{
return S_OK;
}
}
static
HRESULT
InitializeRCDataHelper(
UINT ResourceId,
int SizeInBytes,
void** ppRCData
)
{
if (!ppRCData)
{
ASSERT(0);
return E_INVALIDARG;
}
*ppRCData = NULL;
HRSRC ResourceData = FindResource(
GlobalData_t::s_ModuleInstance,
MAKEINTRESOURCE(ResourceId),
RT_RCDATA
);
if (!ResourceData)
{
return CommonUtilities_t::GetErrorFromWin32();
}
if (SizeofResource(
GlobalData_t::s_ModuleInstance,
ResourceData
) !=
SizeInBytes)
{
ASSERT(0);
return E_INVALIDARG;
}
HGLOBAL GlobalData = LoadResource(GlobalData_t::s_ModuleInstance, ResourceData);
if (!GlobalData)
{
return CommonUtilities_t::GetErrorFromWin32();
}
*ppRCData = reinterpret_cast<void*>(GlobalData);
return S_OK;
}
static
inline
const WCHAR*
LoadString(
HINSTANCE Instance,
UINT StringId
)
{
return reinterpret_cast<const WCHAR*>(
LoadString(Instance, StringId, 0, 0)
);
}
static
bool
HasParentalFocus(
HWND Control
)
{
HWND WindowToCheck = GetFocus();
while (WindowToCheck)
{
if (WindowToCheck == Control)
{
return true;
}
WindowToCheck = GetParent(WindowToCheck);
}
return FALSE;
}
static
bool
MakeSureRegKeyExists(
void
)
{
ce::auto_hkey KeyHandle;
DWORD Disposition = 0;
long Result = RegCreateKeyEx(
SN_VOIP_ROOT,
SN_VOIP_PATH,
0, NULL, 0, 0, NULL,
&KeyHandle,
&Disposition
);
return (Result == ERROR_SUCCESS);
}
};
#endif // !defined __COMMON_HPP__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -