Dispiacere
December 28th, 2009, 07:42 AM
I'm making a simple database program for a friend and am stuck. The program has a small text box, a large multiline text box, and a button. When the user types a word or series of words(in this case, a dog breed) into the small text box and presses the button, the program takes the text from the small text box, puts it in a string variable fn, and calls a function that takes the file "data/" + fn + ".txt", gets the text from the file one line at a time, and puts the text into the large multiline text box.
It's going well so far. I made a simple console version that worked great and the only thing I'm having trouble with is trying to find WinAPI help that has nothing to do with Visual Studios. Specifically, what I'm having trouble with is:
1) How do I make a pointer to the contents of a text box and/or how do I put the contents of a text box into a string variable?
2) How do I clear, write, and append to a multiline text box?
Here's the code. I'm pretty new to WinAPI programming, so if you see any errors or bad practices don't hesitate to set me straight.
#include <windows.h>
#include <fstream.h>
#include <string.h>
#define ID_EDIT 1
#define ID_BUTTON 2
#define ID_MAINBOX 3
void Datafile_Input(string fn);
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
TCHAR szAppName[] = TEXT("Simple Database Program");
int WINAPI WinMain
(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow
)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("Ths program require Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow
(
szAppName,
szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg),
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc
(
HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam
)
{
static HWND hwndEdit;
static HWND hwndButton;
static HWND hwndMainBox;
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static TCHAR szText[512] = "";
switch(msg)
{
case WM_CREATE:
hwndEdit = CreateWindow
(
"edit",
NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER,
10, 10, 500, 20, hwnd, (HMENU) ID_EDIT,
((LPCREATESTRUCT) lParam)->hInstance, NULL
);
hwndButton = CreateWindow
(
"Button","Find Data",
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,
530,10,80,20,hwnd,(HMENU)ID_BUTTON,
((LPCREATESTRUCT) lParam)->hInstance,0
);
hwndMainBox = CreateWindow
(
"edit",
NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE | WS_VSCROLL,
10, 40, 610, 400, hwnd, (HMENU) ID_MAINBOX,
((LPCREATESTRUCT) lParam)->hInstance, NULL
);
return 0;
break;
case WM_SETFOCUS:
SetFocus(hwndEdit);
return 0;
break;
case WM_COMMAND:
switch(wParam)
{
case ID_BUTTON:
{
SetFocus(hwndMainBox);
//Datafile_Input("data/breeds/" + fn + ".txt");
break;
}
}
return 0;
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return 0;
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
void Datafile_Input(string fn)
{
ifstream inf;
inf.open(fn.c_str());
if( !inf )
{
//
}
string s;
getline(inf,s);
while( s != "999" )
{
// cout << s << endl;
getline(inf,s);
}
inf.close();
}
It's going well so far. I made a simple console version that worked great and the only thing I'm having trouble with is trying to find WinAPI help that has nothing to do with Visual Studios. Specifically, what I'm having trouble with is:
1) How do I make a pointer to the contents of a text box and/or how do I put the contents of a text box into a string variable?
2) How do I clear, write, and append to a multiline text box?
Here's the code. I'm pretty new to WinAPI programming, so if you see any errors or bad practices don't hesitate to set me straight.
#include <windows.h>
#include <fstream.h>
#include <string.h>
#define ID_EDIT 1
#define ID_BUTTON 2
#define ID_MAINBOX 3
void Datafile_Input(string fn);
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
TCHAR szAppName[] = TEXT("Simple Database Program");
int WINAPI WinMain
(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow
)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("Ths program require Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow
(
szAppName,
szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg),
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc
(
HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam
)
{
static HWND hwndEdit;
static HWND hwndButton;
static HWND hwndMainBox;
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static TCHAR szText[512] = "";
switch(msg)
{
case WM_CREATE:
hwndEdit = CreateWindow
(
"edit",
NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER,
10, 10, 500, 20, hwnd, (HMENU) ID_EDIT,
((LPCREATESTRUCT) lParam)->hInstance, NULL
);
hwndButton = CreateWindow
(
"Button","Find Data",
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,
530,10,80,20,hwnd,(HMENU)ID_BUTTON,
((LPCREATESTRUCT) lParam)->hInstance,0
);
hwndMainBox = CreateWindow
(
"edit",
NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE | WS_VSCROLL,
10, 40, 610, 400, hwnd, (HMENU) ID_MAINBOX,
((LPCREATESTRUCT) lParam)->hInstance, NULL
);
return 0;
break;
case WM_SETFOCUS:
SetFocus(hwndEdit);
return 0;
break;
case WM_COMMAND:
switch(wParam)
{
case ID_BUTTON:
{
SetFocus(hwndMainBox);
//Datafile_Input("data/breeds/" + fn + ".txt");
break;
}
}
return 0;
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return 0;
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
void Datafile_Input(string fn)
{
ifstream inf;
inf.open(fn.c_str());
if( !inf )
{
//
}
string s;
getline(inf,s);
while( s != "999" )
{
// cout << s << endl;
getline(inf,s);
}
inf.close();
}