Windows API Primer   テキストビューワ

ホーム   目次

テキストビューワ

エディットコントロールにファイルを読み込む方法のサンプルとして、テキストビューワを作ってみました。

textviewer.c


#include <windows.h>

#define  FILE 10
#define  OPEN 11
#define  QUIT 12
#define  HELP 20
#define  ABOU 21

HWND     gEdit;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void    OpenDialog      (HWND);
void    LoadFile        (LPSTR);

int     WINAPI   WinMain(HINSTANCE hInstance,
			 HINSTANCE hPrevInstance,
			 LPSTR     lpCmdLine,
			 int       nCmdShow)
{
  WNDCLASS wc;
  HWND     hwnd;
  MSG      msg;

  HMENU   menubar;
  HMENU   menuFile;
  HMENU   menuHelp;

  HACCEL  haccel;
  ACCEL   accel[3];

  menubar  = CreateMenu();
  menuFile = CreateMenu();
  menuHelp = CreateMenu();

  AppendMenu(menuFile, MF_STRING, OPEN, TEXT("Open\tCtrl + O"));
  AppendMenu(menuFile, MF_SEPARATOR, 0, NULL                  );
  AppendMenu(menuFile, MF_STRING, QUIT, TEXT("Quit\tCtrl + Q"));
  AppendMenu(menubar , MF_POPUP , (UINT_PTR)menuFile, TEXT("File"));
  AppendMenu(menuHelp, MF_STRING, ABOU, TEXT("About\tCtrl + A"));
  AppendMenu(menubar , MF_POPUP , (UINT_PTR)menuHelp, TEXT("Help"));

  wc.style         = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc   = WndProc;
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.hInstance     = hInstance;
  wc.hIcon         = NULL;
  wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND + 1;
  wc.lpszMenuName  = NULL;
  wc.lpszClassName = TEXT("TEXTVIEWER");

  RegisterClass (&wc);

  hwnd             = CreateWindow(TEXT("TEXTVIEWER"),
				  TEXT("Windows API Primer"),
				  WS_OVERLAPPEDWINDOW | WS_VISIBLE,
				  CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
				  NULL, menubar, hInstance, NULL);

  accel[0].key   = 0x4F; // O key
  accel[0].cmd   = OPEN;
  accel[0].fVirt = FCONTROL | FVIRTKEY;
  accel[1].key   = 0x51; // Q key
  accel[1].cmd   = QUIT;
  accel[1].fVirt = FCONTROL | FVIRTKEY;
  accel[2].key   = 0x41; // A key
  accel[2].cmd   = ABOU;
  accel[2].fVirt = FCONTROL | FVIRTKEY;
  haccel = CreateAcceleratorTable(accel, 3);

  while (GetMessage(&msg, NULL, 0, 0))
    {
      if (!TranslateAccelerator(hwnd, haccel, &msg))
	{
	  TranslateMessage(&msg);
	  DispatchMessage (&msg);
	}
    }
  DestroyAcceleratorTable(haccel);

  return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  RECT  rect;
  HFONT font;
  
  switch (msg)
  {
  case WM_CREATE:
    GetClientRect(hwnd, &rect);
    font  = CreateFont(14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, FIXED_PITCH, 0);
    gEdit = CreateWindow(TEXT("EDIT"),
		 	 NULL,
			 WS_CHILD | WS_VISIBLE | WS_BORDER |
			 ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
			 0, 0, rect.right, rect.bottom,
			 hwnd, NULL, NULL, NULL);
    SendMessage(gEdit, WM_SETFONT, (WPARAM)font, 0);
    SetFocus   (gEdit);
    break;
  case WM_SIZE:
    GetClientRect(hwnd,  &rect);
    SetWindowPos (gEdit, HWND_TOP, 0, 0, rect.right, rect.bottom, SWP_NOMOVE);
    break;
  case WM_COMMAND:
    switch LOWORD(wParam)
      {
      case OPEN:
	OpenDialog(hwnd);
	break;
      case QUIT:
	SendMessage(hwnd, WM_CLOSE, 0, 0);
	break;
      case ABOU:
	MessageBox(hwnd, "TextViewer Ver. 1.0", "About", MB_OK);
	break;
      }
    break;
  case WM_DESTROY:
    PostQuitMessage(0);
    break;
  }
    
  return DefWindowProc(hwnd, msg, wParam, lParam);
}

void OpenDialog(HWND hwnd)
{
  OPENFILENAME filename;
  TCHAR        filesize[MAX_PATH];

  ZeroMemory(&filename,     sizeof(filename));
  filename.lStructSize     = sizeof(filename);
  filename.lpstrFile       = filesize;
  filename.lpstrFile[0]    = '\0';
  filename.hwndOwner       = hwnd;
  filename.nMaxFile        = sizeof(filename);
  filename.lpstrFilter     = TEXT("C file(*.c)\0*.c\0");
  filename.nFilterIndex    = 1;
  filename.lpstrInitialDir = NULL;
  filename.lpstrFileTitle  = NULL;
  filename.Flags           = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

  if (GetOpenFileName(&filename))
    {
      LoadFile(filename.lpstrFile);
    }
}

void LoadFile(LPSTR file)
{
  HANDLE filehandle;
  DWORD  dwordsize;
  DWORD  dword;
  LPBYTE buffer = NULL;

  filehandle = CreateFile(file, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
  dwordsize  = GetFileSize(filehandle, NULL);
  buffer     = (LPSTR)HeapAlloc(GetProcessHeap(),
				HEAP_GENERATE_EXCEPTIONS, dwordsize + 1);
  ReadFile(filehandle, (LPWSTR)buffer, dwordsize, &dword, NULL);
  CloseHandle(filehandle);
  buffer[dwordsize] = 0;SetWindowText(gEdit, (LPSTR)buffer);
  HeapFree(GetProcessHeap(), 0, buffer);
}
    

実行結果


49439 visits
Posted: Jun. 13, 2020
Update: Jun. 13, 2020

ホーム   目次