4

Win32ウィンドウのテキストをC#から変更するためのヒント、ヒント、および検索用語を探しています。

具体的には、印刷ダイアログを使用して印刷チケットを作成し、印刷を行わないため、印刷ダイアログのテキストを「印刷」から「OK」に変更しようとしています。

ダイアログのウィンドウハンドルを見つけるにはどうすればよいですか?取得したら、フォームの子ウィンドウでボタンを見つけるにはどうすればよいですか?それを見つけたら、ボタンのテキストをどのように変更しますか?そして、ダイアログが表示される前に、どうすればこれをすべて行うことができますか?

ここにも同様の質問がありますが、CodeProjectの記事は、必要以上に複雑で、これに費やしたいよりも解析に少し時間がかかっていることを示しています。TIA。

4

1 に答える 1

9

ダイアログを確認するには、Spy++を使用する必要があります。クラス名とボタンのコントロールIDは重要です。ネイティブのWindowsダイアログの場合、クラス名は「#32770」である必要があります。その場合、このスレッドでの私の投稿に多くの用途があります。これがC#のもう1つです。ボタンハンドルでP/Invoking SetWindowText()を使用して、ボタンテキストを変更します。


using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class SetDialogButton : IDisposable {
    private Timer mTimer = new Timer();
    private int mCtlId;
    private string mText;

    public SetDialogButton(int ctlId, string txt) {
        mCtlId = ctlId;
        mText = txt;
        mTimer.Interval = 50;
        mTimer.Enabled = true;
        mTimer.Tick += (o, e) => findDialog();
    }

    private void findDialog() {
        // Enumerate windows to find the message box
        EnumThreadWndProc callback = new EnumThreadWndProc(checkWindow);
        if (!EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero)) mTimer.Enabled = false;
    }
    private bool checkWindow(IntPtr hWnd, IntPtr lp) {
        // Checks if <hWnd> is a dialog
        StringBuilder sb = new StringBuilder(260);
        GetClassName(hWnd, sb, sb.Capacity);
        if (sb.ToString() != "#32770") return true;
        // Got it, get the STATIC control that displays the text
        IntPtr hCtl = GetDlgItem(hWnd, mCtlId);
        SetWindowText(hCtl, mText);
        // Done
        return true;
    }
    public void Dispose() {
        mTimer.Enabled = false;
    }

    // P/Invoke declarations
    private const int WM_SETFONT = 0x30;
    private const int WM_GETFONT = 0x31;
    private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp);
    [DllImport("user32.dll")]
    private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp);
    [DllImport("kernel32.dll")]
    private static extern int GetCurrentThreadId();
    [DllImport("user32.dll")]
    private static extern int GetClassName(IntPtr hWnd, StringBuilder buffer, int buflen);
    [DllImport("user32.dll")]
    private static extern IntPtr GetDlgItem(IntPtr hWnd, int item);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern bool SetWindowText(IntPtr hWnd, string txt);
}

使用法:

        using (new SetDialogButton(1, "Okay")) {
            printDialog1.ShowDialog();
        }
于 2010-04-19T15:58:21.017 に答える