簡體   English   中英

WinAPI從編輯控件中檢索文本

[英]WinAPI Retrieving text from Edit Control

這個問題使我已經把頭發拔了

我已經閱讀了有關此主題的每一個質量檢查,每個人都有相同的解決方案,但是它永遠不會對我有用。 我的結論是,由於我是新手,因此我肯定會缺少其他答案中未提及的顯而易見的東西,因此請盡可能詳細地回答。

我有一個使用WinAPI的C ++程序。 我有一個名為hwnd的窗口,此窗口有2個按鈕和2個“編輯控件”文本框。 此時,我要做的就是將在文本框中輸入的文本保存到.txt文件中。

各種答案(和Microsoft網站)都說要使用

SendMessage(editcontroltag,WM_GETTEXT,0,LPARAM緩沖區)

或GetWindowText(editcontroltag,緩沖區,大小)

和更多。 我已經嘗試了所有方法,並且代碼編譯時沒有問題,但是實際上我永遠無法檢索文本框中的文本。 它是空的,或有些亂碼(我嘗試過unicode和ansi,無論哪種方式都發生相同的事情)

亂碼通常看起來像是')6或它的某種變體(對我而言不重要,因此我無法真正確定其來源。

我嘗試將緩沖區轉換為幾乎所有允許的數據類型,並嘗試檢索緩沖區及其地址(該地址顯示正常)。 仍然沒有運氣。

我將在這里為您粘貼代碼,對不起,它令人難以置信的混亂,您會注意到我已注釋掉許多檢索文本的嘗試。 無論哪種方式都沒有。 每次都會顯示相同的錯誤

謝謝

#include <windows.h>
#include <iostream>
#include <fstream>
#include <TCHAR.H>
#include <stdio.h>
//#include "C:\Users\Eric\Desktop\Documentation\resource.h"
//the following defines are for adding menus without headers or rc files
#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002
#define IDC_MAIN_EDIT 101
#define ID_BUTTON 9003
#define ID_EDITCHILD1 100
#define ID_EDITCHILD2 102
#define BUFFER_SIZE 256

using namespace std;

const char g_szClassName[] = "myWindowClass";
BOOL authenticate(char, string);
string bufferToString(char* , int );
BOOL doit = TRUE;
 //Step 4: the Window Procedure

 /*
 Procedure to add items to the main window
 you create the item inside the WM_CREATE function thingy
 look it up online, there are many examples
 This will draw the item in the window, but will not do anything
 next you will need to go to the WM_COMMAND switch statement
 and add a case for the appropriate LWORD of your item

kapish?

 */

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static HWND hwndEDUSER;
    static HWND hwndEDPASS;

    //TCHAR lpszLatin[] =  "Lorem ipsum dolor sit amet, consectetur ";
UpdateWindow(hwnd);
    switch(msg)
    {
        case WM_SIZE:
        {
            HWND hEdit;
            RECT rcClient;

            GetClientRect(hwnd, &rcClient);

            hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
            SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
        }
        break;
        case WM_CREATE:
        {
            HMENU hMenu, hSubMenu;
            HICON hIcon, hIconSm;
            HINSTANCE hInstance;

            if(doit)
                {
            HWND hwndEDUSER = CreateWindowEx(
                                 0,"EDIT",   // predefined class
                                NULL,         // no window title
                                WS_CHILD | WS_VISIBLE |
                                ES_LEFT | ES_MULTILINE ,
                                60, 60, 100, 25,   // set size in WM_SIZE message
                                hwnd,         // parent window
                                (HMENU) ID_EDITCHILD1,   // edit control ID
                                (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
                                NULL);        // pointer not needed

            // Add text to the window.
            SendMessage(hwndEDUSER, WM_SETTEXT, 0, (LPARAM)("username"));
            //SendMessage(hwndEdit, EM_SETLIMITTEXT, 3, NULL);

            HWND hwndEDPASS = CreateWindow(
                                 "EDIT",   // predefined class
                                NULL,         // no window title
                                WS_CHILD | WS_VISIBLE |
                                ES_LEFT,
                                60, 90, 100, 25,   // set size in WM_SIZE message
                                hwnd,         // parent window
                                (HMENU) ID_EDITCHILD2,   // edit control ID
                                (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
                                NULL);        // pointer not needed

            // Add text to the window.
            SendMessage(hwndEDPASS, WM_SETTEXT, 0, (LPARAM)("password"));
            //SendMessage(hwndEdit, EM_SETLIMITTEXT, 3, NULL);


            HWND hwndButton = CreateWindow(
            "BUTTON",  // Predefined class; Unicode assumed
            "Commit",      // Button text
            WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles
            200,         // x position
            10,         // y position
            200,        // Button width
            25,        // Button height
            hwnd,     // Parent window
            NULL,       // No menu.
            (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
            NULL);      // Pointer not needed.


            HWND boobs = CreateWindow(
            "BUTTON",  // Predefined class; Unicode assumed
            "LOG IN",      // Button text
            WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles
            200,         // x position
            35,         // y position
            200,        // Button width
            25,        // Button height
            hwnd,     // Parent window
            (HMENU)ID_BUTTON,       // PREVIOUS COMMENT SAID NO MENU, HOWEVER THIS IS WHERE I ADD THE ID FOR THE BUTTON BEING PRESED
            (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
            NULL);      // Pointer not needed.
                }

           // HWND hWndExample = CreateWindow("EDIT", "Text Goes Here", WS_VISIBLE | WS_CHILD | ES_CENTER, 10,10,100,100, hwnd, NULL, hInstance, NULL);


            hMenu = CreateMenu();

            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");

            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO, "&Go");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Stuff");

            SetMenu(hwnd, hMenu);

            hIcon = static_cast<HICON>(LoadImage(NULL, "C:\menu_one.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE));
            if(hIcon)
                SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
            else
                MessageBox(hwnd, "Could not load large icon!", "Error", MB_OK | MB_ICONERROR);


            hIconSm = static_cast <HICON>(LoadImage(NULL, "C:\menu_two.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE));
            if(hIconSm)
                SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
            elsehttp://msdn.microsoft.com/en-us/library/windows/desktop/ms633520(v=VS.85).aspx
                MessageBox(hwnd, "Could not load small icon!", "Error", MB_OK | MB_ICONERROR);

        }
        break;

        /*case WM_LBUTTONDOWN: //mouse click
        case BN_CLICKED:
        {
            char szFileName[MAX_PATH];
            HINSTANCE hInstance = GetModuleHandle(NULL);

            GetModuleFileName(hInstance, szFileName, MAX_PATH);
            MessageBox(hwnd, "The document has been requested, and will be available shortly", "Document Requested", MB_OK | MB_ICONINFORMATION);
        }*/
        break;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case ID_FILE_EXIT:
                    PostMessage(hwnd, WM_CLOSE, 0, 0);
                break;
                case ID_STUFF_GO:
                    MessageBox(hwnd, "Unavailable", "title", MB_OKCANCEL | MB_ICONINFORMATION );
                break;
                case ID_BUTTON:
                    {

                        //TCHAR user[BUFFER_SIZE];
                        //string user ="";
                        //static char user[256];
                        //char id[256];
                        //((WORD*)user)[0] = BUFFER_SIZE; //cast variable user of type TCHAR into WORD. Set position 0 of said WORD variable to BUFFER_SIZE (needed by EM_GETLINE)
                       // SendMessage(hwndEDUSER, EM_GETLINE, 0, (LPARAM)user);

                        // Edit_GetText(hwndEDUSER,(LPTSTR) user,BUFFER_SIZE);

                        //GetWindowText(hwndEDUSER, (LPSTR)user, 256);
                        //GetDlgItemText(hwndEDUSER, ID_EDITCHILD1, (LPTSTR)user,BUFFER_SIZE);
                        //const char id= *user;

                        int len = SendMessage(hwndEDUSER, WM_GETTEXTLENGTH, 0, 0);
                        char* buffer = new char[len];
                        UpdateWindow(hwnd);
                        SendMessage(hwndEDUSER, WM_GETTEXT, 0, (LPARAM)buffer);
                        //return buffer;
                        string user(buffer);
                        fstream users;
                        users.open("C:\\users\\Eric\\Desktop\\Documentation\\user1.txt");
                        users << user;
                        users.close();
                        doit = FALSE;
                        //PostMessage(hwnd, WM_PAINT, 0, 0);
                        UpdateWindow(hwnd);
                        //char id = (char)user;
                       /* Edit_GetText(hwndEDUSER, (LPTSTR)eyeD,50);
                       string id(eyeD);
                        if(id=="2"){MessageBox(hwnd, "BITTCH", "Document Requested", MB_OK | MB_ICONINFORMATION);} */

                        //string id = (string)user;
                       // authenticate(id, "");
                        //char szFileName[MAX_PATH];
                        //HINSTANCE hInstance = GetModuleHandle(NULL);

                        //GetModuleFileName(hInstance, szFileName, MAX_PATH);
                        MessageBox(hwnd,buffer, "Title", MB_OK | MB_ICONINFORMATION);
                    }
                break;
              /*  case BN_CLICKED:
                    {
                    char szFileName[MAX_PATH];
                    HINSTANCE hInstance = GetModuleHandle(NULL);

                    GetModuleFileName(hInstance, szFileName, MAX_PATH);
                    MessageBox(hwnd, "Commit", "Document Requested", MB_OK | MB_ICONINFORMATION);
                    }
                break;*/
            }
        break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        /*case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);

            // All painting occurs here, between BeginPaint and EndPaint.

            FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));

            EndPaint(hwnd, &ps);
        }*/
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;
    //Step 1: Registering the Window Class;

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION); //comment this out for header and rc
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_INACTIVEBORDER);//(HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION); //comment this out for header and rc
    //wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU); //comment this out for header and rc
    //wc.hIcon  = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    //wc.hIconSm  = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "nameofwin",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 600, 480,
        NULL, NULL, hInstance, NULL);
    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0) //this piece of code is responsible for collecting events (clicks.. etc) and reporting them to the window
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

BOOL authenticate(char uid, string pwd)
{
    //string boobs=uid;
    fstream users;
    users.open("C:\\users\\Eric\\Desktop\\Documentation\\user1.txt");
    users << uid;
    users.close();
    return TRUE;
}

您正在為句柄HWND hwndEDUSER聲明一個靜態變量。 但是在創建窗口時,您沒有使用該變量。 您正在聲明一個新的,它超出范圍並丟失。 稍后,您將使用靜態變量...,其中沒有任何內容。

而不是閱讀“每一個問答”,只需閱讀WM_GETTEXT消息中的文檔即可

wParam中

要復制的最大字符數,包括終止的空字符。

您為wParam傳遞了0 ,因此控件沒有將任何文本復制到緩沖區中,並且留下了原始內容,因為您從未初始化過內存,所以這是垃圾。

只需使用SetWindowTextGetWindowText ,就像這樣:

/* Set the window text */
SetWindowText(hwndEDUSER, _T("username"));

/* Get the window text */
TCHAR *buf;
int len;

if((buf=malloc(len=((GetWindowTextLength(hwndEdit)+1)*sizeof (TCHAR)))!=NULL)
    GetWindowText(hwndEdit, buf, len);
/* Use buf */
free(buf);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM