Is it possible to set focus to the OK button in the TPrintDialog when it opens?
            Asked
            
        
        
            Active
            
        
            Viewed 154 times
        
    0
            
            
        
        Ken White
        
- 123,280
 - 14
 - 225
 - 444
 
        Bob Penoyer
        
- 65
 - 7
 
- 
                    Not natively, but you can probably use the dialog's `OnShow` event to manually find the OK button and set focus on it. – Remy Lebeau Nov 12 '20 at 02:37
 - 
                    @RemyLebeau isn't the PrintDialog taken from printer driver? similar to File dialogs taken from OS? Right now to change dialog windows I am using [winapi ... list the childs of the dialog window and do stuff](https://stackoverflow.com/a/21330590/2521214) (like enlarge and dock OpenGL or VCL window with canvas on it to add preview... ) ... I think this way its possible to obtain the handle of the OK button and do whatever is needed directly no computer vision involved ... however If you know better way I am eager to learn ... as you got obviously much better experience with stuff like this ... – Spektre Nov 12 '20 at 07:53
 - 
                    Bob see also this [is ther a way an app can display a message without the use of messagebox API?](https://reverseengineering.stackexchange.com/a/11920/4709) ... Also I think there should be way to pass windows message to the dialog directly passing something like `WM_OK` ... I am not skilled in winapi so I might be wrong but from a quick search here [first hit](https://stackoverflow.com/q/14962081/2521214) which might be all what you need ... – Spektre Nov 12 '20 at 08:04
 - 
                    @Spektre "*isn't the PrintDialog taken from printer driver?*" - no. It is a standard OS dialog. `TPrintDialog` uses the Win32 [`PrintDlg()`](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms646940(v=vs.85)) function. "*I think this way its possible to obtain the handle of the OK button and do whatever is needed directly*" - exactly. When the dialog is initialized, you can manually retreive the `HWND` of the OK button and set focus on it. – Remy Lebeau Nov 12 '20 at 18:44
 - 
                    @RemyLebeau I taught the dialog invoked by winapi is taken from selected printer driver as Each printer has its own dialog style, controls and stuff... – Spektre Nov 12 '20 at 21:37
 - 
                    1@Spektre a *setup* dialog can be per-driver, but the standard *print* dialog is not. The standard dialog can invoke the driver dialog for letting the user set advanced settings. – Remy Lebeau Nov 12 '20 at 21:38
 
1 Answers
2
            In the TPrintDialog::OnShow event, you can manually set focus to the OK button like this:
void __fastcall TMyForm::PrintDialogShow(TObject *Sender)
{
    HWND btnOK = GetDlgItem(PrintDialog->Handle, IDOK);
    ::SetFocus(btnOK);
}
        Remy Lebeau
        
- 555,201
 - 31
 - 458
 - 770