Skip to content

Commit

Permalink
Support WinUI 2
Browse files Browse the repository at this point in the history
  • Loading branch information
bho35 authored and bho35 committed Jun 11, 2023
1 parent 30cfcba commit d7ddce8
Show file tree
Hide file tree
Showing 17 changed files with 571 additions and 39 deletions.
4 changes: 0 additions & 4 deletions EasyWinModernControl/CModernButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,13 @@ namespace EasyWinModernControl {

private:
Windows::UI::Xaml::Controls::Button _btn;
Windows::UI::Xaml::Controls::RadioButton _radio;


DWORD _id;
_TEasyWinModernCtrl_BtnCallback _callbackFunc = NULL;
PVOID _userData = NULL;

BOOL _useAccentColor = FALSE;

//static void _InternalClicked(const winrt::Windows::Foundation::IInspectable& sender, const RoutedEventArgs& args);

static LPCWSTR xml;
};
}
41 changes: 38 additions & 3 deletions EasyWinModernControl/CModernControl.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,55 @@
#include "stdafx.h"
#include "CModernControl.h"


using namespace EasyWinModernControl;

typedef NTSTATUS(__stdcall* TRtlGetVersion)(OSVERSIONINFOEXW* ovf);
TRtlGetVersion _RtlGetVersion = NULL;

WindowsXamlManager wm = NULL;
Application app = NULL;

class App : public ApplicationT<App, IXamlMetadataProvider>
{
public:

IXamlType GetXamlType(winrt::Windows::UI::Xaml::Interop::TypeName const& type)
{
return provider.GetXamlType(type);
}
IXamlType GetXamlType(hstring const& fullname)
{
return provider.GetXamlType(fullname);
}
com_array<XmlnsDefinition> GetXmlnsDefinitions()
{
return provider.GetXmlnsDefinitions();
}

private:

winrt::Microsoft::UI::Xaml::XamlTypeInfo::XamlControlsXamlMetaDataProvider provider;
};

HRESULT CModernControl::Initialize(BOOL useMTA) {
HRESULT CModernControl::Initialize(BOOL useMTA, BOOL useWinUI) {
if (useMTA) {
winrt::init_apartment(apartment_type::multi_threaded);
}
else {
winrt::init_apartment(apartment_type::single_threaded);
}

if (useWinUI) {
app = winrt::make<App>();
}

wm = WindowsXamlManager::InitializeForCurrentThread();

if (useWinUI) {
winrt::Microsoft::UI::Xaml::Controls::XamlControlsResources res;
Application::Current().Resources().MergedDictionaries().Append(res);
}

return 0;
}
Expand All @@ -27,6 +58,10 @@ void CModernControl::UnInitialize() {
if (wm) {
wm.Close();
}

if (app) {
app.Exit();
}
}

BOOL CModernControl::IsSupportSystem() {
Expand Down Expand Up @@ -55,7 +90,6 @@ CModernControl::CModernControl() {
}

CModernControl::~CModernControl() {
xs.Close();
}


Expand Down Expand Up @@ -100,6 +134,7 @@ HRESULT CModernControl::Show(HWND parentHwnd) {
//show uwp window at win32 window.
GetWindowRect(parentHwnd, &rect);
SetWindowPos(_uwpHwnd, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_SHOWWINDOW);

ShowWindow(_uwpHwnd, SW_SHOW);

escapeArea:
Expand Down Expand Up @@ -163,4 +198,4 @@ void CModernControl::SetTheme(DWORD dwFlags) {
else {
this->_theme = winrt::Windows::UI::Xaml::ElementTheme::Default;
}
}
}
3 changes: 2 additions & 1 deletion EasyWinModernControl/CModernControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace EasyWinModernControl {
class CModernControl
{
public:
static HRESULT Initialize(BOOL useMTA);
static HRESULT Initialize(BOOL useMTA, BOOL useWinUI);
static void UnInitialize();
static BOOL IsSupportSystem();

Expand All @@ -32,6 +32,7 @@ namespace EasyWinModernControl {
virtual void SetTemplate();
virtual void OnAdjustLayout();


DesktopWindowXamlSource xs;

winrt::Windows::Foundation::IInspectable ins = NULL;
Expand Down
85 changes: 85 additions & 0 deletions EasyWinModernControl/CWinUINumberBox.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "stdafx.h"
#include "CWinUINumberBox.h"

using namespace EasyWinModernControl;

LPCWSTR CWinUINumberBox::xml = LR"(
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</StackPanel>)";

CWinUINumberBox::CWinUINumberBox(LPCWSTR controlName, LPCWSTR headerTitle, LPCWSTR textPlaceholder) {
if (controlName) {
this->_numBox.Name(controlName);
}

if (headerTitle) {
this->_numBox.Header(winrt::box_value(headerTitle));
}

if (textPlaceholder) {
this->_numBox.PlaceholderText(textPlaceholder);
}

//fix normal control focus issue
this->_numBox.GettingFocus([this](const winrt::Windows::Foundation::IInspectable& sender, const RoutedEventArgs& args)
{
SendMessageW(this->GetParentHwnd(), WM_SETFOCUS, 0, 0);
});
}

CWinUINumberBox::~CWinUINumberBox() {

}

void CWinUINumberBox::SetButtonType(DWORD mode) {
if (mode == 1) {
this->_numBox.SpinButtonPlacementMode(Microsoft::UI::Xaml::Controls::NumberBoxSpinButtonPlacementMode::Compact);
}
else if (mode == 2) {
this->_numBox.SpinButtonPlacementMode(Microsoft::UI::Xaml::Controls::NumberBoxSpinButtonPlacementMode::Inline);
}
else {
this->_numBox.SpinButtonPlacementMode(Microsoft::UI::Xaml::Controls::NumberBoxSpinButtonPlacementMode::Hidden);
}
}

void CWinUINumberBox::SetValue(DOUBLE value) {
this->_numBox.Value(value);
}


void CWinUINumberBox::SetMinValue(DOUBLE val) {
this->_numBox.Minimum(val);
}

void CWinUINumberBox::SetMaxValue(DOUBLE val) {
this->_numBox.Maximum(val);
}

DOUBLE CWinUINumberBox::GetValue() {
return this->_numBox.Value();
}

void CWinUINumberBox::SetChangeStep(DOUBLE step) {
this->_numBox.SmallChange(step);
}

void CWinUINumberBox::SetTemplate() {
hstring str(xml);
this->ins = XamlReader::Load(str);
if (!this->ins) {
return;
}

Windows::UI::Xaml::Controls::StackPanel panel = this->ins.as<Windows::UI::Xaml::Controls::StackPanel>();

panel.Children().InsertAt(0, this->_numBox);

return;
}

void CWinUINumberBox::OnAdjustLayout() {
this->_numBox.Height(this->_height);
this->_numBox.Width(this->_width);
}
35 changes: 35 additions & 0 deletions EasyWinModernControl/CWinUINumberBox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once
#include "CModernControl.h"

namespace EasyWinModernControl {

class CWinUINumberBox : public CModernControl
{
public:
CWinUINumberBox(LPCWSTR controlName, LPCWSTR headerTitle , LPCWSTR textPlaceholder);

~CWinUINumberBox();

void SetButtonType(DWORD mode);

void SetValue(DOUBLE value);

void SetMinValue(DOUBLE val);

void SetMaxValue(DOUBLE val);

void SetChangeStep(DOUBLE step);

DOUBLE GetValue();

protected:
void SetTemplate();
void OnAdjustLayout();

private:
Microsoft::UI::Xaml::Controls::NumberBox _numBox;

static LPCWSTR xml;
};
}

39 changes: 39 additions & 0 deletions EasyWinModernControl/CWinUIProgressRing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "stdafx.h"
#include "CWinUIProgressRing.h"

using namespace EasyWinModernControl;

LPCWSTR CWinUIProgressRing::xml = LR"(
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</StackPanel>)";

CWinUIProgressRing::CWinUIProgressRing(LPCWSTR controlName) {
if (controlName) {
this->_ring.Name(controlName);
}
}
CWinUIProgressRing::~CWinUIProgressRing() {

}

void CWinUIProgressRing::SetTemplate() {
hstring str(xml);
this->ins = XamlReader::Load(str);
if (!this->ins) {
return;
}

this->_ring.IsActive(true);

Windows::UI::Xaml::Controls::StackPanel panel = this->ins.as<Windows::UI::Xaml::Controls::StackPanel>();

panel.Children().InsertAt(0, this->_ring);

return;
}

void CWinUIProgressRing::OnAdjustLayout() {
this->_ring.Height(this->_height);
this->_ring.Width(this->_width);
}
22 changes: 22 additions & 0 deletions EasyWinModernControl/CWinUIProgressRing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include "CModernControl.h"

namespace EasyWinModernControl {

class CWinUIProgressRing : public CModernControl
{
public:
CWinUIProgressRing(LPCWSTR controlName);
~CWinUIProgressRing();

protected:
void SetTemplate();
void OnAdjustLayout();

private:
Microsoft::UI::Xaml::Controls::ProgressRing _ring;

static LPCWSTR xml;
};

}
50 changes: 50 additions & 0 deletions EasyWinModernControl/CWinUIProgressbar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "stdafx.h"
#include "CWinUIProgressbar.h"

using namespace EasyWinModernControl;

LPCWSTR CWinUIProgressbar::xml = LR"(
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</StackPanel>)";

CWinUIProgressbar::CWinUIProgressbar(LPCWSTR controlName, DOUBLE minVal, DOUBLE maxVal) {
if (controlName) {
this->_progressbar.Name(controlName);
}

this->_progressbar.Minimum(minVal);
this->_progressbar.Maximum(maxVal);
}

CWinUIProgressbar::~CWinUIProgressbar() {

}

void CWinUIProgressbar::SetValue(BOOL isIndeterminate, BOOL isPause, DOUBLE value) {
this->_progressbar.IsIndeterminate(isIndeterminate);

if (!isIndeterminate) {
this->_progressbar.Value(value);
}

this->_progressbar.ShowPaused(isPause);
}

void CWinUIProgressbar::SetTemplate() {
hstring str(xml);
this->ins = XamlReader::Load(str);
if (!this->ins) {
return;
}

Windows::UI::Xaml::Controls::StackPanel panel = this->ins.as<Windows::UI::Xaml::Controls::StackPanel>();

panel.Children().InsertAt(0, this->_progressbar);

}

void CWinUIProgressbar::OnAdjustLayout() {
this->_progressbar.Height(_height);
this->_progressbar.Width(_width);
}
24 changes: 24 additions & 0 deletions EasyWinModernControl/CWinUIProgressbar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
#include "CModernControl.h"

namespace EasyWinModernControl {

class CWinUIProgressbar : public CModernControl
{
public:
CWinUIProgressbar(LPCWSTR controlName, DOUBLE minVal, DOUBLE maxVal);
~CWinUIProgressbar();

void SetValue(BOOL isIndeterminate, BOOL isPause, DOUBLE value);

protected:
void SetTemplate();
void OnAdjustLayout();

private:
Microsoft::UI::Xaml::Controls::ProgressBar _progressbar;

static LPCWSTR xml;
};

}
Loading

0 comments on commit d7ddce8

Please sign in to comment.