c - GetWindowText() 函数未从 INPUT ("EDIT"类型窗口获取/输出任何文本)

标签 c winapi

我一直在制作反馈表,需要将数据记录到文本文件中。为此,我在 ANSI(?) C 编程(不是 C++ 和 C#)中使用 WIN32 (windows.h),并使用 GNU 编译器编译 CODE::BLOCKS 中的代码。 我面临的问题是,我无法记录任何“编辑”子窗口的数据输入(第 15 行:HWND NAME、ROLLNO、CASE_REPORT),以便每当用户按下“时,我都可以让消息框打印它们”保存文本”按钮。 我不知道如何在此处添加代码片段,因此我将整个有问题的代码粘贴到问题中。

     #if defined(UNICODE) && !defined(_UNICODE)
        #define _UNICODE
    #elif defined(_UNICODE) && !defined(UNICODE)
        #define UNICODE

#endif

#include <tchar.h>
#include <windows.h>

//Global Variables are declared here
#define ID_CANCEL 1
#define ID_SENDRPT 2
#define ID_SAVETXT 3
int WINWIDTH=460,WINHEIGHT=600;                                                                                 //Window Width and Window Height
HWND NAME,ROLLNO,CASE_REPORT,BTN_SEND,BTN_CANCEL,BTN_SAVE;                                                      //Declaring names of various elements
HWND NAME_LABEL,ROLLNO_LABEL,LABEL_WARNING,LABEL_CASE_REPORT,LABEL_EMAIL_INFO;                                  //for our application
int _inputy=20,_rollnoy=_inputy+21,_case_report_y=_rollnoy+21;                                                  //Position information for our elements

/*Declaration of User Defined Functions*/
void AGIMIX_MB_INFO(HWND hwnd,const char *_title,const char *_message);
void AGIMIX_MB_ERROR(HWND hend, const char *_title, const char *_message);
void AGIMIX_USR_INPUT(HWND hnd,HWND hwnd, const char *_default_text,int _xpos,int _ypos,int _length,int _ht);
void AGIMIX_PUT_TXT(HWND hnd,HWND hwnd, const char *_text,int _xpos,int _ypos,int _length,int _ht);
void AGIMIX_PUT_BUTTON(HWND hnd,HWND hwnd,HMENU _identify, const char *_text,int _xpos,int _ypos,int _length,int _ht);
void AGIMIX_USER_TXTFIELD(HWND hnd,HWND hwnd, const char *_text,int _xpos,int _ypos,int _length,int _ht);
/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("AGIMIX_WINDOWS_APP_FEEDBACK");
char *input_name;
int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */
 /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND+5;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilities for variation */
           szClassName,         /* Class name */
           _T("AGIMIX FEEDBACK FORM"),       /* Title Text */
           WS_OVERLAPPED|WS_SYSMENU, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           WINWIDTH,                 /* The programs width */
           WINHEIGHT,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
    case WM_CREATE:                                                                                                                                         //What to do when the application window is created
       AGIMIX_MB_INFO(hwnd,"AGIMIX [INFORMATION]","Welcome");                                                                                               //Welcome user with a Message Box
       AGIMIX_MB_ERROR(hwnd,"AGIMIX [WARNING: TRIAL VERSION]","This is a Educational / Trial version only, No functionality is supported!");                //Warn user about limited functionality
       AGIMIX_PUT_TXT(LABEL_WARNING,hwnd," All fields marked with \' * \' are compulsory",0,0,450,19);                                                      //STATIC type to Warn user about compulsory fields to be filled
       AGIMIX_PUT_TXT(NAME_LABEL,hwnd," NAME (FULL CAPS)*: ",0,_inputy,150,20);                                                                             //STATIC type to prompt user for his Name in Full Capital letters
       AGIMIX_USR_INPUT(NAME,hwnd,"",150,_inputy,300,20);                                                                                                   //EDIT type text-field to take input from user
       AGIMIX_PUT_TXT(ROLLNO_LABEL,hwnd," LICENCE NUMBER*: ",0,_rollnoy,150,20);                                                                            //STATIC type to prompt user for his Licence Number
       AGIMIX_USR_INPUT(ROLLNO,hwnd,"",150,_rollnoy,300,20);                                                                                                //EDIT type text-field to take input from user
       AGIMIX_PUT_TXT(LABEL_CASE_REPORT,hwnd," FEEDBACK / COMPLAINT*: ",0,_case_report_y,450,20);                                                           //STATIC type to prompt user for his complaint / feedback information
       AGIMIX_USER_TXTFIELD(CASE_REPORT,hwnd,"",0,_case_report_y+21,450,350);                                                                               //EDIT type text-field with added functionality of Multi-line input and Scrolling for inputting detail feedback / complaint
       AGIMIX_PUT_TXT(LABEL_EMAIL_INFO,hwnd,"     Feedback will be responded to Registered Email with Licence.",0,_case_report_y+380,450,21);               //STATIC type to warn user about preregistered email response to the complaint
       AGIMIX_PUT_BUTTON(BTN_SEND,hwnd,(HMENU)ID_SENDRPT,"SEND",50,_case_report_y+430,100,50);                                                              //BUTTON type input for Sending information to AGIMIX server
       AGIMIX_PUT_BUTTON(BTN_SAVE,hwnd,(HMENU)ID_SAVETXT,"SAVE TXT",170,_case_report_y+430,100,50);                                                         //BUTTON type input for Saving the information in a (.txt) file
       AGIMIX_PUT_BUTTON(BTN_CANCEL,hwnd,(HMENU)ID_CANCEL,"CANCEL && EXIT",290,_case_report_y+430,105,50);                                                  //BUTTON type input to Cancel the current complaint/feedback and exit the window/application
                                                                                                                                                            //3 GetWindowText(NAME,input_name,50);
    break;

    case WM_COMMAND:
        //TCHAR input_name[100];
        if(LOWORD(wParam) == ID_CANCEL)
        {
            AGIMIX_MB_ERROR(hwnd,"AGIMIX [WARNING: TRIAL VERSION]","This is a Educational / Trial version only, No functionality is supported!");           //Warn user about limited functionality
            AGIMIX_MB_INFO(hwnd,"AGIMIX [INFORMATION]","Exit Success! Bye.");                                                                               //Give user a parting Message
            PostQuitMessage (0);                                                                                                                            /* send a WM_QUIT to the message queue */
        }
        if(LOWORD(wParam) == ID_SENDRPT)
        {
            AGIMIX_MB_INFO(hwnd,"AGIMIX [FUNCTION SUPPORT]","Function is not supported as of now and will be available later!");
        }
        if(LOWORD(wParam) == ID_SAVETXT)
        {
            GetWindowText(NAME,input_name,50);
            AGIMIX_MB_INFO(hwnd,input_name,input_name);
            SetWindowText(hwnd,input_name);
            AGIMIX_MB_INFO(hwnd,"AGIMIX [ENTERED INFO]",input_name);
            AGIMIX_MB_INFO(hwnd,"AGIMIX [FUNCTION SUPPORT]","Function is not supported as of now and will be available later!");
        }
    break;

    case WM_DESTROY:                                                                                                                                        //What to do when the user exits the application
            AGIMIX_MB_ERROR(hwnd,"AGIMIX [WARNING: TRIAL VERSION]","This is a Educational / Trial version only, No functionality is supported!");           //Warn user about limited functionality
            AGIMIX_MB_INFO(hwnd,"AGIMIX [INFORMATION]","Exit Success! Bye.");                                                                               //Give user a parting Message
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
    break;

    default:                          /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
    return 0;
}
// User Defined Functions for easy functionality
void AGIMIX_MB_INFO(HWND hwnd,const char *_title,const char *_message)
{
    MessageBox(hwnd,_message,_title,MB_ICONINFORMATION|MB_OK);
}

void AGIMIX_MB_ERROR(HWND hwnd, const char *_title, const char *_message)
{
    MessageBox(hwnd,_message,_title,MB_ICONERROR|MB_OK);
}

void AGIMIX_USR_INPUT(HWND hnd,HWND hwnd, const char *_default_text,int _xpos,int _ypos,int _length,int _ht)
{
    hnd = CreateWindow("EDIT",_default_text,WS_VISIBLE|WS_CHILD|WS_BORDER,_xpos,_ypos,_length,_ht,hwnd,NULL,NULL,NULL);
}

void AGIMIX_PUT_TXT(HWND hnd,HWND hwnd, const char *_text,int _xpos,int _ypos,int _length,int _ht)
{
    hnd = CreateWindow("STATIC",_text,WS_VISIBLE|WS_CHILD,_xpos,_ypos,_length,_ht,hwnd,NULL,NULL,NULL);
}
void AGIMIX_USER_TXTFIELD(HWND hnd,HWND hwnd, const char *_text,int _xpos,int _ypos,int _length,int _ht)
{
    hnd = CreateWindow("EDIT",_text,WS_VISIBLE|WS_CHILD|WS_BORDER|ES_MULTILINE|ES_AUTOVSCROLL|WS_VSCROLL,_xpos,_ypos,_length,_ht,hwnd,NULL,NULL,NULL);
}
void AGIMIX_PUT_BUTTON(HWND hnd,HWND hwnd,HMENU _identify, const char *_text,int _xpos,int _ypos,int _length,int _ht)
{
    hnd = CreateWindow("BUTTON",_text,WS_VISIBLE|WS_CHILD|WS_BORDER,_xpos,_ypos,_length,_ht,hwnd,_identify,NULL,NULL);
}

基本上,每当用户按下任何“SEND”或“SAVETXT”按钮时,“NAME”、“ROLLNO”和“CASE_REPORT”窗口中的内容都应记录在 char *input_name (LINE 31) 变量中) 用于进一步处理,例如 MessageBox() 或保存 txt 文件。

最佳答案

您的代码中有几个问题:

  1. input_name 是一个从未初始化过的指针,因此它指向任何地方。您应该使用 TCHAR input_name[100]; 或类似的内容。
  2. AGIMIX_USR_INPUT 中修改 hnd 是没有意义的,参数在 C 中按值传递。相反,您应该返回创建的窗口句柄,见下文

更正了 AGIMIX_USR_INPUT 函数

HWND AGIMIX_USR_INPUT(HWND hwnd, const char *_default_text, int _xpos, int _ypos, int _length, int _ht)
{
  return CreateWindow("EDIT", _default_text, WS_VISIBLE | WS_CHILD | WS_BORDER, _xpos, _ypos, _length, _ht, hwnd, NULL, NULL, NULL);
}

这样调用它:

NAME = AGIMIX_USR_INPUT(hwnd, "", 150, _inputy, 300, 20);

这对于其他类似的 AGIMIX_ 函数也是有效的。

调用GetWindowText喜欢这样:

GetWindowText(NAME, input_name, _countof(input_name));

其他几点:

  • 您不应使用全部大写的标识符。只有宏才应该全部大写。这是 C 语言的约定。
  • 您应该将代码分解为几个更简单的函数。
  • 您混淆了 TCHARchar。如果您以 ANSI 模式编译您的程序,则它可以运行,但它甚至无法以 UNICODE 模式编译。
  • 您应该正确设置变量声明的格式,现在一团糟。

关于c - GetWindowText() 函数未从 INPUT ("EDIT"类型窗口获取/输出任何文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54363002/

相关文章:

c - 不明白将字符串从一个文件反转到另一个文件

c++ - Winapi WriteFile函数写一个字节

c++ - MoveToEx 和 LineTo 不使用存储在顶点 (push_back) win32 c++ 中的 MAKEPOINTS 绘制

winapi - 如何让鼠标光标不可见?

c - 当我们可以选择使变量成为外部变量时,为什么我们要通过引用传递?

c - strcpy:在 union 中使用 char** 变量作为参数

c - main() 中的无限循环

c - 从c中的输入字符串中删除字符

c++ - 如何检索 USB 设备接口(interface) GUID?

c - 为什么这不编译?