Вопрос

Ищете подсказки, советы и условия поиска для изменения текста в окне Win32 из C #.

Более конкретно, я пытаюсь изменить текст в диалоговом окне «Печать» на «ОК», так как я использую диалоговое окно для создания печати билета и не какая-либо печать.

Как я могу найти ручку окна диалога? Как только у меня есть, как бы я пошел по нахождению кнопки в детских окнах формы? Как только я нашел это, как бы я изменил текст на кнопку? И как я могу сделать все это до того, как показано диалог?

Здесь здесь похожий на подобный вопрос, но он указывает на соответствующую статью CodeProject, которая является WAAAY более сложным, чем необходима, и меня немного дольше, чтобы разобраться, чем я хотел бы потратить на это. TIA.

Это было полезно?

Решение

Вы должны использовать Spy ++, чтобы взглянуть на диалог. Имя класса важно и идентификатор управления кнопкой. Если это родная диалоговое окно 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