/*--------------------------------------------------------
MDIDEMO.C -- Multiple-Document Interface Demonstration
(c) Charles Petzold, 1998
--------------------------------------------------------*/
#include "StdAfx.h"
#include <stdio.h>
#include <list>
#include <ctime>
#define INIT_MENU_POS 0
#define HELLO_MENU_POS 3
#define ID_EDIT 1
#define WM_CMDLINE (WM_USER+1)
#define UNTITLED TEXT("未命名")
#define IDM_FIRSTCHILD 50000
void delimit(char* str,const char* delimiter,std::list<char*> *result) ;
BOOL CopyTitle(LPCTSTR szFileName, PTSTR szEx) ;
int Box(HWND hwnd,const TCHAR* szFormat,...) ;
int AskAboutSave (HWND hwnd, LPCTSTR szTitleName) ;
void OkMessage (HWND hwnd, LPCTSTR szMessage, LPCTSTR szTitleName) ;
void DoCaption (HWND hwnd, LPCTSTR szTitleName) ;
LRESULT CALLBACK FrameWndProc (HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK CloseEnumProc (HWND, LPARAM) ;
LRESULT CALLBACK HelloWndProc (HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) ;
typedef struct tagHELLODATA
{
int cxClient, cyClient ;
int iOffset ;
int iCode ;//文档编码格式,取值[IDC_RADIO1, IDC_RADIO4]
HWND hwndEdit ;
BOOL bNeedSave ;
TCHAR szFileName[MAX_PATH] ;
TCHAR szTitleName[MAX_PATH] ;
PopFont popfont ;
}
HELLODATA, * PHELLODATA ;
#ifndef UNICODE
TCHAR szWndName[] =TEXT ("多文档记事本(ANSI版)") ;
#else
TCHAR szWndName[] =TEXT ("多文档记事本(UNICODE版)") ;
#endif
TCHAR szAppName[] = TEXT ("MDINotePad") ;
TCHAR szFrameClass[] = TEXT ("MdiNotePadFrame") ;
TCHAR szHelloClass[] = TEXT ("MdiNotePadHelloChild") ;
HINSTANCE hInst ;
HWND hDlgModeless = NULL ;
HMENU hMenuInit, hMenuHello ;
HMENU hMenuInitWindow, hMenuHelloWindow ;
std::list<char*> CmdLine ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HACCEL hAccel ;
HWND hwndFrame, hwndClient ;
MSG msg ;
WNDCLASS wndclass ;
srand(time(0));
delimit(szCmdLine,"\"",&CmdLine);
hInst = hInstance ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = FrameWndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, szAppName) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) (COLOR_APPWORKSPACE + 1) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szFrameClass ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = HelloWndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = sizeof (HANDLE) ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON1)) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szHelloClass ;
RegisterClass (&wndclass) ;
hMenuInit = LoadMenu (hInstance, TEXT ("MdiMenuInit")) ;
hMenuHello = LoadMenu (hInstance, TEXT ("MdiMenuHello")) ;
hMenuInitWindow = GetSubMenu (hMenuInit, INIT_MENU_POS) ;
hMenuHelloWindow = GetSubMenu (hMenuHello, HELLO_MENU_POS) ;
hAccel = LoadAccelerators (hInstance, szAppName) ;
hwndFrame = CreateWindowEx (WS_EX_TOPMOST|WS_EX_ACCEPTFILES, szFrameClass, szWndName,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
NULL, hMenuInit, hInstance, NULL) ;
hwndClient = GetWindow (hwndFrame, GW_CHILD) ;
ShowWindow (hwndFrame, iCmdShow) ;
UpdateWindow (hwndFrame) ;
while (GetMessage (&msg, NULL, 0, 0))
{
if (hDlgModeless == NULL || !IsDialogMessage (hDlgModeless, &msg))
{
if (!TranslateMDISysAccel (hwndClient, &msg) &&
!TranslateAccelerator (hwndFrame, hAccel, &msg))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
}
// DestroyMenu (hMenuInit) ;//因为hMenuInit与框架窗口关联着,所以会自动删除
DestroyMenu (hMenuHello) ;
return msg.wParam ;
}
LRESULT CALLBACK FrameWndProc (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
static HWND hwndClient ;
static HDROP hDrop ;
PHELLODATA pHelloData ;
CLIENTCREATESTRUCT clientcreate ;
static HWND hwndChild ;
MDICREATESTRUCT mdicreate ;
int i, nFiles ;
LPFINDREPLACE pfr ;
static UINT messageFindReplace ;
switch (message)
{
case WM_CREATE: // Create the client window
messageFindReplace = RegisterWindowMessage (FINDMSGSTRING) ;
clientcreate.hWindowMenu = hMenuInitWindow ;
clientcreate.idFirstChild = IDM_FIRSTCHILD ;
hwndClient = CreateWindow (TEXT ("MDICLIENT"), NULL,
WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE | MDIS_ALLCHILDSTYLES ,
0, 0, 0, 0, hwnd, (HMENU) 1, hInst, &clientcreate) ;
if(!CmdLine.empty())
PostMessage(hwnd,WM_CMDLINE,(WPARAM)CmdLine.back(),0);
return 0 ;
case WM_DROPFILES:
hDrop=(HDROP)wParam;
nFiles=DragQueryFile(hDrop,-1,0,0);
for(i=0;i<nFiles;++i)
{
SendMessage(hwnd,WM_COMMAND,IDM_FILE_NEW,0);
if (IsWindow (hwndChild))
{
pHelloData = (PHELLODATA) GetWindowLong (hwndChild, 0) ;
DragQueryFile(hDrop,i,pHelloData->szFileName,MAX_PATH);
CopyTitle(pHelloData->szFileName,pHelloData->szTitleName);
if(!PopFile::PopFileRead(pHelloData->hwndEdit,pHelloData->szFileName,&(pHelloData->iCode)))
{
OkMessage (hwndChild, TEXT ("读取文件失败: %s!"),pHelloData->szTitleName) ;
pHelloData->szFileName [0]=0;
pHelloData->szTitleName[0]=0;
}
DoCaption(hwndChild,pHelloData->szTitleName);
}
}
DragFinish(hDrop);
return 0;
case WM_CMDLINE:
SendMessage (hwnd, WM_COMMAND, IDM_FILE_NEW, 0) ;
pHelloData = (PHELLODATA) GetWindowLong (hwndChild, 0) ;
#ifdef UNICODE
A2U((char*)wParam,pHelloData->szFileName,MAX_PATH);
#else
lstrcpy(pHelloData->szFileName,(char*)wParam);
#endif
CopyTitle(pHelloData->szFileName,pHelloData->szTitleName);
if(!PopFile::PopFileRead(pHelloData->hwndEdit,pHelloData->szFileName,&(pHelloData->iCode)))
{
Box(hwndChild,TEXT("读取失败:%s"),pHelloData->szFileName);//
}
DoCaption(hwndChild,pHelloData->szTitleName);
return 0;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDM_FILE_OPEN:
hwndChild = (HWND) SendMessage (hwndClient,WM_MDIGETACTIVE, 0, 0) ;
if (IsWindow (hwndChild))
{
SendMessage (hwndChild, WM_COMMAND, wParam, lParam) ;
return 0;
}
case IDM_FILE_NEWOPEN:
case IDM_FILE_NEW: // Create a Hello child window
mdicreate.szClass = szHelloClass ;
mdicreate.szTitle = UNTITLED ;
mdicreate.hOwner = hInst ;
mdicreate.x = CW_USEDEFAULT ;
mdicreate.y = CW_USEDEFAULT ;
mdicreate.cx = CW_USEDEFAULT ;
mdicreate.cy = CW_USEDEFAULT ;
mdicreate.style = WS_VISIBLE | WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN ;
mdicreate.lParam = 0 ;
hwndChild = (HWND) SendMessage (hwndClient,
WM_MDICREATE, 0,
(LPARAM) (LPMDICREATESTRUCT) &mdicreate) ;
if(LOWORD (wParam)!=IDM_FILE_NEW)
{
SendMessage (hwndChild, WM_COMMAND, IDM_FILE_OPEN, lParam) ;
}
return 0 ;
case IDM_FILE_CLOSE: // Close the active window
hwndChild = (HWND) SendMessage (hwndClient,
WM_MDIGETACTIVE, 0, 0) ;
if (SendMessage (hwndChild, WM_QUERYENDSESSION, 0, 0))
SendMessage (hwndClient, WM_MDIDESTROY, (WPARAM) hwndChild, 0) ;
return 0 ;
case IDM_APP_EXIT:
SendMessage (hwnd, WM_CLOSE, 0, 0) ;
return 0 ;