LoginSignup
2
2

More than 5 years have passed since last update.

C#でスクリーンリーダー対応のWindowsデスクトップアプリを開発するときの落とし穴

Posted at

私が色々なサンプルを参考にC#でWindowsのデスクトップアプリを作っていて地味に困ったことをメモしておきます。


問題点

TextBoxやRichTextBoxを配置して入力をするときに
日本語のOn/Offが通常全角半角キーを押す度にスクリーンリーダーによってアナウンスされます。
これが下記のコードではされません。
ただ実行すると入力ボックスとしての役目は果たしています。
ナレータ NVDA で確認しました。

サンプル1

using System;
using System.Drawing;
using System.Windows.Forms;

class form: Form
{

public static void Main()
{
form app=new form();

TextBox box=new TextBox();
box.Parent = app;
box.Width = box.Width*3;
box.Text = "こんにちわ";

Application.Run(app);
}

}


解決策

色々試してみた結果Main関数の前に [STAThread] を入れればOKでした。

サンプル2

using System;
using System.Drawing;
using System.Windows.Forms;

class form: Form
{

[STAThread]
public static void Main()
{
form app=new form();

TextBox box=new TextBox();
box.Parent = app;
box.Width = box.Width*3;
box.Text = "こんにちわ";

Application.Run(app);
}

}


原因

書き加えた [STAThread] は以降のMain関数をシングルスレットで実行するという宣言です。
そもそもGUIアプリを作成するときにはお約束の事項なようです。
この1行がないとシングルスレットにならず、これが原因でスクリーンリーダーに情報が渡されないようです。

STAThread属性

Visual Studio で作成した場合

初めから記入されており、問題ありませんでした。
ただし、TextBoxやRichTextBoxは別ファイルに作成されるため、下記サンプルには記入されていません。

サンプル3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace winapp
{
static class Program
{
///


/// アプリケーションのメイン エントリ ポイントです。
///

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2