Question

I'm trying to set the text on the "save" button of the Windows "Save file as..." dialog.

I've set up the hook, received the message, found the button (nb. If I call "GetWindowText()" I see "&Save" so I know it's the right button).

Next I changed the text using "SetWindowText()" (and called "GetWindowText()" to check it - the text is correct).

But ... the button still says "Save".

I can change the "Cancel" button using the exact same code - no problem. What's so special about the "Save" button? How can I change it.

Code (for what it's worth):

static UINT_PTR CALLBACK myHook(HWND hwnd, UINT msg, WPARAM, LPARAM)
{
  if (msg == WM_INITDIALOG) {
    wchar_t temp[100];
    HWND h = GetDlgItem(GetParent(hwnd),IDOK);
    GetWindowTextW(h,temp,100);     // temp=="&Save"
    SetWindowTextW(h,L"Testing");
    GetWindowTextW(h,temp,100);     // temp=="Testing"
  }
}
Was it helpful?

Solution

I finally made it work....

I'm pretty sure there's something funny going on with the "Save" button but this code will wrestle it into submission:

// I replace the dialog's WindowProc with this
static WNDPROC oldProc = NULL;
static BOOL CALLBACK buttonSetter(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    // Set the button text on every window redraw....
    if (msg == WM_ERASEBKGND) {
        SetDlgItemTextW(hwnd,IDOK,L"OK");
    }
    return oldProc(hwnd, msg, wParam, lParam);
};

// This is the callback for the GetWriteName hook
static UINT_PTR CALLBACK GWNcallback(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    HWND dlg = GetParent(hwnd);
    if (msg == WM_INITDIALOG) {
        oldProc = (WNDPROC)GetWindowLongPtr(dlg, GWL_WNDPROC);
        if (oldProc !=0) {
            SetWindowLongPtr(dlg, GWL_WNDPROC, (LONG)buttonSetter);
        }
    }
    // We need extra redraws to make our text appear...
    InvalidateRect(dlg,0,1);
}

OTHER TIPS

You probably need to redraw the window after setting the text.

Try calling UpdateWindow() after setting the text.

Use CDM_SETCONTROLTEXT message to set the text rather than mess with SetWindowText directly, i.e.

SendMessage(hwnd, CDM_SETCONTROLTEXT, IDOK, L"Testing");

http://msdn.microsoft.com/en-us/library/ms646960(VS.85).aspx has more on customizing open/save dialogs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top