1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
#include <windows.h>
#define IDC_TEXT 0x7000
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL LoadTextFileToEdit (HWND hwnd, LPCTSTR FileName);
BOOL SaveTextFileFromEdit (HWND hwnd, LPCTSTR FileName);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WindowClass;
WindowClass.style = 0;
WindowClass.cbSize = sizeof (WNDCLASSEX);
WindowClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
WindowClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
WindowClass.hCursor = LoadCursor (NULL, IDC_ARROW);
WindowClass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
WindowClass.hInstance = hInstance;
WindowClass.lpszClassName = "1";
WindowClass.lpszMenuName = NULL;
WindowClass.lpfnWndProc = WndProc;
WindowClass.cbClsExtra = 0;
WindowClass.cbWndExtra = 0;
if (!RegisterClassEx (&WindowClass))
{
MessageBox (NULL,
"Window class registration failed",
NULL,
MB_OK | MB_ICONWARNING);
return 0;
}
HWND hwnd = CreateWindowEx (WS_EX_CLIENTEDGE,
"1",
"Win32",
WS_OVERLAPPEDWINDOW,
315, 115,
640, 480,
NULL,
NULL,
hInstance,
NULL);
if (hwnd == NULL)
{
MessageBox (NULL,
"Window creation failed",
NULL,
MB_OK | MB_ICONWARNING);
return 0;
}
ShowWindow (hwnd, SW_SHOWNORMAL);
MSG msg;
while (GetMessage (&msg, NULL, 0, 0) > 0)
{
TranslateMessage (&msg);
DispatchMessage (&msg);
if (WM_KEYDOWN == msg.message)
{
SendMessage (hwnd, WM_KEYDOWN, msg.wParam, msg.lParam);
}
}
return 0;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
DestroyWindow (hwnd);
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
DestroyWindow (hwnd);
break;
}
break;
case WM_CREATE:
{
HFONT hfDefault;
HWND hEdit;
hEdit = CreateWindowEx (WS_EX_CLIENTEDGE,
"edit",
"",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE,
0, 0,
100, 100,
hwnd,
(HMENU) IDC_TEXT,
GetModuleHandle (NULL),
NULL);
if (hEdit == NULL)
{
MessageBox (hwnd,
"Could not create edit box",
NULL,
MB_OK | MB_ICONWARNING);
DestroyWindow (hwnd);
}
hfDefault = (HFONT) GetStockObject (DEFAULT_GUI_FONT);
SendMessage (hEdit,
WM_SETFONT,
(WPARAM) hfDefault,
MAKELPARAM (FALSE, 0));
}
break;
case WM_SIZE:
{
HWND hEdit;
RECT rcClient;
GetClientRect (hwnd, &rcClient);
hEdit = GetDlgItem (hwnd, IDC_TEXT);
SetWindowPos (hEdit,
NULL,
0,
0,
rcClient.right,
rcClient.bottom,
SWP_NOZORDER);
}
break;
}
return DefWindowProc (hwnd, msg, wParam, lParam);
}
BOOL LoadTextFileToEdit (HWND hwnd, LPCTSTR FileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile (FileName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize;
dwFileSize = GetFileSize (hFile, NULL);
if (dwFileSize != 0xFFFFFFFF)
{
LPSTR FileText;
FileText = GlobalAlloc (GPTR, dwFileSize + 1);
if (FileText != NULL)
{
DWORD dwRead;
if (ReadFile (hFile, FileText, dwFileSize, &dwRead, NULL))
{
FileText [dwFileSize] = 0;
if (SetWindowText (hwnd, FileText))
bSuccess = TRUE;
}
GlobalFree (FileText);
}
}
CloseHandle (hFile);
}
return bSuccess;
}
BOOL SaveTextFileFromEdit (HWND hwnd, LPCTSTR FileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile (FileName,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
DWORD TextLength;
TextLength = GetWindowTextLength (hwnd);
if (TextLength > 0)
{
LPSTR Text;
DWORD dwBufferSize = TextLength + 1;
Text = GlobalAlloc (GPTR, dwBufferSize);
if (Text != NULL)
{
if (GetWindowText (hwnd, Text, dwBufferSize))
{
DWORD Written;
if (WriteFile (hwnd, Text, TextLength, &Written, NULL))
bSuccess = TRUE;
}
GlobalFree (Text);
}
}
CloseHandle (hFile);
}
return bSuccess;
}
|