質問

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

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

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

ここにも同様の質問がありますが、それは必要以上に複雑で、これに費やしたいよりも少しずつ解析するのに少し時間がかかります。ティア。

役に立ちましたか?

解決

Spy ++を使用して、ダイアログを見てください。クラス名は重要で、ボタンの制御IDです。ネイティブのWindowsダイアログの場合、クラス名は「#32770」である必要があります。その場合、あなたは私の投稿に多くの使用を行うことができます このスレッド. 。ここは 別のc#. 。ボタンハンドルでp/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();
        }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top