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
|
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#include "resource.h"
#define ID_A 1 // Form Button
#define IDC_EDIT 2 // Edit Box
#define ID_B 3 // Checkbox
#define ID_C 4 // Group Box
#define ID_D 5 //newfilename
#define ID_E 1700
LRESULT CALLBACK WindowProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(RGB(235, 246, 252));
wc.lpszClassName = L"WindowClass1";
RegisterClassEx(&wc);
hWnd = CreateWindowEx(NULL,
L"WindowClass1", // name of the window class
L"Mini-Prog", // title of the window
WS_OVERLAPPEDWINDOW, // window style
300, // x-position of the window
30, // y-position of the window
700, // width of the window
700, // height of the window
NULL, // we have no parent window, NULL
NULL, // we aren't using menus, NULL
hInstance, // application handle
NULL); // used with multiple windows, NULL
ShowWindow(hWnd, nCmdShow);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
{
//Get the default font
HGDIOBJ hfDefault=GetStockObject(DEFAULT_GUI_FONT);
HWND Gbox = CreateWindowEx(NULL,TEXT("button"), NULL,
WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
10, 10, 665, 640,
hWnd, (HMENU) ID_B, GetModuleHandle(NULL), NULL);
SendMessage(Gbox,WM_SETFONT, (WPARAM)hfDefault,NULL);
//Button
HWND hWndButton = CreateWindowEx(NULL,TEXT("button"), TEXT("Create New File"),
WS_CHILD | WS_VISIBLE,
50, 60, 100, 20,
hWnd, (HMENU) ID_A, GetModuleHandle(NULL), NULL);
//Button Font
SendMessage(hWndButton,WM_SETFONT, (WPARAM)hfDefault,NULL);
//Button
HWND hWndnom = CreateWindowEx(NULL,TEXT("button"), TEXT("Save File"),
WS_CHILD | WS_VISIBLE,
50, 90, 100, 20,
hWnd, (HMENU) ID_E, GetModuleHandle(NULL), NULL);
//Button Font
SendMessage(hWndnom,WM_SETFONT, (WPARAM)hfDefault,NULL);
// Edit Box
HWND hWndEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
TEXT("EDIT"),
NULL,
WS_VISIBLE | WS_CHILD | WS_VSCROLL | ES_MULTILINE,
200, 30, 460, 600,
hWnd,
(HMENU) IDC_EDIT,
GetModuleHandle(NULL),
NULL);
HWND hWndnm = CreateWindowEx(WS_EX_CLIENTEDGE,
TEXT("EDIT"),
NULL,
WS_VISIBLE | WS_CHILD,
20, 30, 170, 20,
hWnd,
(HMENU) ID_D,
GetModuleHandle(NULL),
NULL);
SendMessage(hWndEdit,WM_SETFONT, (WPARAM)hfDefault,NULL);
SendMessage(hWndEdit,WM_SETTEXT,0,(LPARAM)L"Insert text here...");
} break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case ID_A://button
{
//MessageBox(hWnd, L"You clicked me", L"Success!", MB_ICONINFORMATION);
TCHAR szTemp[128];
GetDlgItemText ( hWnd , ID_D , szTemp , 128 );
//MessageBox(hWnd, wcscat(szTemp,L".txt"), L"Success", NULL);
if(wcslen(szTemp) == 0)
{
MessageBox(hWnd, L"Please enter a file name", L"New File Creation", NULL);
}else if(wcslen(szTemp) > 0)
{
/*MessageBox(hWnd, wcscat(szTemp,L".txt has been created"), L"New File Creation", NULL);
SetWindowTextA( GetDlgItem(hWnd, ID_D ), "" );
int argc;
HANDLE hFile;
char DataBuffer[] = "This is some test data to write to the file.";
DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
DWORD dwBytesWritten = 0;
BOOL bErrorFlag = FALSE;
hFile = CreateFile(wcscat(szTemp,L".txt"), // name of the write
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // default security
CREATE_NEW, // create new file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); */ // no attr. template
// Fetch the name (without extension) from the screen
const DWORD dwNameSize = 128;
TCHAR szName[dwNameSize];
GetDlgItemText(hWnd, IDC_EDIT, szName, dwNameSize);
// Append the .txt extension
_tcsncat(szName, _T(.txt), dwRootNameSize);
// Create the file
HANDLE hFile = CreateFile(
szName, // filename
GENERIC_READ,// Open for reading
0, // Do not share
NULL, // No security
OPEN_EXISTING, // Existing file only
FILE_ATTRIBUTE_NORMAL, // Normal file
NULL);
if (hFile == INVALID_HANDLE_VALUE)
return GetLastError();
const DWORD dwBufferSize = 64;
char szBuffer[dwBufferSize];
DWORD dwBytesRead;
ReadFile(hFile, szBuffer, dwBufferSize, dwBytesRead, NULL);
}
}
case ID_E:
{
}
}
} break;
case WM_CLOSE:
{
if (MessageBox(hWnd, L"Are you sure you want to quit?", L"My application", MB_OKCANCEL) == IDOK)
{
DestroyWindow(hWnd);
}
// Else: User canceled. Do nothing.
return 0;
} break;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}
|