qshinoの日記

Powershell関係と徒然なこと

wpfクリップボード

wpfクリップボードを使う。

クリップボードへのテキストコピーは、

Clipboard.SetData(DataFormats.Text, (Object)txet);

取り出すには、同様にGetDataを用いる。

サンプル

// copy string
string textData = "Hinan.";

// To clipboard
Clipboard.SetData(DataFormats.Text, (Object)textData);

参考

System.Windows.Clipboard

Assembly: PresentationCore

https://msdn.microsoft.com/ja-jp/library/system.windows.clipboard(v=vs.110).aspx

http://anis774.net/codevault/clipboardwatcherwpf.html

http://blog.hiros-dot.net/?page_id=3797

http://blog.hiros-dot.net/?page_id=3797

http://dobon.net/vb/dotnet/string/clipboard.html

以上

wpf window owner property

owner property

Windowの親子関係と振る舞い。親子関係は、子のowner propertyに親を設定する事で決まる。親子関係を設定した場合、どちらかのウインドウの振る舞いが他に影響する。未設定の場合は影響しない。また、ユーザープログラムが子ウインドウのownerプロパティを設定する。親のOwnedWindows propertyのアクセスはgetのみであり、外部から設定できない。子のowner propertyが反映される模様。

親子関係

親子関係は自動では設定されず、ユーザープログラムで設定する。

未設定時の振る舞い

どちらかのウインドウのclose/minimize/maximizeによる親子ウインドウへの影響なし。タスクバーではそれぞれ別ウインドウとして表示される。

設定時の振る舞い

和訳 1. 親を最小化するとすべての子も最小化される。 2. 子を最小化しても親には影響なし。 3. 親を最大化すると、親子全てが元に戻る。 4. 親は子を覆わない。 5. ShowDialogで開かれなかった子はモードレス。子を開いても親は入力を受け付ける。 6. 親を閉じると全ての子が閉じられる 7. show()で開いた子に対しては、親を閉じてもClosing eventが送られない。

英文

    1. If an owner window is minimized, all its owned windows are minimized as well.
  • If an owned window is minimized, its owner is not minimized.
  • If an owner window is maximized, both the owner window and its owned windows are restored.
  • An owner window can never cover an owned window.
  • Owned windows that were not opened using ShowDialog are not modal. The user can still interact with the owner window.
  • If you close an owner window, its owned windows are also closed.
  • If an owned window was opened by its owner window using Show, and the owner window is closed, the owned window’s Closing event is not raised.

重要

子をShowDialog()で表示した場合も、親子関係を設定する事。自動では設定されない。

設定方法 C#
// Create a window and make this window its owner
Window ownedWindow = new Window();
ownedWindow.Owner = this;
ownedWindow.Show();

CSS練習

CSSの練習

h1. だけ。

影使ったもの

h1 {
    padding: .5em .75em;
    background-color: #f6f6f6;
    box-shadow: 0 2px 6px rgba(0, 0, 0, .15);
}

番号付き

body {
    counter-reset: secno
}
section {
    counter-increment: secno;
}
h1 {
    position: relative;
    padding: 0 0 .5em 2em;
    border-bottom: 1px solid #ccc;
}
h1::before {
    content: counter(secno);
    position: absolute;
    top: 0;
    left: 0;
    width: 30px;
    height: 30px;
    line-height: 30px;
    border-radius: 100%;
    font-size: .10em;
    text-align: center;
    background-color: #ccc;
    color: #fff;
}

影その二

h1 {
    padding: .5em .75em;
    background: #f4f4f4;
    border-top: 1px dashed #ccc;
    border-bottom: 1px dashed #ccc;
    box-shadow: 0 7px 10px -5px rgba(0, 0, 0, .1) inset;
}

影と番号

body {
    counter-reset: secno;
}
section {
    counter-increment: secno;
}
h1 {
    padding: 0 0 .5em 2em;
    background: #f4f4f4;
    border-top: 1px dashed #ccc;
    border-bottom: 1px dashed #ccc;
    box-shadow: 0 7px 10px -5px rgba(0, 0, 0, .1) inset;
}

h1::before {
    position: absolute;
    top: 0;
    left: 0;
    content: counter(secno);
    width: 30px;
    height: 30px;
    line-height: 30px;
    background-color: #ccc;
    border-radius: 100%;
    color: #fff;
    font-size: .10em;
    text-align: center;
}

参考

https://nelog.jp/wordpress-h1-h6

wpf webbrowser class

wpf webbrowser class

class : System.Windows.Controls.WebBrowser

<Window    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="hdoc" Height="300" Width="300">
  <StackPanel>
    <WebBrowser Name="ax"
      Source="c:¥poi.html" />
  </StackPanel>
</Window>

コード

ソース変更

$ax = $win.FindName("ax")
$ax.Source=uri("c:¥qwe.html")

<!-- Web Browser Control -->
<WebBrowser Name="browser" Source="http://msdn.com" 
  Width="600" Height="600"  />

xaml

<StackPanel>
    <StackPanel Orientation="Horizontal">
        <TextBox Name="address" Width="200" />
        <Button Name="go" Content="Go"/>
    </StackPanel>
    <WebBrowser Name="browser" />
</StackPanel>

C-sharpサンプル

public MainWindow()
{
    InitializeComponent();

    var serviceProvider = (IServiceProvider)webBrowser1.Document;
    if (serviceProvider != null)
    {
        Guid serviceGuid = new Guid("0002DF05-0000-0000-C000-000000000046");
        Guid iid = typeof(SHDocVw.WebBrowser).GUID;
        var webBrowserPtr = (SHDocVw.WebBrowser)serviceProvider
            .QueryService(ref serviceGuid, ref iid);
        if (webBrowserPtr != null)
        {
            webBrowserPtr.NewWindow2 += webBrowser1_NewWindow2;
        }
    }
}

private void webBrowser1_NewWindow2(ref object ppDisp, ref bool Cancel)
{
    // Handle the event.
}

[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
internal interface IServiceProvider
{
    [return: MarshalAs(UnmanagedType.IUnknown)]
    object QueryService(ref Guid guidService, ref Guid riid);
}

参考

https://msdn.microsoft.com/ja-jp/library/system.windows.controls.webbrowser(v=vs.110).aspx

http://www.atmarkit.co.jp/fdotnet/dotnettips/814wpfwebbrowser/wpfwebbrowser.html

html文字セット

html文字セット

<meta http-equiv="Content-Type" content="text/html; charset=">

文字セット一覧。

例)

    <meta http-equiv="Content-Type" content="text/html; charset=Shift-JIS">

参考

http://www.dspt.net/html_tag/001_data/017.html

WPF背景色

wpf背景色

WPFでコントロールに背景色を付ける。

気合いでやるなら、このぐらい。

<Window x:Class="BackgroundColorChange.MainWindow"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 Title="Keypad" Height="500" Width="500">
 <Grid Height="306" ButtonBase.Click="button_Click" UIElement.GotFocus="textBox1_GotFocus">
  <TextBox Height="23" HorizontalAlignment="Left" Margin="24,27,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="0" Background="AliceBlue">
   <TextBox.Style>
    <Style TargetType="{x:Type TextBox}">
     <Style.Triggers>
      <Trigger Property="IsFocused" Value="true">
       <strong><Setter Property="Background"></strong>
        <Setter.Value>
         <LinearGradientBrush>
          <GradientStop Color="Red"/>
         </LinearGradientBrush>
        </Setter.Value>
       </Setter>
      </Trigger>
     </Style.Triggers>
    </Style>
   </TextBox.Style>
  </TextBox>
  <TextBox Height="23" HorizontalAlignment="Left" Margin="24,56,0,0" Name="textBox2" Text="0" VerticalAlignment="Top" Width="120" Background="AliceBlue" />
  <TextBox Height="23" HorizontalAlignment="Left" Margin="24,85,0,0" Name="textBox3" Text="0" VerticalAlignment="Top" Width="120" Background="AliceBlue" />
  <Button Focusable="False" Content="1" Height="25" HorizontalAlignment="Left" Margin="303,24,0,0" Name="button2" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="2" Height="25" HorizontalAlignment="Left" Margin="303,55,0,0" Name="button5" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="3" Height="25" HorizontalAlignment="Left" Margin="303,86,0,0" Name="button8" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="4" Height="25" HorizontalAlignment="Left" Margin="351,24,0,0" Name="button1" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="5" Height="25" HorizontalAlignment="Left" Margin="351,55,0,0" Name="button3" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="6" Height="25" HorizontalAlignment="Left" Margin="351,86,0,0" Name="button4" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="7" Height="25" HorizontalAlignment="Left" Margin="401,24,0,0" Name="button7" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="8" Height="25" HorizontalAlignment="Left" Margin="401,55,0,0" Name="button9" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="9" Height="25" HorizontalAlignment="Left" Margin="401,86,0,0" Name="button10" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="0" Height="25" HorizontalAlignment="Left" Margin="351,117,0,0" Name="button6" VerticalAlignment="Top" 

参考

https://social.msdn.microsoft.com/Forums/vstudio/en-US/a8c18880-f21c-40e5-a8bc-f1a9aaae7635/how-to-change-textbox-background-color-on-xaml?forum=wpf

http://stackoverflow.com/questions/1441059/changing-background-color-of-container-when-textbox-is-in-focus

http://stackoverflow.com/questions/15399622/accessing-background-color-of-textblock

WPF ShowDialog()中の非同期イベント

ShowDialog()中に非同期イベントを処理できない。

PowershellWPFのShowDialog()を処理中に非同期イベントが発生しても処理されない件、原因不明ながら回避策にて対応。

なお、イベントの発生と処理は別物。発生時刻をイベントのプロパティTimeGeneratedで確認。処理時刻をハンドラでの(Get-Date)で確認する。

まずは問題

$win = xxx でWindow生成 $win.ShowDialog()

UI内のイベントで Start-Process のExited EventをRegister-ObjectEventで登録。これはUIスレッドで発生しないため、非同期イベント。

プロセスが終了してもUIスレッドで処理されない。

但し、UIスレッド内でGet-Eventした時に前記イベント発生。

イベントの生成時刻プロパティを見ると、UIスレッドで処理された時刻よりかなり前に発生していた。

  • 対策1 UIのボタンなどの処理で、Get-Eventする。
  • 対策2 DispatcherTimerで定期的にイベント処理を発生させる。

対策1

単純にボタンでGet-Event。ボタン名が"kita", Windowが$winとすると、

$kita = $win.FindName("kita")
$kita.add_click( {Get-Event})

kitaボタンを押すと、非同期イベントがあれば処理してくれる。

対策2

タイマーイベントの副作用で、非同期イベントを処理。ある意味、前記のkitaボタンをタイマーが自動で押してくれる様なもの。

$proc = Start-Process cmd -Passthru

$timer = New-Object System.Windows.Threading.DispatcherTimer
$timer.interval = New-Object TimeSpan(0,0,1)
$timer.add_tick({
  Write-Host "tick" 
  if ( $this.tag ){
    # イベント発生済み
    # イベントジョブ削除
    $this.Stop()
    Remove-Job -id ($this.tag).id
    $this.tag = $null
})
$timer.IsEnabled = $true
$timer.Start()
$timer.tag = $null

$ev = Register-ObjectEvent $proc -EventName Exited -Messagedata @{timer=$timer;ev=$ev;dispatcher=[System.Windows.Threading.Dispatcher]::CurrentDispatcher } $timer -Action { 
  $data = $event.MessageData
  $timer = $data.timer
  $timer.tag = $data.ev
  Unregister-Event -SourceIdentifier $EventSubscriber.Name
  # 何らかの後処理。
  # 本処理PSEventJobがJobとして残っているので、どこかで削除する必要あり。
  # UIスレッド処理: $eventは引数
  $data.dispatcher.BeginInvoke({
    # uiスレッド処理
  }, @($event))
}

参考

http://d.hatena.ne.jp/omoisan/touch/20090711/1249783732

http://main.tinyjoker.net/Tech/CSharp/WPF/�̥����åɤ������Ǥ������.html

http://www.atmarkit.co.jp/ait/spv/1411/04/news133.html

http://ari-it.doorblog.jp/archives/30059088.html

Dispatcher class

https://msdn.microsoft.com/ja-jp/library/system.windows.threading.dispatcher(v=vs.110).aspx#メソッド

Peocess Exited event

https://msdn.microsoft.com/ja-jp/library/system.diagnostics.process.exited(v=vs.90).aspx

WPF timer使用例

https://code.msdn.microsoft.com/windowsdesktop/XAMLCVB-WPF-Windows-WPF-2aab6085

DispatcherTimer

https://msdn.microsoft.com/ja-jp/library/ms615945.aspx#継承階層

IsEnabled

https://msdn.microsoft.com/ja-jp/library/system.windows.threading.dispatchertimer.isenabled.aspx

tag property

https://msdn.microsoft.com/ja-jp/library/system.windows.threading.dispatchertimer.tag.aspx

TimeSpan

https://msdn.microsoft.com/ja-jp/library/system.timespan(v=vs.110).aspx

コンストラクター

https://msdn.microsoft.com/ja-jp/library/bk8a3558(v=vs.110).aspx