#include <windows.h>
#include <mil.h>
#define MIL_APPLICATION_NAME MIL_TEXT("MIL Application")
#define MIL_APPLICATION_NAME_SIZE 128
#define DEFAULT_IMAGE_SIZE_X 640
#define DEFAULT_IMAGE_SIZE_Y 480
#define DEFAULT_IMAGE_SIZE_BAND 1
#define BACKCOLORRED 160
#define BACKCOLORGREEN 160
#define BACKCOLORBLUE 164
#define IDM_START_MAIN 100
void MilApplication(HWND UserWindowHandle);
void MilApplicationPaint(HWND UserWindowHandle);
HWND hwndChild;
HWND hwndMain;
void MilApplication(HWND UserWindowHandle)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay,
MilDigitizer,
MilImage;
MIL_INT BufSizeX;
MIL_INT BufSizeY;
MIL_INT BufSizeBand;
MappAlloc(M_NULL, M_DEFAULT, &MilApplication);
MsysAlloc(M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, M_DEFAULT, &MilSystem);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_WINDOWED, &MilDisplay);
if (MsysInquire(MilSystem, M_DIGITIZER_NUM, M_NULL) > 0)
{
MIL_INT MaxSizeX = 0, MaxSizeY = 0;
UINT uFlags = SWP_NOZORDER|SWP_NOACTIVATE;
MdigAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, &MilDigitizer);
MdigInquire(MilDigitizer, M_SIZE_X, &BufSizeX);
MdigInquire(MilDigitizer, M_SIZE_Y, &BufSizeY);
MdigInquire(MilDigitizer, M_SIZE_BAND, &BufSizeBand);
if((BufSizeX > DEFAULT_IMAGE_SIZE_X) || (BufSizeY > DEFAULT_IMAGE_SIZE_Y))
{
SetWindowPos(hwndMain, 0, 0, 0, (int)BufSizeX, (int)BufSizeY,
SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE);
SetWindowPos(UserWindowHandle, 0, 0, 0, (int)BufSizeX, (int)BufSizeY,
SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE);
}
}
else
{
MilDigitizer = M_NULL;
BufSizeX = DEFAULT_IMAGE_SIZE_X;
BufSizeY = DEFAULT_IMAGE_SIZE_Y;
BufSizeBand = DEFAULT_IMAGE_SIZE_BAND;
}
MbufAllocColor(MilSystem, BufSizeBand, BufSizeX, BufSizeY, 8+M_UNSIGNED,
(MilDigitizer ? M_IMAGE+M_DISP+M_GRAB : M_IMAGE+M_DISP), &MilImage);
MbufClear(MilImage,0);
MdispSelectWindow(MilDisplay, MilImage, UserWindowHandle);
MgraFont(M_DEFAULT, M_FONT_DEFAULT_LARGE);
MgraText(M_DEFAULT, MilImage, (BufSizeX/8)*2, BufSizeY/2,
MIL_TEXT(" Welcome to MIL !!! "));
MgraRect(M_DEFAULT, MilImage, ((BufSizeX/8)*2)-60, (BufSizeY/2)-80,
((BufSizeX/8)*2)+370, (BufSizeY/2)+100);
MgraRect(M_DEFAULT, MilImage, ((BufSizeX/8)*2)-40, (BufSizeY/2)-60,
((BufSizeX/8)*2)+350, (BufSizeY/2)+80);
MgraRect(M_DEFAULT, MilImage, ((BufSizeX/8)*2)-20, (BufSizeY/2)-40,
((BufSizeX/8)*2)+330, (BufSizeY/2)+60);
MessageBox(0,MIL_TEXT("\"Welcome to MIL !!!\" was printed"),
MIL_TEXT("MIL application example"),
MB_APPLMODAL | MB_ICONEXCLAMATION);
if (MilDigitizer)
{
MdigGrabContinuous(MilDigitizer, MilImage);
MessageBox(0,MIL_TEXT("Continuous grab in progress"),
MIL_TEXT("MIL application example"),
MB_APPLMODAL | MB_ICONEXCLAMATION );
MdigHalt(MilDigitizer);
}
MdispSelect(MilDisplay, M_NULL);
MbufFree(MilImage);
if (MilDigitizer)
MdigFree(MilDigitizer);
MdispFree(MilDisplay);
MsysFree(MilSystem);
MappFree(MilApplication);
}
void MilApplicationPaint(HWND UserWindowHandle)
{
UserWindowHandle = UserWindowHandle;
}
int WINAPI MosWinMain(HINSTANCE, HINSTANCE, LPTSTR, int);
long WINAPI MainWndProc(HWND, UINT, WPARAM, LPARAM);
BOOL InitApplication(HINSTANCE, int, HBRUSH);
int WINAPI MosWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HBRUSH brush = CreateSolidBrush(RGB(BACKCOLORRED,
BACKCOLORGREEN,
BACKCOLORBLUE));
lpCmdLine = lpCmdLine;
if (hPrevInstance)
{
MessageBox(0, MIL_TEXT("Sample MIL Host Windows Application already active.\n")
MIL_TEXT("This version sample application does not allow ")
MIL_TEXT("multiple instances."),
MIL_TEXT("Sample MIL Host Application - Error"),
MB_OK | MB_ICONSTOP);
return (FALSE);
}
if (!InitApplication(hInstance,nCmdShow,brush))
return (FALSE);
while (GetMessage(&msg,(HWND)NULL,(UINT)NULL,(UINT)NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DeleteObject (brush);
return ((MIL_INT32)msg.wParam);
}
long WINAPI MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HMENU hAppMenu;
switch (message)
{
case WM_COMMAND:
switch(wParam)
{
case IDM_START_MAIN:
hAppMenu = GetMenu(hWnd);
EnableMenuItem(hAppMenu,MF_BYCOMMAND | IDM_START_MAIN,MF_GRAYED);
DrawMenuBar(hWnd);
MilApplication(hwndChild);
if (!GetWindowLong(hWnd,sizeof(LPVOID)))
{
PostQuitMessage(0);
}
else
{
EnableMenuItem(hAppMenu,MF_BYCOMMAND | IDM_START_MAIN,MF_ENABLED);
DrawMenuBar(hWnd);
}
break;
default:
return ((MIL_INT32)DefWindowProc(hWnd, message, wParam, lParam));
}
break;
case WM_PAINT:
MilApplicationPaint(hWnd);
return ((MIL_INT32)DefWindowProc(hWnd, message, wParam, lParam));
case WM_ENDSESSION:
case WM_CLOSE:
PostMessage(hWnd, WM_DESTROY, (WORD)0, (LONG)0);
break;
case WM_DESTROY:
if (GetWindowLongPtr(hWnd,sizeof(LPVOID)))
{
free((char *)GetWindowLongPtr(hWnd,sizeof(LPVOID)));
SetWindowLongPtr(hWnd,sizeof(LPVOID),0L);
}
PostQuitMessage(0);
DestroyWindow(hwndChild);
DestroyWindow(hWnd);
default:
return ((MIL_INT32)DefWindowProc(hWnd, message, wParam, lParam));
}
return(0L);
}
BOOL InitApplication(HINSTANCE hInstance, int nCmdShow, HBRUSH brush)
{
WNDCLASS wc;
HWND hwnd;
MIL_TEXT_CHAR TmpName[MIL_APPLICATION_NAME_SIZE+1];
LPTSTR szAppName;
szAppName = (LPTSTR)malloc((MIL_APPLICATION_NAME_SIZE+40) * sizeof(MIL_TEXT_CHAR));
MosStrcpy((MIL_TEXT_PTR)szAppName, MIL_APPLICATION_NAME_SIZE+40, MIL_APPLICATION_NAME);
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.lpfnWndProc = (WNDPROC)MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = sizeof(LPVOID)+sizeof(LPTSTR);
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(0,IDI_APPLICATION);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hbrBackground = brush;
wc.lpszMenuName = MIL_TEXT("MILAPPLMENU");
wc.lpszClassName = szAppName;
if (!RegisterClass(&wc))
return (FALSE);
hwnd = CreateWindow (szAppName,
szAppName,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
0, 0, hInstance, 0) ;
if (!hwnd)
return (FALSE);
hwndMain = hwnd;
hwndChild = CreateWindow (szAppName,
szAppName,
WS_CHILDWINDOW | WS_VISIBLE,
0, 0,
DEFAULT_IMAGE_SIZE_X, DEFAULT_IMAGE_SIZE_Y,
hwnd, 0, hInstance, 0) ;
if (!hwndChild)
return (FALSE);
lstrcat(szAppName,MIL_TEXT(" - "));
GetModuleFileName(hInstance,TmpName,MIL_APPLICATION_NAME_SIZE);
TmpName[MIL_APPLICATION_NAME_SIZE] = MIL_TEXT('\0');
lstrcat(szAppName,TmpName);
SetWindowText(hwnd,szAppName);
SetWindowLongPtr(hwnd,0,0L);
SetWindowLongPtr(hwnd,sizeof(LPVOID),(LONG_PTR)szAppName);
ShowWindow (hwnd, nCmdShow) ;
UpdateWindow (hwnd) ;
return (TRUE);
}