#ifdef _WIN32
#include <windows.h>
#endif
#include <iostream>
#include <fstream>
#include <string>
bool IsLoggedIn()
{
using namespace std;
// ...
return un == username && pw == password;
}
int main()
{
using namespace std;
#ifdef _WIN32
// https://habr.com/ru/sandbox/108750/
// Устраняет проблемы выводом кириллицы на консоль Windows
// Файл должен быть сохранён с кодировкой Windows 1251
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
#endif
int choice;
do
{
cout << "1: Register\n2: Login\n Your choice: ";
cin >> choice;
if (choice == 1)
{
// ...
}
else if (choice == 2)
{
bool status = IsLoggedIn();
if (!status)
{
cout << "False Login!\n";
}
else
{
cout << "Succesfully logged in!\n";
}
#ifdef _WIN32
system("pause");
#endif
return status ? 1 : 0;
}
} while (choice == 1);
}
#include "pch.h"
#include "MainPage.xaml.h"
using namespace RemoveApp;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
std::string FormatPoint(POINT&);
std::wstring_convert<std::codecvt_utf8<wchar_t>> MainPage::s_converter;
std::mutex MainPage::s_mutex;
DispatcherTimer^ _timer;
uint32_t _counter;
const uint32_t TICK = 10000;
MainPage::MainPage()
{
InitializeComponent();
UpdateLabel();
_timer = ref new DispatcherTimer();
TimeSpan ts;
ts.Duration = 1000 * TICK;
_timer->Interval = ts;
auto registrationToken = _timer->Tick += ref new EventHandler<Object^>(this, &MainPage::OnTick);
_button->Click += ref new RoutedEventHandler(this, &MainPage::OnClick);
}
void MainPage::OnTick(Object^ sender, Object^ e)
{
UpdateLabel();
}
void MainPage::UpdateLabel()
{
//GetCursorPos(&cp);
//SetCursorPos(0, 0);
//SetCursorPos(cp.x, cp.y);
++_counter;
POINT cp = { _counter, _counter + 10 };
std::string text = FormatPoint(cp);
_label1->Text = StringToPlatformString(text);
}
void MainPage::OnClick(Object^ sender, RoutedEventArgs^ e)
{
if (!_timer->IsEnabled)
{
_timer->Start();
}
}
std::string MainPage::PlatformStringToString(Platform::String^ text)
{
if (text == nullptr) {
return std::string("");
}
std::lock_guard<std::mutex> lock(s_mutex);
return s_converter.to_bytes(text->Data());
}
Platform::String^ MainPage::StringToPlatformString(const std::string& text)
{
if (text.empty()) {
return ref new Platform::String();
}
std::lock_guard<std::mutex> lock(s_mutex);
std::wstring converted = s_converter.from_bytes(text);
return ref new Platform::String(converted.c_str());
}
std::string FormatPoint(POINT& point)
{
return "x = " + std::to_string(point.x) + ", y = " + std::to_string(point.y);
}
#pragma once
#include "MainPage.g.h"
#include <codecvt>
#include <string>
#include <mutex>
#include <cstdint>
using namespace Platform;
using namespace Windows::UI::Xaml;
namespace RemoveApp
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public ref class MainPage sealed
{
public:
MainPage();
void OnTick(Object^ sender, Object^ e);
void OnClick(Object^ sender, RoutedEventArgs^ e);
void MainPage::UpdateLabel();
private:
std::string PlatformStringToString(String^ s);
String^ StringToPlatformString(const std::string& s);
static std::wstring_convert<std::codecvt_utf8<wchar_t>> s_converter;
static std::mutex s_mutex;
};
}
<Page
x:Class="RemoveApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:RemoveApp"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<Grid>
<StackPanel
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock
x:Name="_label1"
Margin="0,25,0,0"
FontSize="24" />
<Button
x:Name="_button"
Margin="0,18,0,0"
Content="Запустить таймер" />
</StackPanel>
</Grid>
</Page>
#pragma once
#include <collection.h>
#include <windows.h>
#include "App.xaml.h"
#include "pch.h"
#pragma once
#include <string>
class Window
{
public:
Window(const std::string& fileName);
void MaximizeWindow();
private:
std::string _fileName;
};
#include "Window.h"
Window::Window(const std::string& fileName)
: _fileName(fileName) {
}
void Window::MaximizeWindow() {
// Здесь можно обратиться к полю _fileName
}
using System;
namespace ConsoleApp1
{
public class ModelBase
{
protected bool CheckValueTypeField<T>(T field)
where T : struct
{
return field.Equals(default(T));
}
protected bool CheckReferenceTypeField<T>(T field)
where T : class
{
return field == null;
}
protected void ThrowIfNotDefault<T>(T field)
where T : struct
{
if (!CheckValueTypeField(field))
{
throw new InvalidOperationException();
}
}
protected void ThrowIfNotNull<T>(T field)
where T : class
{
if (!CheckReferenceTypeField(field))
{
throw new InvalidOperationException();
}
}
}
public class TestModel : ModelBase
{
private string _name;
private int _value;
public string Name
{
get { return _name; }
set
{
ThrowIfNotNull(_name);
_name = value;
}
}
public int Value
{
get { return _value; }
set
{
ThrowIfNotDefault(_value);
_value = value;
}
}
}
class Program
{
static void Main(string[] args)
{
var test = new TestModel();
test.Name = "test name";
test.Value = 1;
try
{
test.Name = "new name";
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
try
{
test.Value = 5;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Display(test);
}
static void Display(TestModel model)
{
Console.WriteLine(
$"{nameof(model.Name)} == {model.Name}; {nameof(model.Value)} == {model.Value};");
}
}
}
public class TestModel : ModelBase
{
private string _name;
private int _value;
public string Name
{
get { return _name; }
set
{
if (!CheckReferenceTypeField(_name))
{
_name = value;
}
}
}
public int Value
{
get { return _value; }
set
{
if (!CheckValueTypeField(_value))
{
_value = value;
}
}
}
}
using System;
namespace ConsoleApp1
{
public abstract class ModelBase
{
private readonly bool _throwAnExceptionInsteadOfAnEvent;
public event EventHandler FieldIsNotDefault;
protected ModelBase(bool throwAnExceptionInsteadOfAnEvent = true)
{
_throwAnExceptionInsteadOfAnEvent = throwAnExceptionInsteadOfAnEvent;
}
protected bool CheckValueTypeField<T>(T field)
where T : struct
{
return field.Equals(default(T));
}
protected bool CheckReferenceTypeField<T>(T field)
where T : class
{
return field == null;
}
protected void AssertIsNotDefault<T>(T field)
where T : struct
{
if (!CheckValueTypeField(field))
{
if (_throwAnExceptionInsteadOfAnEvent)
{
throw new InvalidOperationException();
}
OnFieldIsNotDefault();
}
}
protected void AssertIsNotNull<T>(T field)
where T : class
{
if (!CheckReferenceTypeField(field))
{
if (_throwAnExceptionInsteadOfAnEvent)
{
throw new InvalidOperationException();
}
OnFieldIsNotDefault();
}
}
protected void OnFieldIsNotDefault()
{
FieldIsNotDefault?.Invoke(this, EventArgs.Empty);
}
}
public class TestModel : ModelBase
{
private string _name;
private int _value;
public string Name
{
get { return _name; }
set
{
AssertIsNotNull(_name);
_name = value;
}
}
public int Value
{
get { return _value; }
set
{
AssertIsNotDefault(_value);
_value = value;
}
}
public TestModel()
: base(false)
{
}
}
class Program
{
static void Main(string[] args)
{
var test = new TestModel();
test.FieldIsNotDefault += OnFieldIsNotDefault;
test.Name = "test name";
test.Value = 1;
try
{
test.Name = "new name";
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
try
{
test.Value = 5;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
test.FieldIsNotDefault -= OnFieldIsNotDefault;
Display(test);
}
private static void OnFieldIsNotDefault(object sender, EventArgs e)
{
Console.WriteLine("Поле уже установлено");
}
static void Display(TestModel model)
{
Console.WriteLine(
$"{nameof(model.Name)} == {model.Name}; {nameof(model.Value)} == {model.Value};");
}
}
}