I am a C programmer trying to learn windows programming. Specifically, I am trying to figure out how to put text to text boxes, and retrieve it to place it into a file. Also, I need to place prompts on the window. What I am looking to do is create a window and populate it, where a user can key in data that can be retrieved and placed into a file. Here is what I have gotten so far:
#include <windows.h>
#include <stdio.h>
LPSTR szClassName = "MyClass";
HINSTANCE hInstance;
LRESULT CALLBACK MyWndProc(HWND, UINT, WPARAM, LPARAM);
HWND hwndText[3];
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
{
WNDCLASS wnd;
MSG msg;
HWND hwnd;
hInstance = hInst;
wnd.style = CS_HREDRAW | CS_VREDRAW; //we will explain this later
wnd.lpfnWndProc = MyWndProc;
wnd.cbClsExtra = 0;
wnd.cbWndExtra = 0;
wnd.hInstance = hInstance;
wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); //default icon
wnd.hCursor = LoadCursor(NULL, IDC_ARROW); //default arrow mouse cursor
wnd.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wnd.lpszMenuName = NULL; //no menu
wnd.lpszClassName = szClassName;
if(!RegisterClass(&wnd)) //register the WNDCLASS
{
MessageBox(NULL, "This Program Requires Windows NT",
"Error", MB_OK);
return 0;
}
hwnd = CreateWindow(szClassName,
"Window Title",
WS_OVERLAPPEDWINDOW, //basic window style
CW_USEDEFAULT,
CW_USEDEFAULT, //set starting point to default value
CW_USEDEFAULT,
CW_USEDEFAULT, //set all the dimensions to default value
NULL, //no parent window
NULL, //no menu
hInstance,
NULL); //no parameters to pass
// create a text box and store the handle
hwndText[0] = CreateWindow(
TEXT("edit"), // The class name required is edit
TEXT("Enter text here"), // Default text.
WS_VISIBLE | WS_CHILD | WS_BORDER, // the styles
100,100, // the left and top co-ordinates
300,30, // width and height
hwnd, // parent window handle
NULL, // the ID of your combobox
hInstance, // the instance of your application
NULL
); // extra bits you dont really need
hwndText[1] = CreateWindow(
TEXT("edit"), // The class name required is edit
TEXT("2nd field"), // Default text.
WS_VISIBLE | WS_CHILD | WS_BORDER , // the styles
100,200, // the left and top co-ordinates
300,30, // width and height
hwnd, // parent window handle
NULL, // the ID of your combobox
hInstance, // the instance of your application
NULL
); // extra bits you dont really need
ShowWindow(hwnd, iCmdShow); //display the window on the screen
UpdateWindow(hwnd); //make sure the window is updated correctly
while(GetMessage(&msg, NULL, 0, 0)) //message loop
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK MyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
FILE *fp;
char data[256];
switch(msg)
{
case WM_DESTROY:
GetDlgItemText(hwndText[0],1, data, 255);
fp=fopen("c:\workoutc\wikiwin.txt","w");
fprintf(fp,"%s\n",data);
fclose(fp);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}