00001
00002
00003
00004
00005
00006
00007
00008 #include <stdlib.h>
00009 #include <stdio.h>
00010 #include <windows.h>
00011
00012 #ifdef __X__NT__
00013 #define X__Malloc(s) LocalAlloc(LMEM_FIXED,s)
00014 #define X__Free(p) LocalFree((HLOCAL)p)
00015 #define X__Realloc(p,s) LocalReAlloc((HLOCAL)p,s,LMEM_MOVEABLE)
00016 #else
00017 #define X__Malloc(s) GlobalAlloc(GMEM_FIXED,s)
00018 #define X__Free(p) GlobalFree((HGLOBAL)p)
00019 #define X__Realloc(p,s) GlobalReAlloc((HGLOBAL)p,s,GMEM_MOVEABLE)
00020 #endif
00021
00022 #define FI_DIB 0
00023 #define PALVERSION 0x300
00024 #define MAXPALETTE 256
00025 #define MAXTEXT 256
00026
00027 #if __X__MIPS__
00028 BOOL WINAPI _CRT_INIT(HINSTANCE ,DWORD , LPVOID );
00029 #endif
00030 static void FreeDib(void);
00031 static LRESULT CALLBACK MainWndProc(HWND hWnd,UINT iMessage,WPARAM wParam,
00032 LPARAM lParam);
00033 static void AppPaint(HWND hWnd,HDC hDC,int x,int y);
00034 static void LoadSelectionBitmap(char *, HWND);
00035
00036 static int Selection = -1;
00037 static HWND hParent;
00038 static HINSTANCE hThisInstance;
00039 static HPALETTE hpalCurrent = NULL;
00040 static HBITMAP hbmCurrent = NULL;
00041 static LPSTR lpBitmapHeader = NULL;
00042 static LPSTR lpBitmapBits = NULL;
00043 static char *szViewClass="OFX:PremapClass";
00044 static DWORD fdwStyle = WS_POPUP | WS_CAPTION | WS_SYSMENU |
00045 !WS_MAXIMIZEBOX | !WS_MINIMIZEBOX | WS_THICKFRAME;
00046
00047 static int Viewer(char *mapfile){
00048 MSG msg;
00049 WNDCLASS wndclass;
00050 HWND hWnd;
00051 int xScreen,yScreen;
00052 wndclass.style = 0;
00053 wndclass.lpfnWndProc = (WNDPROC)MainWndProc;
00054 wndclass.cbClsExtra = 0 ;
00055 wndclass.cbWndExtra = 0 ;
00056 wndclass.hInstance = hThisInstance ;
00057 wndclass.hIcon = NULL;
00058 wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
00059 wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
00060 wndclass.lpszMenuName = NULL;
00061 wndclass.lpszClassName = szViewClass;
00062 if (!RegisterClass (&wndclass)) return -1;
00063 xScreen = GetSystemMetrics (SM_CXSCREEN) ;
00064 yScreen = GetSystemMetrics (SM_CYSCREEN) ;
00065 hWnd = CreateWindow(
00066 szViewClass,
00067 "Click on Desired Postage Stamp Image",
00068 fdwStyle,
00069 25,
00070 25,
00071 xScreen-50,
00072 yScreen-50,
00073 hParent,
00074 NULL,
00075 hThisInstance,
00076 NULL) ;
00077 if(hWnd == NULL)return -1;
00078 EnableWindow(hParent,FALSE);
00079 LoadSelectionBitmap(mapfile,hWnd);
00080 ShowWindow (hWnd,SW_SHOWNA);
00081 while(GetMessage (&msg,NULL,0,0)){
00082 TranslateMessage (&msg);
00083 DispatchMessage (&msg);
00084 }
00085 if(IsWindow(hWnd))DestroyWindow(hWnd);
00086 UnregisterClass(wndclass.lpszClassName,wndclass.hInstance);
00087 EnableWindow(hParent,TRUE);
00088 return -1;
00089 }
00090
00091 #define NMATRIX 6
00092 #define NROWS 200
00093 #define HCOLS 320
00094 #define LIMIT 30
00095
00096 static LRESULT CALLBACK MainWndProc(HWND hWnd,UINT iMessage,WPARAM wParam,
00097 LPARAM lParam){
00098 static BOOL bToolCaptured=FALSE;
00099 static HCURSOR hCursorSave;
00100 static POINT LastPt;
00101 PAINTSTRUCT ps;
00102 HDC hDC;
00103 int i;
00104 RECT rc;
00105 POINT pt;
00106 switch (iMessage) {
00107 case WM_DESTROY:
00108 FreeDib();
00109 break;
00110 case WM_RBUTTONUP:
00111 break;
00112 case WM_LBUTTONDOWN:
00113 Selection=1+((HIWORD(lParam)*NMATRIX)/NROWS)*NMATRIX
00114 + (LOWORD(lParam)*NMATRIX)/HCOLS;
00115 if(Selection > LIMIT || Selection < 0){
00116 MessageBeep(MB_OK);
00117 Selection = -1;
00118 }
00119 break;
00120 case WM_LBUTTONUP:
00121
00122
00123
00124
00125 if(Selection > 0 && Selection <= LIMIT)PostQuitMessage(0);
00126 break;
00127 case WM_PAINT:
00128 hDC = BeginPaint(hWnd, &ps);
00129 AppPaint(hWnd,hDC,0,0);
00130 EndPaint(hWnd,&ps);
00131 break ;
00132 case WM_QUERYNEWPALETTE:{
00133 HDC hDC;
00134 HPALETTE hpalT;
00135 if(hpalCurrent == NULL)break;
00136 hDC=GetDC(hWnd);
00137 hpalT = SelectPalette(hDC,hpalCurrent,FALSE);
00138 if(RealizePalette (hDC) > 0)InvalidateRect (hWnd, (LPRECT) (NULL),FALSE);
00139 SelectPalette(hDC,hpalT,FALSE);
00140 ReleaseDC(hWnd,hDC);
00141 return TRUE;
00142 }
00143 case WM_PALETTECHANGED:
00144 if ((HWND)wParam != hWnd ){
00145 HDC hDC;
00146 HPALETTE hpalT;
00147 if(hpalCurrent == NULL)break;
00148 hDC=GetDC(hWnd);
00149 hpalT = SelectPalette(hDC,hpalCurrent,FALSE);
00150 if(RealizePalette (hDC) > 0)InvalidateRect (hWnd, (LPRECT) (NULL),FALSE);
00151 SelectPalette(hDC,hpalT,FALSE);
00152 ReleaseDC(hWnd,hDC);
00153 }
00154 break;
00155 case WM_SYSCOMMAND:
00156 if ((wParam & 0xfff0) == SC_SIZE)break;
00157 if ((wParam & 0xfff0)== SC_MINIMIZE)break;
00158 if ((wParam & 0xfff0)== SC_MAXIMIZE)break;
00159 if ((wParam & 0xfff0)== SC_CLOSE)PostQuitMessage(0);
00160 return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
00161 default:
00162 return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
00163 }
00164 return 0L ;
00165 }
00166
00167 static void FreeDib(void){
00168 if (hpalCurrent) DeleteObject(hpalCurrent);
00169 if (hbmCurrent) DeleteObject(hbmCurrent);
00170 if(lpBitmapBits != NULL)X__Free((char *)lpBitmapBits);
00171 if(lpBitmapHeader != NULL)X__Free((char *)lpBitmapHeader);
00172 lpBitmapHeader = NULL;
00173 lpBitmapBits = NULL;
00174 hpalCurrent = NULL;
00175 hbmCurrent = NULL;
00176 }
00177
00178 static void AppPaint(HWND hWnd,HDC hDC,int x,int y){
00179 RECT rc;
00180 HPALETTE hpalT;
00181 POINT Origin;
00182 HDC hDCbits;
00183 BITMAP bm;
00184 GetClientRect(hWnd,&rc);
00185 if(hpalCurrent != NULL){
00186 hpalT = SelectPalette(hDC,hpalCurrent,FALSE);
00187 RealizePalette(hDC);
00188 if(hbmCurrent != NULL){
00189 hDCbits = CreateCompatibleDC(hDC);
00190 GetObject(hbmCurrent,sizeof(BITMAP),(LPSTR)&bm);
00191 SelectObject(hDCbits,hbmCurrent);
00192 BitBlt(hDC,0,0,bm.bmWidth,bm.bmHeight,hDCbits,0,0,SRCCOPY);
00193 DeleteDC(hDCbits);
00194 }
00195 else{
00196 SetDIBitsToDevice(hDC,0,0,
00197 (WORD)((LPBITMAPINFOHEADER)lpBitmapHeader)->biWidth,
00198 (WORD)((LPBITMAPINFOHEADER)lpBitmapHeader)->biHeight,
00199 0,0,
00200 0,
00201 (WORD)((LPBITMAPINFOHEADER)lpBitmapHeader)->biHeight,
00202 lpBitmapBits,
00203 (LPBITMAPINFO)lpBitmapHeader,
00204 DIB_RGB_COLORS);
00205 }
00206 SelectPalette(hDC,hpalT,FALSE);
00207 }
00208 }
00209
00210 static HBITMAP BitmapFromDib(LPSTR lpHeader,LPSTR lpBits,HPALETTE hpal){
00211 HDC hdc;
00212 HBITMAP hbm;
00213 HPALETTE hpalT;
00214 if (lpHeader == NULL || lpBits == NULL)return NULL;
00215 hdc = GetDC(NULL);
00216 if(hpal != NULL){
00217 hpalT = SelectPalette(hdc,hpal,FALSE);
00218 RealizePalette(hdc);
00219 }
00220 hbm = CreateDIBitmap(hdc,
00221 (LPBITMAPINFOHEADER)lpHeader,
00222 (LONG)CBM_INIT,
00223 lpBits,
00224 (LPBITMAPINFO)lpHeader,
00225 DIB_RGB_COLORS );
00226 if(hpalCurrent)SelectPalette(hdc,hpalT,FALSE);
00227 ReleaseDC(NULL,hdc);
00228 return hbm;
00229 }
00230
00231 static void LoadSelectionBitmap(char *mapfile, HWND hWnd){
00232 LOGPALETTE *pPal;
00233 FILE *dibfile;
00234 LPSTR s;
00235 BITMAPFILEHEADER hdr,*lphdr;
00236 BITMAPINFOHEADER *lpbi;
00237 DWORD ncolours;
00238 RGBQUAD *pRgb;
00239 long i,k,imagesize;
00240 lphdr=&hdr;
00241 if((dibfile=fopen(mapfile,"rb")) == NULL)return;
00242 pPal = (LOGPALETTE*)LocalAlloc(LPTR,sizeof(LOGPALETTE)+256*sizeof(PALETTEENTRY));
00243 if(pPal == NULL)return;
00244 lpBitmapHeader=(LPSTR)X__Malloc((long)sizeof(BITMAPINFOHEADER)+
00245 256L*sizeof(RGBQUAD));
00246 lpbi=(LPBITMAPINFOHEADER)lpBitmapHeader;
00247 if(lpBitmapHeader == NULL){
00248 LocalFree((HANDLE)pPal);
00249 return;
00250 }
00251 pPal->palNumEntries = 256;
00252 pPal->palVersion = PALVERSION;
00253 fread(lphdr,1,sizeof(BITMAPFILEHEADER),dibfile);
00254 fread(lpbi,1,sizeof(BITMAPINFOHEADER),dibfile);
00255 lpbi->biCompression=BI_RGB;
00256 lpbi->biXPelsPerMeter=0;
00257 lpbi->biYPelsPerMeter=0;
00258 lpbi->biClrImportant=256;
00259
00260
00261
00262
00263
00264
00265 pRgb=(RGBQUAD *)((LPSTR)lpbi + lpbi->biSize);
00266 for(i=0;i<256;i++){
00267 fread(&pRgb[i],1,sizeof(RGBQUAD),dibfile);
00268 pPal->palPalEntry[i].peRed = pRgb[i].rgbRed;
00269 pPal->palPalEntry[i].peGreen = pRgb[i].rgbGreen;
00270 pPal->palPalEntry[i].peBlue = pRgb[i].rgbBlue;
00271 pPal->palPalEntry[i].peFlags = (BYTE)0;
00272 }
00273 hpalCurrent = CreatePalette(pPal);
00274 LocalFree((HANDLE)pPal);
00275 imagesize=(long)lpbi->biHeight * (long)lpbi->biWidth;
00276 if((lpBitmapBits=(LPSTR)X__Malloc(imagesize)) == NULL){
00277 FreeDib();
00278 return;
00279 }
00280 fread(lpBitmapBits,1,imagesize,dibfile);
00281 { RECT Rectangle;
00282 Rectangle.left = 0;
00283 Rectangle.top = 0;
00284 Rectangle.right = lpbi->biWidth;
00285 Rectangle.bottom = lpbi->biHeight;
00286 AdjustWindowRect (&Rectangle,fdwStyle,TRUE);
00287 SetWindowPos (hWnd, (HWND)NULL, 0, 0,
00288 Rectangle.right - Rectangle.left,
00289 Rectangle.bottom - Rectangle.top-GetSystemMetrics(SM_CYCAPTION),
00290 SWP_NOMOVE | SWP_NOZORDER);
00291 }
00292
00293 fclose(dibfile);
00294 }
00295
00296 #if __WATCOMC__
00297 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00298 #else
00299 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00300 #endif
00301 switch (dwReason) {
00302 case DLL_PROCESS_ATTACH: {
00303 #if __X__MIPS__
00304 if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00305
00306 #endif
00307 hThisInstance=hDLL;
00308 break;
00309 }
00310 case DLL_PROCESS_DETACH:
00311 #if __X__MIPS__
00312 if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00313
00314 #endif
00315 break;
00316 }
00317 return TRUE;
00318 }
00319
00320 #if __SC__
00321 #pragma startaddress(DllMain)
00322 #endif
00323
00324 BOOL _GetPresetMapImage
00325 (HWND parent, char *mapfile){
00326 hParent=parent;
00327 Selection= -1;
00328 Viewer(mapfile);
00329 return Selection;
00330 }