GetWindowText()函数未从输入(“ EDIT”类型的窗口)获取/输出任何文本

时间:2019-01-25 10:05:48

标签: c winapi

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

     #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 个答案:

答案 0 :(得分:1)

您的代码中有几个问题:

  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中进行编译。
  • 您应该正确设置变量声明的格式,这很混乱。