--- mstools/samples/mandel/julia.c 2018/08/09 18:20:41 1.1 +++ mstools/samples/mandel/julia.c 2018/08/09 18:21:39 1.1.1.2 @@ -30,6 +30,7 @@ * none * \**************************************************************************/ +//#define STRICT #include #include #include @@ -40,7 +41,7 @@ //#define CYCLETHRD #define PRTTHRD -#define NEWPRTAPI +//#define NEWPRTAPI //#define DEBUG @@ -55,12 +56,11 @@ // Forward declarations. // BOOL InitializeApp (INT*); -LONG MainWndProc (HWND, UINT, DWORD, LONG); -LONG ChildWndProc (HWND, UINT, DWORD, LONG); -LONG About (HWND, UINT, DWORD, LONG); -LONG TextWndProc (HWND, UINT, DWORD, LONG); -LONG JuliaWndProc (HWND, UINT, DWORD, LONG); -LONG DrawWndProc (HWND, UINT, DWORD, LONG); +LONG APIENTRY MainWndProc (HWND, UINT, DWORD, LONG); +LONG APIENTRY ChildWndProc (HWND, UINT, DWORD, LONG); +LONG APIENTRY About (HWND, UINT, DWORD, LONG); +LONG APIENTRY TextWndProc (HWND, UINT, DWORD, LONG); +LONG APIENTRY JuliaWndProc (HWND, UINT, DWORD, LONG); BOOL APIENTRY SuspendDrawThrd (HWND, LONG); BOOL APIENTRY ResumeDrawThrd (HWND, LONG); BOOL StartDraw (PINFO); @@ -70,6 +70,7 @@ BOOL StartMandelbrot (PINFO); BOOL StartMandelbrotFix (PINFO); HBITMAP SaveBitmap (HWND); void DrawBitmap (HDC, PINFO, int, int, int, int); +BOOL bDrawDIB (HDC, PINFO, int, int, int, int); LONG lMul(LONG, LONG); LONG lDiv(LONG, LONG); PINFO pGetInfoData(HWND); @@ -82,6 +83,7 @@ BOOL bPrintBmp(PPRTDATA); extern BOOL bCycle(HWND); extern BOOL bCleanupPrinter(VOID); extern INT iCreatePenFrPal(HDC, PVOID *); +extern int FAR PASCAL ShellAbout(HWND, LPCSTR, LPCSTR, HICON); /******************************Public*Routine******************************\ * @@ -228,6 +230,7 @@ BOOL InitializeApp(INT *piPen) } if ((iNumClr = iCreatePenFrPal(hDC, NULL)) != 0) { + //DbgPrint("iNumClr = %d\n", iNumClr); sprintf( gtext,"iNumClr = %d\n", iNumClr); OutputDebugString( gtext); @@ -270,7 +273,7 @@ BOOL InitializeApp(INT *piPen) * Wrote it. \**************************************************************************/ -long MainWndProc( +long APIENTRY MainWndProc( HWND hwnd, UINT message, DWORD wParam, @@ -431,8 +434,12 @@ long MainWndProc( } case MM_ABOUT: - if (DialogBox(ghModule, "AboutBox", ghwndMain, (DLGPROC)About) == -1) - MessageBox(ghwndMain, "DEMO: About Dialog Creation Error!", "Error", MB_OK); + + ShellAbout(ghwndMain, "Mandelbrot Dream", "", + LoadIcon(ghModule, MAKEINTRESOURCE(APPICON))); + + // if (DialogBox(ghModule, "AboutBox", ghwndMain, (DLGPROC)About) == -1) + // MessageBox(ghwndMain, "DEMO: About Dialog Creation Error!", "Error", MB_OK); return 0L; // @@ -509,7 +516,7 @@ long MainWndProc( * 09-09-91 Petrus Wong Rewrote. \***************************************************************************/ -long ChildWndProc( +long APIENTRY ChildWndProc( HWND hwnd, UINT message, DWORD wParam, @@ -789,7 +796,7 @@ long ChildWndProc( GetClientRect(pInfo->hwnd, &rc); hDC = GetDC(pInfo->hwnd); if (LoadBitmapFile(hDC, pInfo, szFile)) - DrawBitmap(hDC, pInfo, 0, 0, rc.right, rc.bottom); + bDrawDIB(hDC, pInfo, 0, 0, rc.right, rc.bottom); ReleaseDC(hwnd, hDC); bReleaseInfoData(hwnd); @@ -1286,7 +1293,7 @@ long ChildWndProc( pInfo->hBmpSaved = SaveBitmap(pInfo->hwnd); Escape(hdcPrinter, STARTDOC, 20, "Mandelbrot", NULL); - DrawBitmap(hdcPrinter, pInfo, 0, 0, iWidth, iHeight); + bDrawDIB(hdcPrinter, pInfo, 0, 0, iWidth, iHeight); Escape(hdcPrinter, NEWFRAME, NULL, NULL, NULL); Escape(hdcPrinter, ENDDOC, NULL, NULL, NULL); ReleaseDC(pInfo->hwnd, hDC); @@ -1576,7 +1583,7 @@ long ChildWndProc( * 09-09-91 Petrus Wong Rewrote. \***************************************************************************/ -long About( +long APIENTRY About( HWND hDlg, UINT message, DWORD wParam, @@ -1609,7 +1616,7 @@ long About( * \***************************************************************************/ -LONG TextWndProc (HWND hwnd, UINT message, DWORD wParam, LONG lParam) +LONG APIENTRY TextWndProc (HWND hwnd, UINT message, DWORD wParam, LONG lParam) { static HFONT hFont = (HFONT) NULL; @@ -1732,7 +1739,7 @@ LONG TextWndProc (HWND hwnd, UINT messag * Wrote it. \**************************************************************************/ -LONG JuliaWndProc (HWND hwnd, UINT message, DWORD wParam, LONG lParam) +LONG APIENTRY JuliaWndProc (HWND hwnd, UINT message, DWORD wParam, LONG lParam) { // // These statics are shared by all the Julia windows. But, this is @@ -1787,7 +1794,7 @@ LONG JuliaWndProc (HWND hwnd, UINT messa if (pInfo->hBmpSaved) { hDC = GetDC(hwnd); - DrawBitmap(hDC, pInfo, 0, 0, rc.right, rc.bottom); + bDrawDIB(hDC, pInfo, 0, 0, rc.right, rc.bottom); ReleaseDC(hwnd, hDC); } bReleaseInfoData(hParent); @@ -2092,6 +2099,9 @@ BOOL APIENTRY SuspendDrawThrd (HWND hwnd BOOL bDrawing; char sz[30]; + if (!IsWindow(hwnd)) + return 1L; + GetClassName(hwnd, sz, 15); if (strcmp(sz, "ChildClass") != 0) return 1L; @@ -2141,6 +2151,9 @@ BOOL APIENTRY ResumeDrawThrd (HWND hwnd BOOL bDrawing; char sz[30]; + if (!IsWindow(hwnd)) + return 1L; + GetClassName(hwnd, sz, 15); if (strcmp(sz, "ChildClass") != 0) return 1L; @@ -2511,7 +2524,7 @@ BOOL StartDraw2(PINFO pInfo) } } r = sqrt(w0 * w0 + w1 + w1); - theta = theta/2.0 + ((int) ((2.0*rand()/(RAND_MAX+1.0)))*pi); +// theta = theta/2.0 + ((int) (2.0*rand()/(RAND_MAX+1.0)))*pi; r = sqrt(r); x0 = r*cos(theta); y0 = r*sin(theta); @@ -2900,6 +2913,125 @@ void DrawBitmap(HDC hdc, PINFO pInfo, in UNREFERENCED_PARAMETER(cy); } + +BOOL bDrawDIB(HDC hDC, PINFO pInfo, int xStart, int yStart, int cx, int cy) +{ + HBITMAP hTmpBmp, hBmpOld, hBmp; + BOOL bSuccess; + PBITMAPINFO pbmi; + PBYTE pBits; + BITMAPINFO bmi; + PBYTE pjTmp, pjTmpBmi; + ULONG sizBMI; + HDC hdcMem; + + bSuccess = TRUE; + if (pInfo->bUseMono) { + DrawBitmap(hDC, pInfo, xStart, yStart, cx, cy); + return bSuccess; + } + + if ((hBmp = pInfo->hBmpSaved) == NULL) { + MessageBox(ghwndMain, "There's no Bitmap to draw!", "Error", MB_OK); + return FALSE; + } + + // + // Let the graphics engine to retrieve the dimension of the bitmap for us + // GetDIBits uses the size to determine if it's BITMAPCOREINFO or BITMAPINFO + // if BitCount != 0, color table will be retrieved + // + bmi.bmiHeader.biSize = 0x28; // GDI need this to work + bmi.bmiHeader.biBitCount = 0; // don't get the color table + if ((GetDIBits(hDC, hBmp, 0, 0, (LPSTR)NULL, &bmi, DIB_RGB_COLORS)) == 0) { + MessageBox(ghwndMain, "GetDIBits failed!", "Error", MB_OK); + return FALSE; + } + + // + // Now that we know the size of the image, alloc enough memory to retrieve + // the actual bits + // + if ((pBits = (PBYTE)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, + bmi.bmiHeader.biSizeImage)) == NULL) { + MessageBox(ghwndMain, "Failed in Memory Allocation for pBits!", "Error", MB_OK); + return FALSE; + } + + // + // Note: 24 bits per pixel has no color table. So, we don't have to + // allocate memory for retrieving that. Otherwise, we do. + // + pbmi = &bmi; // assume no color table + if (bmi.bmiHeader.biBitCount != 24) { // has color table + sizBMI = sizeof(BITMAPINFO)+sizeof(RGBQUAD)*(1<bmiHeader.biWidth, pbmi->bmiHeader.biHeight)) { + hBmpOld = SelectObject(hDC, hTmpBmp); + if ((GetDIBits(hDC, hBmp, 0, pbmi->bmiHeader.biHeight, (LPSTR)pBits, pbmi, DIB_RGB_COLORS))==0){ + MessageBox(ghwndMain, "Failed in GetDIBits!", "Error", MB_OK); + bSuccess = FALSE; + goto ErrExit4; + } + } else { + MessageBox(ghwndMain, "Failed in creating bitmap!", "Error", MB_OK); + bSuccess = FALSE; + goto ErrExit3; + } + + + + if (pInfo->bStretch) { + SetStretchBltMode(hDC, pInfo->iStretchMode); + StretchDIBits(hDC, xStart, yStart, cx, cy, + 0, 0, pbmi->bmiHeader.biWidth, pbmi->bmiHeader.biHeight, (LPSTR)pBits, pbmi, DIB_RGB_COLORS, SRCCOPY); + } else { + hdcMem = CreateCompatibleDC(hDC); + SelectObject(hdcMem, hBmp); + StretchDIBits(hDC, xStart, yStart, pbmi->bmiHeader.biWidth, pbmi->bmiHeader.biHeight, + 0, 0, pbmi->bmiHeader.biWidth, pbmi->bmiHeader.biHeight, (LPSTR)pBits, pbmi, DIB_RGB_COLORS, SRCCOPY); + DeleteDC(hdcMem); + } + + +ErrExit4: + SelectObject(hDC, hBmpOld); + DeleteObject(hTmpBmp); +ErrExit3: + GlobalFree(pbmi); +ErrExit1: + GlobalFree(pBits); + return bSuccess; +} + + /******************************Public*Routine******************************\ * * pGetInfoData(HWND hwnd) @@ -3215,14 +3347,14 @@ BOOL bPrintBmp(PPRTDATA pPrtData) { DocInfo.lpszOutput = NULL; StartDoc(hdcPrinter, &DocInfo); StartPage(hdcPrinter); - DrawBitmap(hdcPrinter, &(pPrtData->info), 0, 0, iWidth, iHeight); + bDrawDIB(hdcPrinter, &(pPrtData->info), 0, 0, iWidth, iHeight); EndPage(hdcPrinter); EndDoc(hdcPrinter); #else Escape(hdcPrinter, STARTDOC, 20, "Mandelbrot", NULL); - DrawBitmap(hdcPrinter, &(pPrtData->info), 0, 0, iWidth, iHeight); + bDrawDIB(hdcPrinter, &(pPrtData->info), 0, 0, iWidth, iHeight); Escape(hdcPrinter, NEWFRAME, NULL, NULL, NULL); Escape(hdcPrinter, ENDDOC, NULL, NULL, NULL);