ファイル名:text.h
#define IDM_OPEN 101
#define IDM_EXIT 102
#define IDM_TAB8 103
#define IDM_TAB4 104
#define IDM_TAB2 105
#define IDM_TAB1 106
#define IDM_LINE 107
#define IDM_ABOUT 108
ファイル名:text.c
#pragma resource "text.res"
#define STRICT
char buf[512];
#include <windows.h>
#include <commdlg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "text.h"
#define APPNAME "TEXTVIEW"
#ifdef WIN32
#define huge
//#define _fnthctype nthctype
#define fmalloc(size) malloc(size)
#define ffree(ptr) free(ptr)
#define MAXPATH 260
#define MAXNAME 256
#else
#include <io.h>/*filelength()*/
#define fmalloc(size) MAKELONG(0,GlobalAlloc(GMEM_FIXED,size))
#define ffree(ptr) GlobalFree((HGLOBAL)HIWORD(ptr))
#define MAXPATH 80
#define MAXNAME 13
#endif
WORD Version; BOOL NT;
static HWND hwndApp;
static HINSTANCE hInstApp;
static char *szAppName="textview";
static HMENU hmenu;
static char huge *textbuf;
#ifdef WIN32
#define MAXLINE 65535
#else
#define MAXLINE 2000
#endif
static char far *linebuf[MAXLINE];
static int linecount;
static int cyClient, cyChar;
static int cxClient, cxChar;
static int pitch;
static int tab=8;
void SetTitleBar(HWND hwnd, char *name){
if (name==NULL) sprintf(buf, APPNAME":(無題)");
else sprintf(buf, APPNAME":%s", name);
SetWindowText(hwnd, buf);
}
void readtext(HWND hwnd, char *path, char *name){
unsigned long textsize, readsize;
HFILE hfile;
hfile=_lopen(path, OF_READ | OF_SHARE_DENY_WRITE);
if (hfile==HFILE_ERROR) {
MessageBox(hwnd, "オープンできません", szAppName, MB_OK);
return;
}
if (textbuf) ffree(textbuf), textbuf=NULL, linebuf[0]=NULL;
#ifdef WIN32
{
WIN32_FIND_DATA ffd; HANDLE h;
h=FindFirstFile(path, &ffd);
if (h==INVALID_HANDLE_VALUE) {
MessageBox(hwnd, "ファイルが見つからない", szAppName, MB_OK);
return;
}
FindClose(h);
textsize=ffd.nFileSizeLow+ffd.nFileSizeHigh*65536;
}
#else
textsize=filelength(hfile);
#endif
textbuf=(LPSTR)fmalloc(textsize+1);
if (textbuf==NULL) {
char buf[100];
linebuf[0]=NULL; linecount=0;
sprintf(buf, "メモリ不足 %d", textsize);
MessageBox(hwnd, buf, szAppName, MB_OK);
return;
}
readsize=_hread(hfile, textbuf, textsize);
#ifndef WIN32
if (readsize>65535LU) readsize=65535LU;
#endif
if (readsize>textsize) textbuf[0]=0, linebuf[0]=NULL; /* HFILE_ERROR */
else textbuf[readsize]=0/*, textsize=readsize*/;
_lclose(hfile);
//MessageBox(NULL, "success", "read", MB_OK);
if (textbuf[0]){
char far *p; int i=0;
linebuf[0]=textbuf;
for(;;){
p=_fstrchr(linebuf[i], '\n');
i++; if (i==MAXLINE) break;
if (p==NULL) {
linebuf[i]=NULL;
break;
}
*p=0;
if (p!=linebuf[i-1])
if (*(p-1)=='\r') *(p-1)=0;
if (*(p+1)==0) {linebuf[i]=NULL; break;}
linebuf[i]=p+1;
}
linecount=i;
}
SetTitleBar(hwnd, name);
InvalidateRect(hwnd, NULL, TRUE);
}
static void tabmenucheck(int i){
if (tab==8) CheckMenuItem(hmenu, IDM_TAB8, i);
if (tab==4) CheckMenuItem(hmenu, IDM_TAB4, i);
if (tab==2) CheckMenuItem(hmenu, IDM_TAB2, i);
if (tab==1) CheckMenuItem(hmenu, IDM_TAB1, i);
}
#ifdef __cplusplus
extern "C"
#endif
long FAR PASCAL MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
static OPENFILENAME ofn;
static char achFilePath[MAXPATH], achFileTitle[MAXNAME]="noname.txt";
int i;
HDC hdc; RECT rc;
PAINTSTRUCT ps;
TEXTMETRIC tm;
HFONT hfont, hfontOld;
int fuFormat;
switch (message) {
case WM_CREATE:
hdc = GetDC (hwnd);
GetTextMetrics (hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
pitch=cyChar*24/20;
ReleaseDC (hwnd, hdc);
return 0;
case WM_SIZE:
cxClient = LOWORD (lParam);
cyClient = HIWORD (lParam);
return 0;
case WM_COMMAND:
switch((WORD)wParam) {/*コントロールID*/
case IDM_OPEN:
memset(&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "テキスト(*.TXT)\0*.TXT\0全ファイル(*.*)\0*.*\0";
ofn.lpstrCustomFilter = NULL;
ofn.nFilterIndex = 1;
achFilePath[0] = 0;
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;
ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
if(GetOpenFileName((LPOPENFILENAME)&ofn))
readtext(hwnd, achFilePath, achFileTitle);
return 0;
case IDM_EXIT:
SendMessage(hwnd, WM_CLOSE, 0, 0L);
return 0;
case IDM_TAB8:
tabmenucheck(MF_UNCHECKED);
tab=8; InvalidateRect(hwnd, NULL, TRUE);
tabmenucheck(MF_CHECKED);
return 0;
case IDM_TAB4:
tabmenucheck(MF_UNCHECKED);
tab=4; InvalidateRect(hwnd, NULL, TRUE);
tabmenucheck(MF_CHECKED);
return 0;
case IDM_TAB2:
tabmenucheck(MF_UNCHECKED);
tab=2; InvalidateRect(hwnd, NULL, TRUE);
tabmenucheck(MF_CHECKED);
return 0;
case IDM_TAB1:
tabmenucheck(MF_UNCHECKED);
tab=1; InvalidateRect(hwnd, NULL, TRUE);
tabmenucheck(MF_CHECKED);
return 0;
case IDM_LINE:
if (cyChar==pitch) pitch=cyChar*24/20, CheckMenuItem(hmenu, IDM_LINE, MF_CHECKED);
else pitch=cyChar, CheckMenuItem(hmenu, IDM_LINE, MF_UNCHECKED);
InvalidateRect(hwnd, NULL, TRUE);
return 0;
case IDM_ABOUT:
#ifdef WIN32
strcpy(buf, "Text Viewer 32bit版\nVersion 0.00\nCopyright (c)のぐー");
#else
strcpy(buf, "Text Viewer 16bit版\nVersion 0.00\nCopyright (c)のぐー");
#endif
MessageBox(hwnd, buf, "About", MB_OK);
return 0;
}
break;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
hfont = (HFONT)GetStockObject(OEM_FIXED_FONT);
hfontOld=(HFONT)SelectObject(hdc,hfont);
SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
//if (tab==8) fuFormat = DT_SINGLELINE|DT_EXPANDTABS|DT_NOPREFIX;
//else fuFormat = DT_SINGLELINE|DT_EXPANDTABS|DT_TABSTOP|(tab<<8);
fuFormat = tab*cxChar;//TabbedTextOut()を使うとき
rc.left = cxChar; rc.right=cxClient;
for (i = 0; i < linecount; i++) {
rc.top = pitch * (0.25 + i);
rc.bottom = rc.top+pitch;
//DrawText(hdc, linebuf[i], _fstrlen(linebuf[i]), &rc, fuFormat);
//TextOut(hdc, rc.left, rc.top, linebuf[i], _fstrlen(linebuf[i]));
TabbedTextOut(hdc, rc.left, rc.top, linebuf[i], _fstrlen(linebuf[i]),
1, &fuFormat, rc.left);
}
SelectObject(hdc, hfontOld);
//sprintf(buf, "%d %d %d", cyChar, cxChar, linecount);
//TextOut (hdc, 0, 0, buf, lstrlen (buf));
EndPaint (hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
BOOL InitApplication(HINSTANCE hInstance){
WNDCLASS wc;
wc.style = 0;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
//wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIcon = LoadIcon(hInstance, "TextIcon");
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
//wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = "TextMenu";
wc.lpszClassName = "TextWClass";
return RegisterClass(&wc);
}
void GetWindowsVersion(void){
// 0x300: Windows 3.00
// 0x30a: Windows 3.10
// 0x30b: Windows 3.11
// 0x35f: Windows 95 (16bit) (3.95)
// 0x400: Windows 95 (32bit) (4.00)
// 0x362: Windows 98 (16bit) (3.98)
// 0x40a: Windows 98 (32bit) (4.10)
WORD DosVersion = HIWORD(GetVersion());
Version = LOWORD(GetVersion());
Version = (WORD)((LOBYTE(Version)<<8) + HIBYTE(Version));
#ifdef WIN32
if (DosVersion & 0x8000) NT=0; else NT=1;
#else
if (Version==0x35f && DosVersion >=0x70a) Version+=3;
#endif
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){
GetWindowsVersion();
hwndApp = CreateWindow(
"TextWClass", APPNAME, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if (!hwndApp) return FALSE;
ShowWindow(hwndApp, nCmdShow);
UpdateWindow(hwndApp);
hmenu=GetMenu(hwndApp);
tabmenucheck(MF_CHECKED);
if (cyChar!=pitch) CheckMenuItem(hmenu, IDM_LINE, MF_CHECKED);
return TRUE;
}
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
MSG msg;
HACCEL hAccel=LoadAccelerators(hInstance, "TextAccel");
hInstApp=hInstance;
if (!hPrevInstance)
if (!InitApplication(hInstance))
return FALSE;
if (!InitInstance(hInstance, nCmdShow))
return FALSE;
while (GetMessage(&msg, NULL, 0, 0)) {
if (!TranslateAccelerator(hwndApp, hAccel, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
ファイル名:text.rc
//#include <windows.h>
#include "text.h"
TextIcon ICON "text.ICO"
TextMenu MENU
BEGIN
POPUP "ファイル(\036F\037フ)"
BEGIN
MENUITEM "開く(\036O\037ヒ)...", IDM_OPEN
MENUITEM SEPARATOR
MENUITEM "終了(\036X\037シ)", IDM_EXIT
END
POPUP "表示(\036V\037ヒ)"
BEGIN
MENUITEM "TAB(&8)", IDM_TAB8
MENUITEM "TAB(&4)", IDM_TAB4
MENUITEM "TAB(&2)", IDM_TAB2
MENUITEM "TAB(&1)", IDM_TAB1
MENUITEM SEPARATOR
MENUITEM "行ピッチを広げる(&L)", IDM_LINE
END
POPUP "ヘルプ(\036H\037ヘ)"
BEGIN
MENUITEM "バージョン情報(\036A\037シ)...", IDM_ABOUT
END
END
TextAccel ACCELERATORS
BEGIN
VK_F1, IDM_ABOUT, VIRTKEY
END
ファイル名:text.def
NAME TextView
DESCRIPTION 'テキストビュアー'
EXETYPE WINDOWS
STUB 'WINSTUB.EXE'
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE MULTIPLE
HEAPSIZE 1024
STACKSIZE 8192
EXPORTS
MainWndProc @1 ; ウィンドウ処理関数の名前
戻る