C++为什么 setWindowText 不显示任何文本?

C++ why setWindowText doesn't display any text?

本文关键字:任何 文本 显示 为什么 setWindowText C++      更新时间:2023-10-16

我有如下代码,我的问题是为什么在单元格 A[0][0] setWindowText 中什么都不放?

if(LOWORD( wParam ) == 104){
    int td;
    int td_width=80;
    int tr = 0;
    int tr_height=20; 
    for (tr=0;tr<2;tr++) {
        for (td=0;td<10;td++) {
            HWND A[tr][td];
            A[tr][td] = CreateWindowEx( 0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL,
td*td_width, tr*tr_height+50, td_width+1, tr_height+1, hwnd, NULL, hInstance, NULL );
        }
        SetWindowText(A[0][0], "MK" );
    }
}

你在最内层的循环中定义 A[tr][td]。 这会将其范围限制为该循环。 换句话说,在每次迭代中,您都会创建一个全新的数组并仅分配其中一个元素。

这甚至编译有点令人惊讶。 我猜你在其他地方定义了另一个 A 数组,这就是你在 SetWindowText 调用中引用的那个数组。

正如彼得所说,你在错误的位置声明了你的数组。 但更重要的是,您还将数组声明为固定长度,但使用运行时值来指定其边界。这是行不通的,甚至不应该编译。 固定长度数组的边界必须在编译时知道,而不是在运行时知道。

试试这个:

if (LOWORD(wParam) == 104)
{
    const int td_width = 80;
    const int tr_height = 20; 
    HWND A[2][10];
    for (int tr = 0; tr < 2; ++tr)
    {
        for (int td = 0; td < 10; ++td)
        {
            A[tr][td] = CreateWindowEx( 0, TEXT("EDIT"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL, td*td_width, tr*tr_height+50, td_width+1, tr_height+1, hwnd, NULL, hInstance, NULL );
        }
    }
    SetWindowText(A[0][0], TEXT("MK") );
}