Skip to content
This repository was archived by the owner on Jan 28, 2022. It is now read-only.

Commit 3e6c8af

Browse files
committed
Add M2WinRTHelpers.
1 parent 981e5fc commit 3e6c8af

4 files changed

Lines changed: 268 additions & 0 deletions

File tree

M2TeamCommonLibrary/M2TeamCommonLibrary.vcxitems

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
<ClCompile Include="$(MSBuildThisFileDirectory)M2CXHelpers.cpp" />
1919
<ClCompile Include="$(MSBuildThisFileDirectory)M2Win32GUIHelpers.cpp" />
2020
<ClCompile Include="$(MSBuildThisFileDirectory)M2Win32Helpers.cpp" />
21+
<ClCompile Include="$(MSBuildThisFileDirectory)M2WinRTHelpers.cpp" />
2122
</ItemGroup>
2223
<ItemGroup>
2324
<ClInclude Include="$(MSBuildThisFileDirectory)M2BaseHelpers.h" />
2425
<ClInclude Include="$(MSBuildThisFileDirectory)M2CXHelpers.h" />
2526
<ClInclude Include="$(MSBuildThisFileDirectory)M2MessageDialogResource.h" />
2627
<ClInclude Include="$(MSBuildThisFileDirectory)M2Win32GUIHelpers.h" />
2728
<ClInclude Include="$(MSBuildThisFileDirectory)M2Win32Helpers.h" />
29+
<ClInclude Include="$(MSBuildThisFileDirectory)M2WinRTHelpers.h" />
2830
</ItemGroup>
2931
<ItemGroup>
3032
<ResourceCompile Include="$(MSBuildThisFileDirectory)M2MessageDialogResource.rc" />

M2TeamCommonLibrary/M2TeamCommonLibrary.vcxitems.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<Filter Include="M2Win32GUIHelpers">
1414
<UniqueIdentifier>{c3376011-3a37-453f-8b61-e247c38d599a}</UniqueIdentifier>
1515
</Filter>
16+
<Filter Include="M2WinRTHelpers">
17+
<UniqueIdentifier>{4d531645-d79a-43b5-8917-abeb8cdacf41}</UniqueIdentifier>
18+
</Filter>
1619
</ItemGroup>
1720
<ItemGroup>
1821
<ClCompile Include="$(MSBuildThisFileDirectory)M2CXHelpers.cpp">
@@ -27,6 +30,9 @@
2730
<ClCompile Include="$(MSBuildThisFileDirectory)M2Win32GUIHelpers.cpp">
2831
<Filter>M2Win32GUIHelpers</Filter>
2932
</ClCompile>
33+
<ClCompile Include="$(MSBuildThisFileDirectory)M2WinRTHelpers.cpp">
34+
<Filter>M2WinRTHelpers</Filter>
35+
</ClCompile>
3036
</ItemGroup>
3137
<ItemGroup>
3238
<ClInclude Include="$(MSBuildThisFileDirectory)M2CXHelpers.h">
@@ -44,6 +50,9 @@
4450
<ClInclude Include="$(MSBuildThisFileDirectory)M2Win32GUIHelpers.h">
4551
<Filter>M2Win32GUIHelpers</Filter>
4652
</ClInclude>
53+
<ClInclude Include="$(MSBuildThisFileDirectory)M2WinRTHelpers.h">
54+
<Filter>M2WinRTHelpers</Filter>
55+
</ClInclude>
4756
</ItemGroup>
4857
<ItemGroup>
4958
<ResourceCompile Include="$(MSBuildThisFileDirectory)M2MessageDialogResource.rc">
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/******************************************************************************
2+
Project: M2-Team Common Library
3+
Description: Implementation for the C++/WinRT helper functions.
4+
File Name: M2WinRTHelpers.cpp
5+
License: The MIT License
6+
******************************************************************************/
7+
8+
#include "pch.h"
9+
10+
#include <Windows.h>
11+
12+
#ifdef _M2_WINRT_HELPERS_
13+
14+
#include "M2WinRTHelpers.h"
15+
16+
/// <summary>
17+
/// Creates a GUID, a unique 128-bit integer used for CLSIDs and interface
18+
/// identifiers.
19+
/// </summary>
20+
/// <returns>
21+
/// The function will return GUID struct.
22+
/// </returns>
23+
GUID M2CreateGuid()
24+
{
25+
GUID guid = { 0 };
26+
winrt::check_hresult(CoCreateGuid(&guid));
27+
return guid;
28+
}
29+
30+
/// <summary>
31+
/// Finds a sub string from a source string.
32+
/// </summary>
33+
/// <param name="SourceString">
34+
/// The source string.
35+
/// </param>
36+
/// <param name="SubString">
37+
/// The sub string.
38+
/// </param>
39+
/// <param name="IgnoreCase">
40+
/// Determines whether to ignore case.
41+
/// </param>
42+
/// <returns>
43+
/// Returns true if successful, or false otherwise.
44+
/// </returns>
45+
bool M2FindSubString(
46+
winrt::hstring SourceString,
47+
winrt::hstring SubString,
48+
bool IgnoreCase)
49+
{
50+
return (::FindNLSStringEx(
51+
nullptr,
52+
(IgnoreCase ? NORM_IGNORECASE : 0) | FIND_FROMSTART,
53+
SourceString.c_str(),
54+
SourceString.size(),
55+
SubString.c_str(),
56+
SubString.size(),
57+
nullptr,
58+
nullptr,
59+
nullptr,
60+
0) >= 0);
61+
}
62+
63+
/// <summary>
64+
/// Converts a numeric value into a string that represents the number expressed
65+
/// as a size value in byte, bytes, kibibytes, mebibytes, gibibytes, tebibytes,
66+
/// pebibytes or exbibytes, depending on the size.
67+
/// </summary>
68+
/// <param name="ByteSize">
69+
/// The numeric byte size value to be converted.
70+
/// </param>
71+
/// <returns>
72+
/// Returns a winrt::hstring object which represents the converted string.
73+
/// </returns>
74+
winrt::hstring M2ConvertByteSizeToString(
75+
uint64_t ByteSize)
76+
{
77+
const wchar_t* Systems[] =
78+
{
79+
L"Byte",
80+
L"Bytes",
81+
L"KiB",
82+
L"MiB",
83+
L"GiB",
84+
L"TiB",
85+
L"PiB",
86+
L"EiB"
87+
};
88+
89+
size_t nSystem = 0;
90+
double result = static_cast<double>(ByteSize);
91+
92+
if (ByteSize > 1)
93+
{
94+
for (
95+
nSystem = 1;
96+
nSystem < sizeof(Systems) / sizeof(*Systems);
97+
++nSystem)
98+
{
99+
if (1024.0 > result)
100+
break;
101+
102+
result /= 1024.0;
103+
}
104+
105+
result = static_cast<uint64_t>(result * 100) / 100.0;
106+
}
107+
108+
return winrt::to_hstring(result) + L" " + Systems[nSystem];
109+
}
110+
111+
namespace M2
112+
{
113+
void NotifyPropertyChangedBase::RaisePropertyChanged(
114+
winrt::hstring PropertyName)
115+
{
116+
this->m_PropertyChanged(
117+
*this, winrt::PropertyChangedEventArgs(PropertyName));
118+
}
119+
120+
winrt::event_token NotifyPropertyChangedBase::PropertyChanged(
121+
winrt::PropertyChangedEventHandler const& value)
122+
{
123+
return this->m_PropertyChanged.add(value);
124+
}
125+
126+
void NotifyPropertyChangedBase::PropertyChanged(
127+
winrt::event_token const & token)
128+
{
129+
this->m_PropertyChanged.remove(token);
130+
}
131+
}
132+
133+
// Execute function on the UI thread with normal priority.
134+
// Parameters:
135+
// agileCallback: The function you want to execute.
136+
// Return value:
137+
// The return value is Windows::Foundation::IAsyncAction^.
138+
winrt::IAsyncAction M2ExecuteOnUIThread(
139+
winrt::DispatchedHandler const& agileCallback)
140+
{
141+
using winrt::Windows::ApplicationModel::Core::CoreApplication;
142+
using winrt::Windows::UI::Core::CoreDispatcherPriority;
143+
144+
return CoreApplication::MainView().CoreWindow().Dispatcher().RunAsync(
145+
CoreDispatcherPriority::Normal, agileCallback);
146+
}
147+
148+
#endif // _M2_WINRT_HELPERS_
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/******************************************************************************
2+
Project: M2-Team Common Library
3+
Description: Definition for the C++/WinRT helper functions.
4+
File Name: M2WinRTHelpers.h
5+
License: The MIT License
6+
******************************************************************************/
7+
8+
#pragma once
9+
10+
#ifndef _M2_WINRT_HELPERS_
11+
#define _M2_WINRT_HELPERS_
12+
13+
#include <Windows.h>
14+
#include "M2BaseHelpers.h"
15+
16+
#include <winrt\Windows.Foundation.h>
17+
18+
#include <winrt\Windows.ApplicationModel.Core.h>
19+
#include <winrt\Windows.UI.Core.h>
20+
#include <winrt\Windows.UI.Xaml.Data.h>
21+
22+
#include <map>
23+
#include <string>
24+
#include <vector>
25+
26+
namespace winrt
27+
{
28+
using Windows::Foundation::IAsyncAction;
29+
using Windows::UI::Core::DispatchedHandler;
30+
using Windows::UI::Xaml::Data::INotifyPropertyChanged;
31+
using Windows::UI::Xaml::Data::PropertyChangedEventArgs;
32+
using Windows::UI::Xaml::Data::PropertyChangedEventHandler;
33+
}
34+
35+
/// <summary>
36+
/// Creates a GUID, a unique 128-bit integer used for CLSIDs and interface
37+
/// identifiers.
38+
/// </summary>
39+
/// <returns>
40+
/// The function will return GUID struct.
41+
/// </returns>
42+
GUID M2CreateGuid();
43+
44+
/// <summary>
45+
/// Finds a sub string from a source string.
46+
/// </summary>
47+
/// <param name="SourceString">
48+
/// The source string.
49+
/// </param>
50+
/// <param name="SubString">
51+
/// The sub string.
52+
/// </param>
53+
/// <param name="IgnoreCase">
54+
/// Determines whether to ignore case.
55+
/// </param>
56+
/// <returns>
57+
/// Returns true if successful, or false otherwise.
58+
/// </returns>
59+
bool M2FindSubString(
60+
winrt::hstring SourceString,
61+
winrt::hstring SubString,
62+
bool IgnoreCase);
63+
64+
/// <summary>
65+
/// Converts a numeric value into a string that represents the number expressed
66+
/// as a size value in byte, bytes, kibibytes, mebibytes, gibibytes, tebibytes,
67+
/// pebibytes or exbibytes, depending on the size.
68+
/// </summary>
69+
/// <param name="ByteSize">
70+
/// The numeric byte size value to be converted.
71+
/// </param>
72+
/// <returns>
73+
/// Returns a winrt::hstring object which represents the converted string.
74+
/// </returns>
75+
winrt::hstring M2ConvertByteSizeToString(
76+
uint64_t ByteSize);
77+
78+
namespace M2
79+
{
80+
struct NotifyPropertyChangedBase : winrt::implements<
81+
NotifyPropertyChangedBase, winrt::INotifyPropertyChanged>
82+
{
83+
private:
84+
winrt::event<winrt::PropertyChangedEventHandler> m_PropertyChanged;
85+
86+
protected:
87+
void RaisePropertyChanged(
88+
winrt::hstring PropertyName);
89+
90+
public:
91+
NotifyPropertyChangedBase() = default;
92+
93+
winrt::event_token PropertyChanged(
94+
winrt::PropertyChangedEventHandler const& value);
95+
96+
void PropertyChanged(
97+
winrt::event_token const& token);
98+
};
99+
}
100+
101+
// Execute function on the UI thread with normal priority.
102+
// Parameters:
103+
// agileCallback: The function you want to execute.
104+
// Return value:
105+
// The return value is Windows::Foundation::IAsyncAction^.
106+
winrt::IAsyncAction M2ExecuteOnUIThread(
107+
winrt::DispatchedHandler const& agileCallback);
108+
109+
#endif // _M2_WINRT_HELPERS_

0 commit comments

Comments
 (0)