2 Commits 57507b8b2b ... 75f380067e

Author SHA1 Message Date
  nsp 75f380067e stimpy: add method to read text file 1 year ago
  nsp 7586ce247b stimpy: add menu accelerators 1 year ago

+ 44 - 0
stimpy/stimpy/fileio.cpp

@@ -0,0 +1,44 @@
+#include <windows.h>
+#include "fileio.h"
+
+BOOL LoadTextFileToEdit(HWND hEdit, LPWSTR pwszFileName)
+{
+	HANDLE hFile;
+	BOOL bSuccess = FALSE;
+
+	hFile = CreateFile(
+		pwszFileName, 
+		GENERIC_READ,
+		FILE_SHARE_READ,
+		NULL,
+		OPEN_EXISTING,
+		0,
+		NULL);
+
+	if(hFile == INVALID_HANDLE_VALUE)
+		return false;
+
+	DWORD dwFileSize;
+
+	dwFileSize = GetFileSize(hFile, NULL);
+	if(dwFileSize == 0xFFFFFFFF)
+		return false;
+
+	PSTR pszFileText;
+	pszFileText = (PSTR)GlobalAlloc(GPTR, dwFileSize + 1);
+	if(pszFileText == NULL)
+		return false;
+
+	DWORD dwRead;
+
+	if(!ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
+		return false;
+
+	pszFileText[dwFileSize] = 0;
+
+	SetWindowTextA(hEdit, pszFileText);
+
+	GlobalFree(pszFileText);
+
+	return true;
+}

+ 2 - 0
stimpy/stimpy/fileio.h

@@ -0,0 +1,2 @@
+
+BOOL LoadTextFileToEdit(HWND hEdit, LPWSTR pwszFileName);

+ 1 - 0
stimpy/stimpy/resource.h

@@ -5,6 +5,7 @@
 #define IDR_APP_MENU                            102
 #define IDI_APP_ICON                            103
 #define IDD_APP_SETTINGS                        108
+#define IDR_ACCELERATORS                        110
 #define IDC_TIMERLEN                            40000
 #define IDR_SETTINGS                            40000
 #define IDC_TIMERLBL                            40001

+ 10 - 6
stimpy/stimpy/stimpy.cpp

@@ -1,5 +1,6 @@
 #include <windows.h>
 #include "resource.h"
+#include "fileio.h"
 
 // Necessary to get the styles
 
@@ -44,9 +45,7 @@ void OpenFile(HWND hWnd)
 	ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
 	ofn.lpstrDefExt = L"txt";
 	if(GetOpenFileName(&ofn))
-	{
-		// Do something usefull with the filename stored in szFileName
-	}
+		LoadTextFileToEdit(GetDlgItem(hWnd, IDC_MAIN_EDIT), ofn.lpstrFile);
 }
 
 BOOL CALLBACK StringListDlgProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
@@ -229,11 +228,16 @@ int WINAPI WinMain(
 	ShowWindow(hWnd, nCmdShow);
 	UpdateWindow(hWnd);
 
+	HACCEL hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCELERATORS));
+
 	while(GetMessage(&msg, NULL, 0, 0) > 0)
 	{
-		// Without TranslateMessage the keyboard accelerator keys won't work
-		TranslateMessage(&msg); 
-		DispatchMessage(&msg);
+		if (!TranslateAccelerator(hWnd, hAccel, &msg))
+        {
+			// Without TranslateMessage the Alt keys to access menus won't work
+            TranslateMessage(&msg);
+            DispatchMessage(&msg);
+        }
 	}
 
 	return msg.wParam;

+ 12 - 1
stimpy/stimpy/stimpy.rc

@@ -18,7 +18,7 @@ IDR_APP_MENU MENU
 {
     POPUP "&File"
     {
-        MENUITEM "&Open", IDR_OPEN
+        MENUITEM "&Open\tCtrl-O", IDR_OPEN
         MENUITEM "E&xit", IDR_EXIT
     }
     POPUP "&Settings"
@@ -53,6 +53,17 @@ FONT 8, "Ms Shell Dlg"
 
 
 //
+// Accelerator resources
+//
+LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
+IDR_ACCELERATORS ACCELERATORS
+{
+    "O",            IDR_OPEN, VIRTKEY, CONTROL
+}
+
+
+
+//
 // Icon resources
 //
 LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL

+ 2 - 0
stimpy/stimpy/stimpy.vcxproj

@@ -76,12 +76,14 @@
     <None Include="README.md" />
   </ItemGroup>
   <ItemGroup>
+    <ClCompile Include="fileio.cpp" />
     <ClCompile Include="stimpy.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="stimpy.rc" />
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="fileio.h" />
     <ClInclude Include="resource.h" />
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

+ 6 - 0
stimpy/stimpy/stimpy.vcxproj.filters

@@ -18,6 +18,9 @@
     <ClInclude Include="resource.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="fileio.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="stimpy.rc">
@@ -28,5 +31,8 @@
     <ClCompile Include="stimpy.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="fileio.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>