【问题标题】:Opening a file and writing to Text box c++打开文件并写入文本框 C++
【发布时间】:2015-01-26 04:27:59
【问题描述】:

好的,所以我需要这个按钮,当点击它时,它会打开一个用户指定的文件。打开文件后,需要将其读取/复制到该按钮下方的文本框中。最终需要发生其他事情,但我在这样做时遇到了麻烦。以下是我迄今为止创建 GUI 的代码,然后我完全不知道如何继续。

#include <iostream>
#include <fstream>
#include <windows.h>
/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    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 color as the background of the window */
    wincl.hbrBackground = GetSysColorBrush(COLOR_3DFACE);

    /* 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 possibilites for variation */
           szClassName,         /* Classname */
           "Software Engineering II",       /* Title Text */
           WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           900,                 /* The programs width */
           500,                 /* 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, nFunsterStil);

    /* 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;
}


#define ID_LABEL 
static HWND hwndLbl1;
static HWND hwndLbl2;
static HWND hwndLbl3;
static HWND hwndLbl4;
bool buttonPressed = false;

/*  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:
             hwndLbl1 = CreateWindow(TEXT("STATIC"),TEXT("Building File Selection"),
             WS_VISIBLE | WS_CHILD,
             10,10,150,25,
             hwnd, (HMENU) 0, NULL, NULL
             );
             hwndLbl2 = CreateWindow(TEXT("STATIC"),TEXT("Class File Selection"),
             WS_VISIBLE | WS_CHILD,
             500,10,150,25,
             hwnd, (HMENU) 0, NULL, NULL
             );
             hwndLbl3 = CreateWindow(TEXT("STATIC"),TEXT("Building No."),
             WS_VISIBLE | WS_CHILD,
             10, 75, 150, 25,
             hwnd, (HMENU) 0, NULL, NULL
             );
             hwndLbl4 = CreateWindow(TEXT("STATIC"),TEXT("Room No."),
             WS_VISIBLE | WS_CHILD,
             500,75,150,25,
             hwnd, (HMENU) 0, NULL, NULL
             );
             // Building Room open
             CreateWindow(TEXT("button"),TEXT("Open"),
             WS_VISIBLE | WS_CHILD, 
             10,40,80,25,
             hwnd, (HMENU) 1, NULL, NULL
             );
             // Classroom open
             CreateWindow(TEXT("button"),TEXT("Open"),
             WS_VISIBLE | WS_CHILD,
             500, 40, 80, 25,
             hwnd, (HMENU) 2, NULL, NULL
             ); 
             CreateWindow(TEXT("button"),TEXT("Search"),
             WS_VISIBLE | WS_CHILD,
             800,100,80,25,
             hwnd, (HMENU) 3, NULL, NULL
             );
             // The information should be read from here.
             CreateWindow(TEXT("edit"), TEXT("Text goes here"),
             WS_VISIBLE | WS_CHILD | WS_BORDER,
             10, 100, 300, 20, 
             hwnd, (HMENU) 5, NULL, NULL
             );
             CreateWindow(TEXT("edit"),TEXT("Text goes here"),
             WS_VISIBLE | WS_CHILD | WS_BORDER,
             500,100,300,20,
             hwnd, (HMENU) 5, NULL, NULL
             );
             CreateWindow(TEXT("edit"),TEXT("Output from Building"),
             WS_CHILD | WS_VISIBLE | ES_READONLY | WS_BORDER,
             10, 150, 400, 250,
             hwnd, (HMENU) 6, NULL, NULL
             );
             CreateWindow(TEXT("edit"),TEXT("Output from Room file"),
             WS_CHILD | WS_VISIBLE | ES_READONLY | WS_BORDER,
             500, 150, 380, 250,
             hwnd, (HMENU) 7, NULL, NULL
             );
             CreateWindow(TEXT("button"),TEXT("Continue"),
             WS_VISIBLE | WS_CHILD, 
             500,410,80,25,
             hwnd, (HMENU) 8, NULL, NULL
             );
             CreateWindow(TEXT("button"),TEXT("Show .PRN"),
             WS_VISIBLE | WS_CHILD, 
             600,410,80,25,
             hwnd, (HMENU) 9, NULL, NULL
             );
             CreateWindow(TEXT("button"),TEXT("Show .LOG"),
             WS_VISIBLE | WS_CHILD, 
             700,410,80,25,
             hwnd, (HMENU) 10, NULL, NULL
             ); 
             CreateWindow(TEXT("button"),TEXT("Show .CSV"),
             WS_VISIBLE | WS_CHILD, 
             800,410,80,25,
             hwnd, (HMENU) 11, NULL, NULL
             );
             break;
             // This is where you issue commands for buttons
             // To read from files and output them into those 2 text boxes that are ID'd (HMENU) 6 and (HMENU 7.
        case WM_COMMAND:
             if(LOWORD(wParam) == 1){
                  FILE * BuildingFile;
                  buttonPressed = true;
                /*  ifstream infile;
                  infile.open ("Bldgs-Rms-Txt-File-2012-08082012.txt");
                  if(infile.is_open()){
                                       while(infile.good())
                                                           print to buildingtextbox (char) infile.get(); 
                  //BuildingFile = fopen("Bldgs-Rms-Txt-File-2012-08082012.txt","r");     // In the argument of MessageBox, this is the PARENT WINDOW, which should be the same for all
                  */
                  }
             if (LOWORD (wParam) == 2){
                   FILE * RoomFile;
                   RoomFile = fopen("cbm_005_003639_201310_08082012.DAT","r");
                  }
             if (LOWORD (wParam) == 6){
                       std::cout << "chuck";
                        }               
             break;        
        case WM_DESTROY:
            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;
}

【问题讨论】:

  • 也许将代码从 200 多行减少到对这个论坛来说合理的东西可能会给你一个答案。太长了。
  • 我想到了这一点,但我不确定你是否能够理解问题中引用的整个 GUI
  • 使用调试器缩小问题范围。任何人都需要这样做才能回答这个问题
  • 在这篇文章中甚至没有任何类似的问题。
  • @RELENTLESS00G 没错。如果您记得提出问题,您将获得更好的结果。我希望这些建议对您有所帮助。

标签: c++ winapi


【解决方案1】:

1) 您需要从 CreateWindow 调用中保留窗口句柄 (hWnd)。

// Up top
#include <string>

// Class member:
HWND building1text;

// Saving the hWnd on the CreateWindow call
building1text = CreateWindow(TEXT("edit"),TEXT("Output from Building"),
             WS_CHILD | WS_VISIBLE | ES_READONLY | WS_BORDER,
             10, 150, 400, 250,
             hwnd, (HMENU) 6, NULL, NULL
             );

2) 然后您可以使用 SetWindowText(hWnd, LPCTSTR) 将文本放入文本框中。

string lineString, totalString;
// open file here
while(infile.good()) {
    // concatenate next line from file (works if file contains 
    // null-terminated lines of stuff
    getline(infile, lineString, '\n');
    totalString += lineString;
}
SetWindowText(building1text, totalString);

我没有设置编译器,但希望它对你有用。当然,我也不知道你的数据文件是如何格式化的。

【讨论】:

  • 好的,非常感谢,我看到了 setedittext 并想不通,我猜 SetWindowText 更容易。另外,您从哪里获得 building1text 作为 hwnd?我假设你只是为那个特定的窗口弥补了它。请问是这个问题很难找到还是其他人只是白痴?
  • building1text 在顶部的示例代码中声明。您的程序不会从任何 CreateWindow 调用中保留 HWND,但您将需要它们来访问这些控件。回答您的第二个问题...我认为您将需要学习如何使用字符串并将文件读入其中。我希望这足以让您从这里继续这一旅程。
  • 哎呀。没有课。好吧,你可以在文件范围内声明你的 HWND。
  • 您无需记住HWNDs 即可访问控件。您通常会调用GetDlgItem 来按 ID 引用控件。正如您所指出的,SetWindowText 的第二个参数是LPCTSTR 类型,因此您不能使用std::string。使用std::basic_string&lt;TCHAR&gt; 或致电SetWindowTextW 并使用std::wstring
  • 谢谢您,IInspectable。 GetDlgItem 和后续调用不会是使用保留的 HWND 的两倍吗?
猜你喜欢
  • 2012-11-24
  • 1970-01-01
  • 2017-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-09
  • 1970-01-01
相关资源
最近更新 更多