実現したいこと
ウィンドウフォームにあるテキストをコピーして、それを別のアプリ(例えばメモ帳)に張り付ける
方法を知りたい。
発生している問題・分からないこと
クリップボードにコピーはできていると思います。
しかし、別のアプリに張り付けしようとしても、別のアプリ(ここではメモ帳)に反応がありません。
該当のソースコード
cs
1namespace WinFormsApp1_2025_01_29_クリップボード 2{ 3 internal static class Program 4 { 5 /// <summary> 6 /// The main entry point for the application. 7 /// </summary> 8 [STAThread] 9 static void Main() 10 { 11 // To customize application configuration such as set high DPI settings or default font, 12 // see https://aka.ms/applicationconfiguration. 13 ApplicationConfiguration.Initialize(); 14 Form1 form1 = new Form1(); 15 Application.Run(form1); 16 } 17 } 18} 19
cs
1using System.Diagnostics; 2using System.Runtime.InteropServices; 3 4namespace WinFormsApp1_2025_01_29_クリップボード 5{ 6 public partial class Form1 : Form 7 { 8 // Win32 APIのインポート 9 [DllImport("user32.dll", SetLastError = true)] 10 public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 11 12 [DllImport("user32.dll", SetLastError = true)] 13 public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); 14 15 [DllImport("user32.dll", SetLastError = true)] 16 public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); 17 18 const int WM_PASTE = 0x0302; // WM_PASTEメッセージ 19 20 21 private TextBox textBox1 = new TextBox(); 22 private TextBox textBox2 = new TextBox(); 23 private Button copyButton = new Button(); 24 private Button pasteButton = new Button(); 25 26 public Form1() 27 { 28 InitializeComponent(); 29 30 copyButton.Text = "コピー"; 31 copyButton.Click += (sender, e) => CopyToClipboard(textBox1.Text); 32 33 pasteButton.Text = "貼り付け"; 34 pasteButton.Click += (sender, e) => textBox2.Text = PasteFromClipboard(); 35 36 Controls.Add(textBox1); 37 Controls.Add(copyButton); 38 Controls.Add(pasteButton); 39 Controls.Add(textBox2); 40 41 textBox1.SetBounds(0, 0, 100, 100); 42 copyButton.SetBounds(0, 100, 100, 100); 43 textBox2.SetBounds(0, 200, 100, 100); 44 pasteButton.SetBounds(0, 300, 100, 100); 45 } 46 47 private void Form1_Load(object sender, EventArgs e) 48 { 49 50 } 51 52 53 54 public void CopyToClipboard(string text) 55 { 56 if ( text != null ) 57 Clipboard.SetText(text); 58 59 // メモ帳を起動 60 Process notepad = new Process(); 61 notepad.StartInfo.FileName = "notepad.exe"; 62 notepad.Start(); 63 64 // メモ帳が起動するのを待つ 65 notepad.WaitForInputIdle(); 66 67 68 // メモ帳のウィンドウハンドルを取得 69 IntPtr notepadHandle = FindWindow("Notepad", null); 70 if (notepadHandle != IntPtr.Zero) 71 { 72 // メモ帳に貼り付け 73 SendMessage(notepadHandle, WM_PASTE, IntPtr.Zero, IntPtr.Zero); 74 // (しかし、メモ帳に反応なし) 75 } 76 } 77 public string PasteFromClipboard() 78 { 79 Debug.WriteLine(Clipboard.GetText()); 80 return Clipboard.GetText(); 81 } 82 83 public void PasteToActiveWindow() 84 { 85// SendKeys.SendWait("^v"); 86 } 87 } 88 89} 90
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
AIに聞いてみたところ、プロセスを取得する方法を提示されたので、やってみましたが、
うまくいきません。
メモ帳は起動するので、// メモ帳を起動 のところはうまくいってます。
しかし、貼り付けを SendMessage()関数で実現しようとしても、反応がありません。
補足
OS: Windows 11 pro
言語: C#
FW: Windows Form
IDE: Visual Studio 2022 Community
動作を想定するのは、Windows 環境だけでいいです。

回答1件
あなたの回答
tips
プレビュー