|
9#
楼主 |
发表于 2015-7-9 11:34:40
|
只看该作者
- ' Get ready to Print!
- lngTextColor = apiSetTextColor(hMemDC, RGB(255, 255, 255)) 'White
- lngBackColor = apiSetBkColor(hMemDC, RGB(0, 0, 0)) 'Black
- ' I gave up on SetTextAlign and went with MoveToEx
- lngTemp = apiSetTextAlign(hMemDC, TA_UPDATECP)
-
- lngRet = apiMoveToEx(hMemDC, lpSZ.cx, lpSZ.cy, ByVal 0&) '(1), y(1), ByVal 0&)
- lngRet = apiTextOut(hMemDC, 0, 0, strname, Len(strname))
-
- 'Clean up by deleting our created font.
- hFont = apiSelectObject(hMemDC, prevfont)
- apiDeleteObject (hFont)
-
- 'OK..let's start to build our bitmapinfo structure
- 'Get our existing bitmap information for bitmapinfoheader
- lngRet = apiGetObject(hBitmap, LenB(MyBitmap), MyBitmap)
-
- With MyBitmapInfoHeader
- .biSize = LenB(MyBitmapInfoHeader)
- .biWidth = MyBitmap.bmWidth
- .biHeight = MyBitmap.bmHeight
- .biPlanes = 1
- .biBitCount = MyBitmap.bmPlanes * MyBitmap.bmBitsPixel
- .biCompression = 0
- .biSizeImage = 0
- .biXPelsPerMeter = 0 'lngXdpi ' * 39.370854
- .biYPelsPerMeter = 0 'lngXdpi ' * 39.370854
- .biClrUsed = 0
- .biClrImportant = 0
- End With
-
- 'Set MyBitmapInfoHeader to MyBitmapInfo.bmiHeader
- MyBitmapInfo.bmiHeader = MyBitmapInfoHeader
-
- 'Deselect the bitmap out of the dc
- 'Microsoft says the Bitmap MUST NOT be selected into an existing device context
- lngRet = apiSelectObject(hMemDC, hOrigBitmap)
-
- 'Since we are converting to a monochrome bitmap we'll just
- 'leave room for 2 colors, Foreground and Background
- lngNumColors = 2
- lngAllocMem = lngNumColors * LenB(MyRGBquad)
- 'Will need above to perform total memory requirement calculation
-
- 'Calculate biSizeImage
- MyBitmapInfo.bmiHeader.biSizeImage = MyBitmap.bmWidthBytes * MyBitmap.bmHeight
-
- 'Calculate total memory requirements
- lngAllocMem = lngAllocMem + MyBitmapInfo.bmiHeader.biSize _
- + MyBitmapInfo.bmiHeader.biSizeImage
-
- 'Allocate Calculate total storage required
- hlngMemory = apiGlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, _
- lngAllocMem) ' + 100) 'Safety First!
- lngMemoryLock = apiGlobalLock(hlngMemory)
-
- 'Call DIBits with my allocated memory as pointer for the lbits parameter
- 'Will transfer bits to our memory block
- 'We offset by 48 bytes
- '40 = MyBitmapInfoHeader structure
- ' 8 = 2 RGB Quad structures for my color table
- lngRet = apiGetDIBits(hMemDC, hBitmap, 0, MyBitmapInfoHeader.biHeight, _
- ByVal lngMemoryLock + 48, MyBitmapInfo, DIB_RGB_COLORS)
-
- 'Could probably use the original bitmap but I was having a lot
- 'of problems around here so I maintained the 2 bitmaps for debugging
- 'Create monochrome bitmap to receive the GetDIBits above
- hbitmapmono = apiCreateBitmap(MyBitmapInfoHeader.biWidth, _
- MyBitmapInfoHeader.biHeight, 1, 1, ByVal 0&)
-
- lngRet = apiSetDIBits(hMemDC, hbitmapmono, 0, MyBitmapInfoHeader.biHeight, _
- ByVal lngMemoryLock + 48, MyBitmapInfo, DIB_RGB_COLORS)
-
- 'We need to build a bitmapinfo structure in memory
- '40 bytes bitmapinfo strucure
- '8 bytes 2 RGB QUAD structures
- 'Followed by actual bitmap data of whatever size is required
-
- 'copy MyBitmapInfoHeader structure to beginning of memory block
- Call apiCopyMemory(ByVal lngMemoryLock, MyBitmapInfo.bmiHeader.biSize, 40)
- 'LenB(MyBitmapInfoHeader) = 40. I hardcoded because of trouble - not sure why.
-
- 'SetDiBits writes into the bitmapinfo color table
- 'We have to set the 2 RGB quads to match the original
- 'Values the user chose for the text control
- 'I'd really rather leave the background transparent
- 'so user could simply specify the control's background color in
- 'Form-> Design view. This would then require an ActiveX control
- 'to redraw the text after the user selects a new background color
- MyBitmapInfo.bmiColors(1).rgbBlue = UnRGB(ctlCmdButton.ForeColor, 2)
- MyBitmapInfo.bmiColors(1).rgbGreen = UnRGB(ctlCmdButton.ForeColor, 1)
- MyBitmapInfo.bmiColors(1).rgbRed = UnRGB(ctlCmdButton.ForeColor, 0)
- MyBitmapInfo.bmiColors(1).rgbReserved = 0
-
- MyBitmapInfo.bmiColors(0).rgbBlue = UnRGB(BGColor, 2)
- MyBitmapInfo.bmiColors(0).rgbGreen = UnRGB(BGColor, 1)
- MyBitmapInfo.bmiColors(0).rgbRed = UnRGB(BGColor, 0)
- MyBitmapInfo.bmiColors(0).rgbReserved = 0
-
- 'The most difficult problems cropped up with the CopyMemory sub.
- 'It's much easier in Assembler, or even C, to tell if you are
- 'working with a pointer, or a pointer to a pointer.
- 'ByRef...ByVal I'm still learning by trial and error.
- 'copy MyBitmapInfo.bmiColors(0 to 1) structure to memory block + 40
- Call apiCopyMemory(ByVal lngMemoryLock + 40, MyBitmapInfo.bmiColors(0), 8)
-
- 'DEBUG Leave in so next girl/guy can check and see if memcopy is working
- 'I miss my Assembly Debugger!
- 'Dim mys(60) as byte
- 'Call apiCopyMemory(mys(0), ByVal lngMemoryLock, 60)
-
- ReDim varpicture(lngAllocMem - 1) ' + 10) 'Safety First!
- Call apiCopyMemory(varpicture(0), ByVal lngMemoryLock, lngAllocMem) ' + 10)
-
- 'release memory lock
- lngRet = apiGlobalUnlock(hlngMemory)
-
- 'release memory block
- lngRet = apiGlobalFree(hlngMemory)
-
-
- ' Save the Bitmap to a disk file
- With FileHeader
- .bfType = &H4D42
- .bfSize = Len(FileHeader) + Len(MyBitmapInfo) + MyBitmapInfo.bmiHeader.biSize
- .bfOffBits = Len(FileHeader) + Len(MyBitmapInfo)
- End With
-
- ' Get next avail file handle
- Fnum = FreeFile
-
- ' Get the Systems Temp path
- ' Returns Length of path(num characters in path)
- lngRet = GetTempPath(PathLen, strPath)
- ' Chop off NULLS and trailing ""
- strPath = Left(strPath, lngRet) & Chr(0)
-
- ' Now need a unique Filename
- ' locked from a previous aborted attemp.
- ' Needs more work!
- strPathandFileName = GetUniqueFilename(strPath, "SLC" & Chr(0), "BMP")
-
- Open strPathandFileName For Binary As Fnum
- Put Fnum, , FileHeader
- Put Fnum, , varpicture
- Close Fnum
-
- 'Set newly created controls properties
- 'to match properties the user setup in their label control.
- 'need to match TRANSPARENT background setting in next revision.
- ctlCmdButton.Picture = strPathandFileName
-
- ' add other border/backcolor paramters here
- 'ctlCmdButton.Tag = "Rotated:" & lngRotation & " Degrees"
-
- ' If we have Rotated Text let's set the
- ' Contol's dimensions to display all of
- ' Rotated Text
- If lngRotation <> 0 Then
- With lpRect
- If .right * (1440 / lngXdpi) > ctlCmdButton.Width Then _
- ctlCmdButton.Width = .right * (1440 / lngXdpi)
-
- If .Bottom * (1440 / lngXdpi) > ctlCmdButton.Height Then _
- ctlCmdButton.Height = .Bottom * (1440 / lngXdpi)
- End With
- End If
-
-
- 'Normal Function Clean up
- lngRet = apiDeleteObject(hBitmap)
- lngRet = apiDeleteObject(hbitmapmono)
- Set ctlCmdButton = Nothing
- Set objFormReport = Nothing
-
- 'Add any other cleanup code here.
- Call apiDeleteDC(hMemDC)
- Call apiReleaseDC(hwnd, hdc)
-
- ' Delete Temp file
- Kill strPathandFileName
-
- 'Signal Function return OK
- fCmdButTextPic = True
-
- ExitHere:
- 'Perform any additional cleanup your code requires
-
- Exit Function
复制代码 |
|