1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
// Main.cpp
// cl Main.cpp /O1 /Os /GS- /FeOpenFile.exe TCLib.lib Kernel32.lib user32.lib ole32.lib uuid.lib
// cl Main.cpp /O1 /Os /MT /FeOpenFile.exe Kernel32.lib User32.lib ole32.lib uuid.lib
// 40,448 Bytes VC15 (VStudio 2008); x64; UNICODE; C Runtime Linkage
// 5,120 Bytes VC15 (VStudio 2008); x64; UNICODE; TCLib Linkage
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#include <windows.h>
#include <Shobjidl.h>
#include "Main.h"
LRESULT __stdcall fnWndProc_OnCreate(WndEventArgs& Wea)
{
HWND hCtrl = NULL;
HRESULT hr;
hr=CoInitialize(NULL);
if(SUCCEEDED(hr))
{
Wea.hIns=((LPCREATESTRUCT)Wea.lParam)->hInstance;
hCtrl=CreateWindowEx(0,L"button",L"Open File...",WS_CHILD|WS_VISIBLE,185,30,125,30,Wea.hWnd,(HMENU)IDC_OPEN,Wea.hIns,0);
hCtrl=CreateWindowEx(WS_EX_CLIENTEDGE,L"edit",L"",WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,10,100,460,25,Wea.hWnd,(HMENU)IDC_FILE,Wea.hIns,0);
}
else
return -1;
return 0;
}
void btnOpen_Click(WndEventArgs& Wea)
{
COMDLG_FILTERSPEC ComDlgFS[3] = {{L"C++ code files", L"*.cpp;*.h;*.rc"},{L"Executable Files", L"*.exe;*.dll"}, {L"All Files",L"*.*"}};
IFileOpenDialog* pFileOpen = NULL;
IShellItem* pShellItem = NULL;
wchar_t* ppszName = NULL;
HWND hCtrl = NULL;
CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_IFileOpenDialog, (void**)(&pFileOpen));
pFileOpen->SetFileTypes(3,ComDlgFS);
pFileOpen->SetTitle(L"A Single Selection Dialog");
pFileOpen->Show(0);
pFileOpen->GetResult(&pShellItem);
pShellItem->GetDisplayName(SIGDN_FILESYSPATH,&ppszName);
hCtrl=GetDlgItem(Wea.hWnd,IDC_FILE);
SetWindowText(hCtrl,ppszName);
CoTaskMemFree(ppszName);
pShellItem->Release();
pFileOpen->Release();
}
LRESULT __stdcall fnWndProc_OnCommand(WndEventArgs& Wea)
{
switch(LOWORD(Wea.wParam))
{
case IDC_OPEN:
{
btnOpen_Click(Wea);
break;
}
}
return 0;
}
LRESULT __stdcall fnWndProc_OnDestroy(WndEventArgs& Wea)
{
CoUninitialize();
PostQuitMessage(0);
return 0;
}
LRESULT __stdcall fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
WndEventArgs Wea;
for(size_t i=0; i<dim(EventHandler); i++)
{
if(EventHandler[i].iMsg==msg)
{
Wea.hWnd=hwnd, Wea.lParam=lParam, Wea.wParam=wParam;
return (*EventHandler[i].fnPtr)(Wea);
}
}
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
wchar_t szClassName[]=L"OpenFile";
HWND hWnd=NULL;
MSG messages;
WNDCLASS wc;
memset(&wc,0,sizeof(WNDCLASS));
wc.lpszClassName = szClassName, wc.lpfnWndProc = fnWndProc;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION), wc.hInstance = hIns;
wc.hCursor = LoadCursor(NULL,IDC_ARROW), wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
RegisterClass(&wc);
hWnd=CreateWindow(szClassName,szClassName,WS_OVERLAPPEDWINDOW,300,400,500,225,HWND_DESKTOP,0,hIns,0);
if(hWnd)
{
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
}
return (int)messages.wParam;
}
|