0

I am making a function that uses TaskDialogIndirect() to construct more advanced Message Boxes. I want to be able to pass custom buttons to the function. For that, I am using an array of TASKDIALOG_BUTTON as a parameter. The only problem is that I am getting an error and I do not know what it means:

C2061 - Syntax Error: identifier 'TASKDIALOG_BUTTON'

I could not find any information on the Internet about this exact issue.

CMessageBox.hpp:

#pragma once

#include "Global.hpp"

struct TaskDialogData
{
    int X;
    int Y;
};


class CMessageBox
{
    public:
        static int MessageBoxPosL(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType, int X, int Y, PCWSTR Icon, TASKDIALOG_BUTTON CustomBtns[], TASKDIALOG_COMMON_BUTTON_FLAGS StandardBtns = NULL, TASKDIALOG_FLAGS flags = NULL);
};

CMessageBox.cpp:

#include "CMessageBox.hpp"

static HRESULT CALLBACK TaskDialogCallback(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, LONG_PTR lpRefData)
{
    if (msg == TDN_DIALOG_CONSTRUCTED) // This sets the position of the MessageBox
    {
        TaskDialogData* data = (TaskDialogData*)lpRefData;
        SetWindowPos(hwnd, NULL, data->X, data->Y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
    }
    else if (msg == TDN_HYPERLINK_CLICKED) { // Opens all hyperlinks in default browser
        LPCTSTR url = (LPCTSTR)lParam;
        ShellExecute(NULL, _T("open"), url, NULL, NULL, SW_SHOW);
    }
    return S_OK;
}

int CMessageBox::MessageBoxPosL(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType, int X, int Y, PCWSTR Icon, TASKDIALOG_BUTTON CustomBtns[], TASKDIALOG_COMMON_BUTTON_FLAGS StandardBtns, TASKDIALOG_FLAGS flags)
{
    TaskDialogData data;
    data.X = X;
    data.Y = Y;
    
    TASKDIALOGCONFIG config = {};
    config.cbSize = sizeof(config);
    config.hwndParent = hWnd;                           // Parent window; SHOULD BE NORMALLY NULL!
    config.pszWindowTitle = (PCWSTR)lpCaption;
    config.pszContent = (PCWSTR)lpText;
    config.pfCallback = &TaskDialogCallback;            // Callback function
    config.lpCallbackData = (LONG_PTR)&data;            // Data sent to callback function
    config.dwFlags = TDF_ENABLE_HYPERLINKS | flags;
    config.dwCommonButtons = StandardBtns;
    config.pButtons = CustomBtns;
    config.cButtons = _ARRAYSIZE(CustomBtns);
    config.pszMainIcon = Icon;

    int button = 0;
    TaskDialogIndirect(&config, &button, NULL, NULL);
    return button; //This returns the Button that was clicked in the TaskDialog
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Davide_24
  • 67
  • 7
  • 2
    You have to get the #includes correct. "global.hpp" is a practice you'll regret sooner or later. Sooner. – Hans Passant Jul 31 '22 at 19:11
  • Also, your `(PCWSTR)` type casts are dangerous. Get rid of them. If the code is compiled with `UNICODE` defined then `LPCTSTR` will be compatible as-is with `PCWSTR` so the casts will be redundant. But if the code is not compiled with `UNICODE` defined then `LPCTSTR` will not be compatible with `LPCWSTR` and your type casts will invoke **undefined behavior** rather then letting the compile fail. Since `TASKDIALOGCONFIG` doesn't support `LPCSTR` strings anyway, you shouldn't be using `LPCTSTR` in this code at all. – Remy Lebeau Jul 31 '22 at 20:33
  • Stop using TCHAR and use compile everything as unicode: https://stackoverflow.com/a/50572941/104458 – selbie Aug 01 '22 at 02:23

0 Answers0