ファイル名:edit.dlg

editdlg DIALOG 6, 18, 200, 120
STYLE WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
CAPTION "簡易テキストエディタ"
FONT 12, "System"
MENU editmenu
BEGIN
	EDITTEXT  IDEDIT, 4, 15, 150, 79, ES_MULTILINE | WS_VSCROLL | ES_WANTRETURN | ES_NOHIDESEL
END

ファイル名:edit.rc

#include <windows.h>
#include "edit.h"
editmenu MENU
BEGIN
	POPUP        "ファイル(\036F\037フ)"
	BEGIN
		MENUITEM "新規作成(\036N\037サ)", IDM_NEW
		MENUITEM "開く(\036O\037ヒ)...", IDM_OPEN
		MENUITEM "上書き保存(\036S\037セ)", IDM_SAVE
		MENUITEM "名前をつけて保存(\036A\037ナ)...", IDM_NAME
		MENUITEM SEPARATOR
		MENUITEM "終了(\036X\037シ)",    IDM_EXIT
	END
	POPUP        "ヘルプ(\036H\037ヘ)"
	BEGIN
		MENUITEM "ヘルプの表示(\036H\037ヘ)...", IDM_HELP
		MENUITEM "バージョン情報(\036A\037シ)...", IDM_ABOUT
	END
END
#include "edit.dlg"

ファイル名:edit.h

#define IDEDIT 100
#define IDM_NEW 101
#define IDM_OPEN 102
#define IDM_SAVE 103
#define IDM_NAME 104
#define IDM_EXIT 105
#define IDM_HELP 106
#define IDM_ABOUT 107

ファイル名:edit.c

#define STRICT
#include <windows.h>
#include "edit.h"
#include <commdlg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
	#define fmalloc(size) malloc(size)
	#define ffree(ptr)    free(ptr)
	#define MAXPATH 260
	#define MAXNAME 256
#else
	#define fmalloc(size) MAKELONG(0,GlobalAlloc(GMEM_FIXED,size))
	#define ffree(ptr)    GlobalFree((HGLOBAL)HIWORD(ptr))
	#define MAXPATH 80
	#define MAXNAME 13
#endif

#define MAXTEXTSIZE 30000

void SetTitleBar(HWND hwnd, char *name){
	char buf[14+MAXNAME];
	if (name==NULL) sprintf(buf, "簡易テキストエディタ:(無題)");
	else            sprintf(buf, "簡易テキストエディタ:%s", name);
	SetWindowText(hwnd, buf);
}

void readtext(HWND hwnd, char *path, char *name){
	LPSTR p; UINT readsize;
	HFILE hfile=_lopen(path, OF_READ | OF_SHARE_DENY_WRITE);
	if (hfile==HFILE_ERROR) {
		MessageBox(hwnd, "オープンできません", "簡易テキストエディタ", MB_OK);
		return;
	}
	p=(LPSTR)fmalloc(MAXTEXTSIZE+1);
	if (p==NULL) {
		MessageBox(hwnd, "メモリ不足", "簡易テキストエディタ", MB_OK);
		return;
	}
	readsize=_lread(hfile, p, MAXTEXTSIZE);
	if (readsize>MAXTEXTSIZE) p[0]=0; /* HFILE_ERROR */
	else                      p[readsize]=0;
	SetDlgItemText(hwnd, IDEDIT, p);
	ffree(p);
	_lclose(hfile);
	SetTitleBar(hwnd, name);
}
void writetext(HWND hwnd, char *path, char *name){
	LPSTR p; UINT writesize;
	HFILE hfile=_lcreat(path, 0);
	if (hfile==HFILE_ERROR) {
		MessageBox(hwnd, "オープンできません", "簡易テキストエディタ", MB_OK);
		return;
	}
	p=(LPSTR)fmalloc(MAXTEXTSIZE+1); /*これはまずい。EDITの現在のサイズを知れないか? */
	if (p==NULL) {
		MessageBox(hwnd, "メモリ不足", "簡易テキストエディタ", MB_OK);
		return;
	}
	writesize=GetDlgItemText(hwnd, IDEDIT, p, MAXTEXTSIZE+1);
	_lwrite(hfile, p, writesize);
	_lclose(hfile);
	ffree(p);
	SetTitleBar(hwnd, name);
}
/*ダイアログ関数(Windowsからコールバックされる)*/
#ifdef __cplusplus
extern "C"
#endif
BOOL FAR PASCAL dlgproc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam){
	RECT rc;
	static OPENFILENAME ofn;
	static char achFilePath[MAXPATH], achFileTitle[MAXNAME];

	switch(msg){
	case WM_COMMAND:
		switch((WORD)wParam) {/*コントロールID*/
		case IDM_NEW:
			SetDlgItemText(hDlg, IDEDIT, "");
			achFilePath[0]=achFileTitle[0]=0;
			SetTitleBar(hDlg, NULL);
			break;
		case IDM_SAVE:
			if (achFilePath[0]) {writetext(hDlg, achFilePath, achFileTitle); break;}
			/*NO BREAK*/
		case IDM_OPEN:
		case IDM_NAME:
			memset(&ofn, 0, sizeof(OPENFILENAME));
			ofn.lStructSize = sizeof(OPENFILENAME);
			ofn.hwndOwner = hDlg;
			ofn.lpstrFilter = "テキスト(*.TXT)\0*.TXT\0全ファイル(*.*)\0*.*\0";
			ofn.lpstrCustomFilter = NULL;
			ofn.nFilterIndex = 1;
			if ((WORD)wParam==IDM_OPEN)	achFilePath[0] = 0;
			else strcpy(achFilePath,achFileTitle);
			ofn.lpstrFile = (LPSTR)achFilePath;
			ofn.nMaxFile = sizeof(achFilePath);
			ofn.lpstrFileTitle = (LPSTR)achFileTitle;
			ofn.nMaxFileTitle = sizeof(achFileTitle);
			ofn.lpstrInitialDir = NULL;
			ofn.lpstrTitle = NULL;	//ofn.lpstrTitle = "ファイルを開く";
			ofn.lpstrDefExt = NULL;
			if ((WORD)wParam==IDM_OPEN)	ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
			else                        ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
			if ((WORD)wParam==IDM_OPEN) {
				if(GetOpenFileName((LPOPENFILENAME)&ofn))
					readtext(hDlg, achFilePath, achFileTitle);
			}else{
				if (GetSaveFileName((LPOPENFILENAME)&ofn))
					writetext(hDlg, achFilePath, achFileTitle);
			}
			break;
		case IDCANCEL: case IDM_EXIT:
			DestroyWindow(hDlg);
			break;
		case IDM_ABOUT:
			MessageBox(hDlg, "簡易エディタ\nバージョン 0.01\nCopyright (c)のぐー",
			"バージョン情報", MB_OK);
			break;
		case IDM_HELP:
			MessageBox(hDlg, "未サポート","簡易テキストエディタ", MB_OK);
			break;
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	case WM_SIZE:
		GetClientRect (hDlg, &rc);
		MoveWindow(GetDlgItem(hDlg, IDEDIT) , 0, 0, rc.right, rc.bottom, TRUE);
		break;
	case WM_INITDIALOG:
		SetTitleBar(hDlg, NULL);
		GetClientRect (hDlg, &rc);
		MoveWindow(GetDlgItem(hDlg, IDEDIT) , 0, 0, rc.right, rc.bottom, TRUE);
		return TRUE;/* SetFocusしたらFALSE */
	default:
		return FALSE; /*メッセージを処理しなかった*/
	}
	return TRUE; /*メッセージを処理した*/
}
/*メイン関数*/
int PASCAL WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int cmdshow){
	DLGPROC lpProc = (DLGPROC)MakeProcInstance((FARPROC)dlgproc, hinst);
	MSG msg;
	CreateDialog(hinst, "editdlg", NULL, lpProc);
	while (GetMessage(&msg, NULL, 0, 0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	FreeProcInstance((FARPROC)lpProc);
	return 0;
}

ファイル名:edit.def

NAME	EDIT
DESCRIPTION  'EDIT: EASY TEXT EDITOR'
EXETYPE WINDOWS
STUB	    'WINSTUB.EXE'
CODE    PRELOAD MOVEABLE
DATA    MOVEABLE PRELOAD MULTIPLE
HEAPSIZE  4096
STACKSIZE 8192
EXPORTS
	DLGPROC  @1
戻る