【问题标题】:BoldDays with TMonthCalendarBoldDays 与 TMonthCalendar
【发布时间】:2012-10-09 06:25:41
【问题描述】:

我编写了一个 C++ Builder VCL Forms 应用程序,其中包含一个名为 TMonthCalendar 的 TMonthCalendar 控件。

我想将控件设置为粗体。

这是我当前的代码:

TMonthCalendar->BoldDays([1,8], MonthBoldInfo);

但是我收到以下错误:

E2193 Too few parameters in call to '_fastcall TCommonCalendar::BoldDays(unsigned int *,const int,unsigned int &)'

我可以帮忙吗?

这里是文档的链接:http://docwiki.embarcadero.com/Libraries/XE3/en/Vcl.ComCtrls.TMonthCalendar.OnGetMonthInfo

我发现我的代码和文档之间没有区别。但是我仍然遇到错误。

谢谢

更新

我正在尝试以下代码:

unsigned int arr[2] = {1,8};
TMonthCalendar->BoldDays(arr, 1, MonthBoldInfo);

但出现以下错误:

[BCC32 错误] Assessment2.cpp(361): E2357 引用初始化为“unsigned long”,需要“unsigned int”类型的左值 完整的解析器上下文 Assessment2.cpp(359): 解析: void _fastcall TformMain::TMonthCalendarGetMonthInfo(TObject *,unsigned long,unsigned long &)

[BCC32 错误] Assessment2.cpp(361): E2342 参数“MonthBoldInfo”中的类型不匹配(需要“unsigned int &”,得到“unsigned long”) 完整的解析器上下文 Assessment2.cpp(359): 解析: void _fastcall TformMain::TMonthCalendarGetMonthInfo(TObject *,unsigned long,unsigned long &)

更新

我想从向量中检索某个月份的所有天数,然后通过 TMonthCalendar 控件将天数设置为粗体。

这是我的代码:

vector<appointment> appointmentsOnMonth = calCalendar.getAllAppointmentsOnMonth(TMonthCalendar->Date);
if (appointmentsOnMonth.size() > 0)
{
    unsigned int arr[appointmentsOnMonth.size()];
    for (int i = 0; i < appointmentsOnMonth.size(); i++)
    {
        int dayOfAppointment = DayOf(appointmentsOnMonth[i].getAppDateTime());
        arr[i] = dayOfAppointment;
    }
    TMonthCalendar->BoldDays(arr, 1, reinterpret_cast<unsigned int&>(MonthBoldInfo));
}

dayOfAppointment 变量工作正常,并获取应以粗体显示的天数的整数值。我正在寻求一些帮助,请将这些日子显示为粗体。

我遇到了一些与 unsigned int arr[] 和显示粗体天数有关的错误。 他们在这里:

[BCC32 错误] Assessment2.cpp(366): E2313 需要常量表达式 [BCC32 错误] Assessment2.cpp(372): E2034 无法将 'int[1]' 转换为 'unsigned int *'

我认为这是因为静态数组需要编译时常量,因此第二个代码永远不会编译。有没有办法解决这个问题?

【问题讨论】:

    标签: c++ c++builder monthcalendar


    【解决方案1】:

    C++ 中BoldDays() 的前两个参数在Delphi 中由一个开放数组参数组成。开放数组由数据指针和指向数据的最大索引组成。在 C++ 中,不能使用 [1,8] 语法。那是 Delphi 语法。在 C++ 中,请改用 ARRAYOFCONST()OPENARRAY() 宏,例如:

    TMonthCalendar->BoldDays(ARRAYOFCONST((1,8)), MonthBoldInfo);
    

    或者:

    TMonthCalendar->BoldDays(OPENARRAY(unsigned int, (1,8)), MonthBoldInfo);
    

    或者,只需使用您自己的数组手动声明参数值:

    unsigned int arr[2] = {1,8};
    TMonthCalendar->BoldDays(arr, 1, MonthBoldInfo);
    

    更新OnGetMonthInfo 事件的MonthBoldInfo 参数是unsigned long&amp;,但BoldDays() 取而代之的是unsigned int&amp;。通过引用传递值时,数据类型需要匹配。你有两个选择:

    1) 使用中间变量:

    unsigned int arr[2] = {1,8};
    unsigned int days;
    TMonthCalendar->BoldDays(arr, 1, days);
    MonthBoldInfo = days;
    

    2) 使用类型转换:

    unsigned int arr[2] = {1,8};
    TMonthCalendar->BoldDays(arr, 1, reinterpret_cast<unsigned int&>(MonthBoldInfo));
    

    更新:您不能使用运行时值声明静态固定长度数组。您必须改用动态分配的数组。由于您已经在使用std::vector,您可以将其用于数组:

    vector<appointment> appts = calCalendar.getAllAppointmentsOnMonth(TMonthCalendar->Date);
    if (!appts.empty())
    {
        vector<unsigned int> arr(appts.size());
        for (vector<appointment>::iterator i = appts.begin(); i != appts.end(); ++i)
        {
            arr[i] = DayOf(i->getAppDateTime());
        }
        TMonthCalendar->BoldDays(&arr[0], arr.size()-1, reinterpret_cast<unsigned int&>(MonthBoldInfo));
    }
    

    话虽如此,OnGetMonthInfo 事件用于检索所有年份中给定月份的粗体日期,即重复事件,因此像您一样使用TMonthCalendar::Date 属性没有意义。您应该改用提供的Month 参数:

    vector<appointment> appts = calCalendar.getAllAppointmentsOnMonth(Month);
    

    要为特定年份的给定月份设置粗体日期,请改用OnGetMonthBoldInfo 事件,它为您提供MonthYear 参数:

    vector<appointment> appts = calCalendar.getAllAppointmentsOnMonthOfYear(Month, Year);
    

    【讨论】:

    • 谢谢雷米。我已经尝试了您的代码,但遇到了一些问题。我已经更新了我原来的帖子。请看一看。
    • 谢谢雷米。你能看看我更新的问题吗?
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多