#Include "windows.bi" #Include "win\commdlg.bi" #Include "resource.bi" Const As String g_szClassName = "myWindowClass" Dim Shared As HMODULE hInstance Dim Shared As ZString Ptr lpCmdLine #define IDC_MAIN_EDIT 101 Function LoadTextFileToEdit(hEdit As HWND, pszFileName As LPCTSTR) As BOOL Dim As HANDLE hFile Dim As BOOL bSuccess = FALSE hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, _ OPEN_EXISTING, 0, NULL) If hFile <> INVALID_HANDLE_VALUE Then Dim As Long dwFileSize dwFileSize = GetFileSize(hFile, NULL) If dwFileSize <> &hFFFFFFFF Then Dim As LPSTR pszFileText pszFileText = Cast(LPSTR, GlobalAlloc(GPTR_, dwFileSize + 1)) If pszFileText <> NULL Then Dim As Long dwRead If ReadFile(hFile, pszFileText, dwFileSize, @dwRead, NULL) Then pszFileText[dwFileSize] = 0 ' Add null terminator If SetWindowText(hEdit, pszFileText) Then bSuccess = TRUE ' It worked! EndIf EndIf GlobalFree(pszFileText) EndIf EndIf CloseHandle(hFile) EndIf Return bSuccess End Function Function SaveTextFileFromEdit(hEdit As HWND, pszFileName As LPCTSTR) As BOOL Dim As HANDLE hFile Dim As BOOL bSuccess = FALSE hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL, _ CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) If hFile <> INVALID_HANDLE_VALUE Then Dim As Long dwTextLength dwTextLength = GetWindowTextLength(hEdit) ' No need to bother if there's no text. If dwTextLength > 0 Then Dim As LPSTR pszText Dim As Long dwBufferSize = dwTextLength + 1 pszText = Cast(LPSTR, GlobalAlloc(GPTR_, dwBufferSize)) If pszText <> NULL Then If GetWindowText(hEdit, pszText, dwBufferSize) Then Dim As Long dwWritten If WriteFile(hFile, pszText, dwTextLength, @dwWritten, NULL) Then bSuccess = TRUE EndIf EndIf GlobalFree(pszText) EndIf EndIf CloseHandle(hFile) EndIf Return bSuccess End Function Sub DoFileOpen(hwnd As HWND) Dim As OPENFILENAME ofn Dim As ZString*MAX_PATH szFileName = "" ZeroMemory(@ofn, SizeOf(ofn)) ofn.lStructSize = SizeOf(OPENFILENAME) ofn.hwndOwner = hwnd ofn.lpstrFilter = @"Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0" ofn.lpstrFile = @szFileName ofn.nMaxFile = MAX_PATH ofn.Flags = OFN_EXPLORER Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY ofn.lpstrDefExt = @"txt" If GetOpenFileName(@ofn) Then Dim As HWND hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT) LoadTextFileToEdit(hEdit, szFileName) EndIf End Sub Sub DoFileSave(hwnd As HWND) Dim As OPENFILENAME ofn Dim As ZString*MAX_PATH szFileName = "" ZeroMemory(@ofn, SizeOf(ofn)) ofn.lStructSize = SizeOf(OPENFILENAME) ofn.hwndOwner = hwnd ofn.lpstrFilter = @"Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0" ofn.lpstrFile = @szFileName ofn.nMaxFile = MAX_PATH ofn.lpstrDefExt = @"txt" ofn.Flags = OFN_EXPLORER Or OFN_PATHMUSTEXIST Or OFN_HIDEREADONLY Or OFN_OVERWRITEPROMPT If GetSaveFileName(@ofn) Then Dim As HWND hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT) SaveTextFileFromEdit(hEdit, szFileName) EndIf End Sub Function WndProc(hwnd As HWND, msg As UINT, wParam As WPARAM, lParam As LPARAM) As LRESULT Select Case msg Case WM_CREATE Dim As HFONT hfDefault Dim As HWND hEdit hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", _ WS_CHILD Or WS_VISIBLE Or WS_VSCROLL Or WS_HSCROLL Or _ ES_MULTILINE Or ES_AUTOVSCROLL Or ES_AUTOHSCROLL, _ 0, 0, 100, 100, hwnd, Cast(HMENU ,IDC_MAIN_EDIT), _ GetModuleHandle(NULL), NULL) If hEdit = NULL Then MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK Or MB_ICONERROR) EndIf hfDefault = Cast(HFONT, GetStockObject(DEFAULT_GUI_FONT)) SendMessage(hEdit, WM_SETFONT, Cast(WPARAM, hfDefault), MAKELPARAM(FALSE, 0)) Case WM_SIZE Dim As HWND hEdit Dim As RECT rcClient GetClientRect(hwnd, @rcClient) hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT) SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER) Case WM_CLOSE DestroyWindow(hwnd) Case WM_DESTROY PostQuitMessage(0) Case WM_COMMAND Select Case LoWord(wParam) Case ID_FILE_EXIT PostMessage(hwnd, WM_CLOSE, 0, 0) Case ID_FILE_NEW SetDlgItemText(hwnd, IDC_MAIN_EDIT, "") Case ID_FILE_OPEN DoFileOpen(hwnd) Case ID_FILE_SAVEAS DoFileSave(hwnd) End Select Case Else Return DefWindowProc(hwnd, msg, wParam, lParam) End Select Return 0 End Function Function WinMain(hInstance As HINSTANCE, hPrevInstance As HINSTANCE, lpCmdLine As LPSTR, nCmdShow As Long) As Long Dim As WNDCLASSEX wc Dim As HWND hwnd Dim As MSG Msg wc.cbSize = SizeOf(WNDCLASSEX) wc.style = 0 wc.lpfnWndProc = @WndProc wc.cbClsExtra = 0 wc.cbWndExtra = 0 wc.hInstance = hInstance wc.hIcon = LoadIcon(NULL, IDI_APPLICATION) wc.hCursor = LoadCursor(NULL, IDC_ARROW) wc.hbrBackground = Cast(HBRUSH, COLOR_WINDOW + 1) wc.lpszMenuName = MAKEINTRESOURCE(IDR_MAINMENU) wc.lpszClassName = @g_szClassName wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION) If RegisterClassEx(@wc) = 0 Then MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION Or MB_OK) Return 0 EndIf hwnd = CreateWindowEx(0, _ g_szClassName, _ "theForger's Tutorial Application", _ WS_OVERLAPPEDWINDOW, _ CW_USEDEFAULT, CW_USEDEFAULT, 480, 320, _ NULL, NULL, hInstance, NULL) If hwnd = NULL Then MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION Or MB_OK) Return 0 EndIf ShowWindow(hwnd, nCmdShow) UpdateWindow(hwnd) Do While GetMessage(@msg,NULL,0,0) TranslateMessage(@msg) DispatchMessage(@msg) Loop Return msg.wParam End Function ' Program start hInstance = GetModuleHandle(NULL) lpCmdLine = GetCommandLine WinMain(hInstance, NULL, lpCmdLine, SW_SHOWDEFAULT) ExitProcess(0)