打开文件并写入文本框c ++

时间:2015-01-26 04:28:00

标签: c++ winapi

好的,所以我需要这个按钮,当点击它时,会打开一个用户指定的文件。打开文件后,需要将其读取/复制到该按钮下方的文本框中。最终其他事情需要发生,但我这样做有困难。以下是我到目前为止创建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;
}

1 个答案:

答案 0 :(得分: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);

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