趣æ¨
常ã«åé¢ï¼å
¨ç»é¢è¡¨ç¤ºããããããªã¹ã¯çã¢ããªãä½ãããã¦ãã¨ããããåå¼·ããªãã試ä½åãä½ã£ã¦ã¿ãã
ã¦ã¤ã³ãã¦ã¢ããªãªãã¦MFCãã使ã£ããã¨ãªããC#ãªãã¦ç¥ããªãããããã¨ããç¶æ
ããå§ããã®ã§ãå
¨ä½çã«ã»ã³ã¹ç¡ãããã
ä»åã¯ä»¥ä¸æ©è½ãæã£ãã¢ããªãWinUI3ã§ä½ã£ã¦ã¿ãã
- ãã¿ã³æ¼ãã¨è¡¨ç¤ºã¢ã¼ãåãæ¿ãã
- ãã«ã¹ã¯æã¯ã©ã®ã¦ã¤ã³ãã¦ãã¢ã¯ãã£ãã ããã¨ãã®ã¢ããªãåé¢ã«åºã¦ãã
- ãã«ã¹ã¯æã¯ã¿ã¤ãã«ãã¼ãåºããªã
- ãã«ã¹ã¯æã¯ã¿ã¹ã¯ãã¼ã¾ã§é ã
- WebView2ã使ã£ã¦ãã©ã¤ããªã¢ãã¿ã¼ã«ã®ã¿æ¬ããã°ã表示ããã
ãããã¯å®è¡ä¸ãåé¢æã«ä¾å¤ã¨ãçºçãã¦æ¢ã¾ã£ã¡ããã¨ãã¯ä½ãè¦ããªã£ã¡ãã£ã¦ããããªã¨ãã¯Shiftï¼F5ã§ãããã°ã¢ã¼ããçµäºããã°ããã®ã ãã©ãããã«ãã°ããæ°ãä»ããVisualStudioãã¢ã¯ãã£ãã«ãã¦Alt+F4ã§ç¡çããè½ã¨ãã¦ããããã
ã
ã¢ããªã¯æ¬è¨äºå·çå¾ãã¡ããã¡ããæ´æ°ãã¦ããã¤ããã§ãææ°ã®ã³ã¼ãã¯æ°ãåãããGitHubã«ããã·ã¥ãã¦ããããã¨èãä¸ã
URLï¼https://github.com/Elizack/KioskLikeApp
(ããããå
容ã¯ã¯ã¦ãªããã°ããQiitaã¨ãZennã¨ãã«æ稿ããã»ããããã®ããªããã)
XAMLã¯ãã
<?xml version="1.0" encoding="utf-8"?>
<Window
  x:Class="FullScreenAppTest.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="using:FullScreenAppTest"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:controls="using:Microsoft.UI.Xaml.Controls"
  mc:Ignorable="d"
  Title="FullScreenAppTest">
  <Grid x:Name="grid1" HorizontalAlignment="Left" VerticalAlignment="Top">
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="0" Grid.Column="0">
      <Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
      <TextBlock x:Name="textBlock1">tb1</TextBlock>
    </StackPanel>
    <controls:WebView2 x:Name="MyWebView" Grid.Row="1" Grid.Column="0"
              Source="https://kedarake-haidarake.hatenablog.jp/" HorizontalAlignment="Stretch"
              VerticalAlignment="Stretch"/>
  </Grid>
</Window>
CSã¯ãã(NuGetã§WindowsDisplayAPIãå
¥ãã¦ã)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Windows.Foundation;
using Windows.Foundation.Collections;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace FullScreenAppTest
{
  /// <summary>
  /// An empty window that can be used on its own or navigated to within a Frame.
  /// </summary>
  public sealed partial class MainWindow : Window
  {
    // trueã®ã¨ããã«ã¹ã¯ãªã¼ã³ã¢ã¼ãã«ããã
    bool fs_flg = false;
    // ãã«ã¹ã¯ãªã¼ã³åãæ¿ãç´åã®ã¦ã¤ã³ãã¦ä½ç½®ããµã¤ãºãè¦ãã¦ããç¨ã®å¤æ°
    int prev_x = 0, prev_y = 0;
    int prev_width = 0, prev_height = 0;
    public MainWindow()
    {
      this.InitializeComponent();
      _presenter = this.AppWindow.Presenter as OverlappedPresenter;
    }
    private OverlappedPresenter? _presenter;
    private void myButton_Click(object sender, RoutedEventArgs e)
    {
      // ãã¿ã³æ¼ãã¦ç»é¢ã¢ã¼ããåãæ¿ã
      fs_flg = !fs_flg;
      changeScreenMode();
    }
    private void changeScreenMode()
    {
      if (_presenter is not null)
      {
        _presenter.IsAlwaysOnTop = fs_flg & false; // trueã«ããã¨å¸¸ã«åé¢ã«ãªã
        _presenter.SetBorderAndTitleBar(hasBorder: !fs_flg, hasTitleBar: !fs_flg);
        _presenter.IsResizable = !fs_flg; // falseã«ããã¨ç»é¢ãµã¤ãºå¤æ´ä¸å¯ã«ãªã
        _presenter.IsMinimizable = fs_flg; // trueã«ããã¨ãã£ã¹ãã¬ã¤ã®ç«¯ã¾ã§ã¦ã¤ã³ãã¦ãã§ããã§ãã
      }
      if (fs_flg)
      {
        myButton.Content = "ãã«ã¹ã¯ãªã¼ã³ã¢ã¼ã";
        // ãã«ã¹ã¯ãªã¼ã³ã«ããç´åã®ã¦ã¤ã³ãã¦ä½ç½®ããµã¤ãºãè¦ãã¦ãã(å度ãã¿ã³æ¼ããã¨ãã«æ»ããã)
        prev_x = this.AppWindow.Position.X;
        prev_y = this.AppWindow.Position.Y;
        prev_width = this.AppWindow.ClientSize.Width;
        prev_height = this.AppWindow.ClientSize.Height;
         // WebView2ç¨Gridã®ãµã¤ãºããã©ã¤ããªã¢ãã¿ã¼ã«åããããã®ãã©ã¡ã¼ã¿
        double grid_width = 0, grid_height = 0;
        // ãã£ã¹ãã¬ã¤ã®å·¦ä¸ã®åº§æ¨ã¨ããµã¤ãºãåå¾
        int topLeft_x = 0, topLeft_y = 0;
        int width = 0, height = 0;
        foreach(WindowsDisplayAPI.Display disp in WindowsDisplayAPI.Display.GetDisplays())
        {
          // å·¦ä¸åå¾(ãã©ã¤ããªã¢ãã¿ã¼ãå·¦ä¸ã«ç¡ãå ´åãxãyã«ã¯ãã¤ãã¹ãã¯ããã£ã½ã)
          topLeft_x = topLeft_x < disp.CurrentSetting.Position.X ? topLeft_x : disp.CurrentSetting.Position.X;
          topLeft_y = topLeft_y < disp.CurrentSetting.Position.Y ? topLeft_y : disp.CurrentSetting.Position.Y;
          // 縦å¹
ã¨æ¨ªå¹
åè¨ãã
          width += disp.CurrentSetting.Resolution.Width;
          height += disp.CurrentSetting.Resolution.Height;
          // ãã©ã¤ããªã¢ãã¿ã¼ã§ããã°ãWebView2ç¨ã®è¨å®å¤ã«å¤å
¥ãã
          if(disp.CurrentSetting.Position.X == 0 && disp.CurrentSetting.Position.Y == 0)
          {
            grid_width = disp.CurrentSetting.Resolution.Width;
            grid_height = disp.CurrentSetting.Resolution.Height;
          }
        }
        // TextBlockã«ãã£ã¹ãã¬ã¤ã®å·¦ä¸ã¨å³ä¸ã®åº§æ¨ãåºå(確èªç¨)
        textBlock1.Text = "*1";
        // ã¦ã¤ã³ãã¦ã®å·¦ä¸ã(min_x,min_y)ã«ããµã¤ãºã(width,height)ã«ããã
        this.AppWindow.MoveAndResize(new Windows.Graphics.RectInt32(topLeft_x, topLeft_y, width, height));
        // WebViewã®è¡¨ç¤ºç¯å²ããã©ã¤ããªã¢ãã¿ã¼ã«åãã
        grid1.Translation = new Vector3(Math.Abs(topLeft_x), Math.Abs(topLeft_y), 0); // ã¦ã¤ã³ãã¦ã®å·¦ä¸ãåç¹ãããã«å¯¾ããç¸å¯¾ä½ç½®ãæå®ããã
        grid1.Width = grid_width;
        grid1.Height = grid_height;
      }
      else
      {
        myButton.Content = "ã¦ã¤ã³ãã¦ã¢ã¼ã";
        this.AppWindow.MoveAndResize(new Windows.Graphics.RectInt32(prev_x, prev_y, prev_width, prev_height));
        grid1.Translation = new Vector3(0, 0, 0);
        grid1.Width = prev_width;
        grid1.Height = prev_height;
      }
    }
  }
}
Â
Â
Â
Â
Â