⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 openfile.cpp

📁 使用TEA加密方法的简单加密解密程序. 包含 COMMAND版本 和 WIN32 两个版本.可供初学者学习. 超级雷电
💻 CPP
字号:
// OpenFile.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include <commdlg.h>
#include "resource.h"
#include "myGlobal.h"
#include "func.h"
#include <Shlobj.h>
#include <cstring>
#include <process.h>


// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
int SelFolder(HWND hParent, char* strFolder);
int SelFile(HWND hWin, char* szFileNameIn, char* szFileTitleIn);
void InitNoticeStruct(struct _notice* pn, HWND hWin, char* szFileNameIn, char* szFileTitleNameIn,
							char* szFloderNameOut, char* code, int iflag, bool sta);

VOID Thread (PVOID pvoid)
{
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_OPENFILE, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_OPENFILE);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= DLGWINDOWEXTRA;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_OPENFILE);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_BTNFACE+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_OPENFILE;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable

   //hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
   //   CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
   hWnd=CreateDialog(hInstance,(LPCTSTR)IDD_DIALOG1,NULL,NULL) ;

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

	switch (message) 
	{
		case WM_CREATE:
				CheckRadioButton(hWnd, IDC_RADIO1, IDC_RADIO1, IDC_RADIO1);
				LoadString(hInst, IDS_STRING_IN, szFileNameIn, MAX_PATH);
				LoadString(hInst, IDS_STRING_OUT, szFolderNameOut, MAX_PATH);


			break;


		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;
				case IDC_IN:
					if (SelFile(hWnd, szFileNameIn, szFileTitleNameIn)) bIn = true;
					SetWindowText(GetDlgItem(hWnd, IDC_EDIT1), szFileNameIn);
					break;
				case IDC_OUT:
					if (SelFolder(hWnd, szFolderNameOut)) bOut = true;
					else {
						LoadString(hInst, IDS_STRING_OUT, szFolderNameOut, MAX_PATH);
					}

					SetWindowText(GetDlgItem(hWnd, IDC_EDIT2), szFolderNameOut);
					break;

				case IDC_ENCODE:
				case IDC_DECODE:
					if (!bIn) {
						MessageBox(hWnd, "请选择输入文件!", "说明", MB_OK);
						return false;
					}
					if (!GetWindowText(GetDlgItem(hWnd, IDC_EDIT3), szCode, 9))
						szCode[0] = '\0';
					sta = true;
					
					InitNoticeStruct(&notice, hWnd, szFileNameIn, szFileTitleNameIn,
										szFolderNameOut, szCode, bIn && bOut, sta);

					if (wmId == IDC_ENCODE) {
						_beginthread(EnCode, 0, &notice) ;

					}
					else if (wmId == IDC_DECODE) {
						_beginthread(DeCode, 0, &notice) ;

					}
					
					break;

				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			if (!bIn && !bOut) {
				LoadString(hInst, IDS_STRING_IN, szFileNameIn, MAX_PATH);
				LoadString(hInst, IDS_STRING_OUT, szFolderNameOut, MAX_PATH);
			}
			SetWindowText(GetDlgItem(hWnd, IDC_EDIT1), szFileNameIn);
			SetWindowText(GetDlgItem(hWnd, IDC_EDIT2), szFolderNameOut);
			// TODO: Add any drawing code here...
			//RECT rt;
			//GetClientRect(hWnd, &rt);
			//DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}

/*==========================================================
				初始化OPENFILENAME结构
==========================================================*/

int SelFile(HWND hWin, char* szFileNameIn, char* szFileTitleIn)
{
	OPENFILENAME ofn;

	// initialize OPENFILENAME struct
	ofn.lStructSize       = sizeof (OPENFILENAME) ;
	ofn.hwndOwner         = hWin ;
	ofn.hInstance         = NULL ;
	ofn.lpstrFilter       = szFilter;
	ofn.lpstrCustomFilter = NULL ;
	ofn.nMaxCustFilter    = 0 ;
	ofn.nFilterIndex      = 0 ;
	ofn.lpstrFile         = szFileNameIn ;
	ofn.nMaxFile          = MAX_PATH ;
	ofn.lpstrFileTitle    = szFileTitleIn;
	ofn.nMaxFileTitle     = MAX_PATH ;
	ofn.lpstrInitialDir   = NULL ;
	ofn.lpstrTitle        = NULL ;
	ofn.Flags             = OFN_FILEMUSTEXIST ;
	ofn.nFileOffset       = 0 ;
	ofn.nFileExtension    = 0 ;
	ofn.lpstrDefExt       = NULL;
	ofn.lCustData         = 0 ;
	ofn.lpfnHook          = NULL ;
	ofn.lpTemplateName    = NULL ;

	if (!GetOpenFileName(&ofn)) return 0;

	return 1;
}

int SelFolder(HWND hParent, char* strFolder)
{
    strFolder[0] = '\0';
 
    LPMALLOC lpMalloc;
 
    if (::SHGetMalloc(&lpMalloc) != NOERROR) return 0;
 
    char szDisplayName[_MAX_PATH];
    char szBuffer[MAX_PATH];
    BROWSEINFO browseInfo;
    browseInfo.hwndOwner = hParent;
    browseInfo.pidlRoot = NULL; // set root at Desktop
    browseInfo.pszDisplayName = szDisplayName;
    browseInfo.lpszTitle = "选择文件夹";
    browseInfo.ulFlags = BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS;
    browseInfo.lpfn = NULL;
    browseInfo.lParam = 0;
 
    LPITEMIDLIST lpItemIDList;
    if ((lpItemIDList = ::SHBrowseForFolder(&browseInfo)) != NULL)
    {
        // Get the path of the selected folder from the    item ID list.
        if (::SHGetPathFromIDList(lpItemIDList, szBuffer))
        {
            // At this point, szBuffer contains the path the user chose.
            if (szBuffer[0] == '\0') return 0;
 
            // We have a path in szBuffer! Return it.
            strcpy(strFolder, szBuffer);
            return 1;
        }
        else return 1; // strResult is empty
 
        lpMalloc->Free(lpItemIDList);
        lpMalloc->Release();
    }

}

void InitNoticeStruct(struct _notice* pn, HWND hWin, char* szFileNameIn, char* szFileTitleNameIn,
							char* szFloderNameOut, char* code, int iflag, bool sta)
{
	pn->hWin = hWin;
	pn->iflag = iflag;
	pn->szFileNameIn =  szFileNameIn;
	pn->szFileTitleNameIn = szFileTitleNameIn;
	pn->szFloderNameOut = szFloderNameOut;
	pn->code = code;
	pn->sta = sta;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -