# Include <windows. h>
# Include "resource. h"
# Pragma comment (linker, "/subsystem: windows ")
// The Return Value of the process function is LRESULT and the type is CALLBACK.
Lresult callback WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
Int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpszCmdLine, int nCmdShow)
{
HWND hwnd; // The Main Window handle, which is assigned a value in CreateWindow.
MSG msg; // message variable, used in GetMessage
WNDCLASS wndclass; // window class
TCHAR * szAppName = TEXT ("File"); // class name and window name
Wndclass. hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); // window background
Wndclass. hCursor = LoadCursor (NULL, IDC_ARROW); // mouse
Wndclass. hIcon = LoadIcon (NULL, IDI_APPLICATION); // icon
Wndclass. lpszClassName = szAppName; // Class Name
Wndclass. cbclsextra = 0; // additional parameter of the class
Wndclass. cbwndextra = 0; // additional window parameters. Used to differentiate Windows based on the same window class.
// You must specify the dlgw.wextra size when customizing the dialog box class.
Wndclass. lpszmenuname = text ("mainmenu"); // menu name. It can be used as the ID of a subwindow.
Wndclass. Style = cs_hredraw | cs_vredraw; // window style
Wndclass. lpfnwndproc = wndproc; // Window Process
Wndclass. hinstance = hinstance; // instance handle containing the Window Process
If (! Registerclass (& wndclass) // register the window class
Return 0;
Hwnd = createwindow (// create a window
Szappname, // window class name
Szappname, // window title
Ws_overlappedwindow, // window style
Cw_usedefault, // The initial X coordinate
Cw_usedefault, // initial y coordinate
Cw_usedefault, // initial width
Cw_usedefault, // initial height
Null, // parent window
Null, // menu
Hinstance, // instance handle related to the window
Null // additional parameter
);
Showwindow (hwnd, ncmdshow); // display window
Updatewindow (hwnd); // update window
While (getmessage (& MSG, null, 0, 0) // message loop
{
TranslateMessage (& msg); // translate the WM_XXXKEYXXX message into a WM_CHAR message
DispatchMessage (& msg); // process of passing a message to the window
}
Return msg. wParam; // return
}
Int FileDialog (HWND hwndParent, TCHAR * szFile, BOOL bOpen)
{
OPENFILENAME ofn;
ZeroMemory (& ofn, sizeof (ofn ));
Ofn. Flags = OFN_EXPLORER;
Ofn. lStructSize = sizeof (ofn );
Ofn. hwndOwner = hwndParent;
Ofn. lpstrFile = szFile;
Ofn. nMaxFile = MAX_PATH;
Ofn. lpstrFile [0] = '/0 ';
Ofn. lpstrFilter = TEXT ("Text Files (*. txt)/0 *. txt/0 ");
Return
BOpen?
GetOpenFileName (& ofn ):
Getsavefilename (& ofn );
}
Lresult winapi wndproc (hwnd, uint message, wparam, lparam)
{
Switch (Message)
{
Case wm_command:
Switch (loword (wparam ))
{
Case idm_file_new:
{
MessageBox (hwnd, text ("functions to be implemented"), text ("file"), mb_ OK );
Return 0;
}
Case idm_file_open:
{
Tchar szfile [max_path];
If (filedialog (hwnd, szfile, true ))
{
HDC = getdc (hwnd );
RECT rc;
DWORD dwBytesRead = 0;
HANDLE hFile = CreateFile (
SzFile, // file name of the file to be opened
GENERIC_READ, // What do I do when I open it?
File_cmd_write | file_cmd_read, // What other programs can do?
NULL, // SECURITY_ATTRIBUTE, generally NULL
OPEN_EXISTING, // do you want to create a file or open an existing one?
FILE_ATTRIBUTE_NORMAL, // file attribute, hide file? Read-Only files? ...
NULL); // File Attribute template, generally NULL
TCHAR szData [20];
BOOL bResult = ReadFile (
Hfile, // which file to read?
Szdata, // Where can I read it?
20, // how many items are read?
& Dwbytesread, // how many actually reads? If the following is null, this cannot be null.
Null); // generally null
If (bresult & dwbytesread = 0)
{
// You have not learned how to determine the end of a file
// This is incorrect.
MessageBox (hwnd, text ("EOF met! "), Text (" file "), mb_ OK );
// Return 0;
}
Getclientrect (hwnd, & rc );
Drawtext (HDC, szdata, 3, & rc, dt_center); // 3 is the number of characters
Releasedc (hwnd, HDC );
Closehandle (hfile );
}
Return 0;
}
Case idm_file_save:
{
Tchar szfile [max_path];
If (filedialog (hwnd, szfile, false ))
{
Handle hfile = createfile (
Szfile, // file name
Generic_write, // specify the operation permission
File_cmd_write, // specify the operation permissions of other programs on this file.
NULL, // SECURITY_ATTRIBUTE
OPEN_EXISTING ,//
FILE_ATTRIBUTE_NORMAL, // file attributes
NULL); // file property Template
DWORD dwBytesWritten = 0;
SetFilePointer (// file pointer
HFile, // which file pointer is set?
10, // offset
NULL, // is also the offset. Together with the above, the offset can be larger.
FILE_BEGIN); // relative position offset
SetEndOfFile (hFile); // change the file size
WriteFile (
Hfile, // which file to write?
Text ("hello"), // what to write?
5, // how much to write?
& Dwbyteswritten, // What is actually written?
Null); // It is the same as reading a file. It can be null.
If (hfile! = NULL)
{
Closehandle (hfile );
}
}
Return 0;
}
Case idm_file_delete:
{
Tchar szfile [max_path];
If (FileDialog (hwnd, szFile, TRUE ))
{
DeleteFile (szFile); // permanently delete the file. If you want to put it in the recycle bin, use SHFileOperation
}
Return 0;
}
Case IDM_FILE_EXIT:
{
PostQuitMessage (0 );
Return 0;
}
} // WM_COMMAND
Return 0;
Case WM_DESTROY:
PostQuitMessage (0 );
Return 0;
} // Switch (message)
Return DefWindowProc (hwnd, message, wParam, lParam );
}