设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

12下一页
返回列表 发新帖
查看: 4016|回复: 13
打印 上一主题 下一主题

[API] 【API】自定义字体如何消除锯齿化?

[复制链接]
跳转到指定楼层
1#
发表于 2012-8-21 23:55:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 roych 于 2012-8-21 23:59 编辑

       帮一个朋友做的,不过对GUI不太熟悉,这里提了一下,不过转到Access,加了这两个API声明还是没法实现,不知道大家对这方面有没有研究?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2012-8-22 08:24:09 | 只看该作者
学习学习
3#
发表于 2012-8-22 11:57:35 | 只看该作者
斑竹直接贴代码可否,3Q

点击这里给我发消息

4#
发表于 2012-8-22 12:40:07 | 只看该作者
谢谢分享
5#
 楼主| 发表于 2012-8-22 18:49:54 | 只看该作者
风中漫步 发表于 2012-8-22 11:57
斑竹直接贴代码可否,3Q

不知道你说的贴代码是指哪方面。如果是指我的例子的话,请留意报表中的事件。
而至于搜索到的那个帖子,由于是基于VB的,部分内容不适用于Access,所用的API函数部分参数,虽然之前用新字体(myfont)相应属性来处理,但没见到什么效果。

点评

呵呵,没装ACC啊  发表于 2012-8-23 11:58
6#
 楼主| 发表于 2015-7-9 11:30:01 | 只看该作者
风中漫步 发表于 2012-8-22 11:57
斑竹直接贴代码可否,3Q

好吧,挖坟。
  1. Private Const LF_FACESIZE = 32

  2. Private Type RECT
  3.   Left As Long
  4.   right As Long
  5.   top As Long
  6.   Bottom As Long
  7. End Type

  8. Private Type Size
  9.         cx As Long
  10.         cy As Long
  11. End Type

  12. Private Type SizeX2
  13.         cx As Long
  14.         cy As Long
  15.         widthX As Long
  16.         widthY As Long
  17. End Type

  18. Private Type LOGFONT
  19.         lfHeight As Long
  20.         lfWidth As Long
  21.         lfEscapement As Long
  22.         lfOrientation As Long
  23.         lfWeight As Long
  24.         lfItalic As Byte
  25.         lfUnderline As Byte
  26.         lfStrikeOut As Byte
  27.         lfCharSet As Byte
  28.         lfOutPrecision As Byte
  29.         lfClipPrecision As Byte
  30.         lfQuality As Byte
  31.         lfPitchAndFamily As Byte
  32.         lfFaceName As String * LF_FACESIZE
  33. End Type

  34. Private Type RGBQUAD
  35.         rgbBlue As Byte
  36.         rgbGreen As Byte
  37.         rgbRed As Byte
  38.         rgbReserved As Byte
  39. End Type

  40. Private Type BITMAP
  41.   bmType As Long
  42.   bmWidth As Long
  43.   bmHeight As Long
  44.   bmWidthBytes As Long
  45.   bmPlanes As Integer
  46.   bmBitsPixel As Integer
  47.   bmBits As Long
  48. End Type

  49. Private Type BITMAPINFOHEADER '40 bytes
  50.         biSize As Long
  51.         biWidth As Long
  52.         biHeight As Long
  53.         biPlanes As Integer
  54.         biBitCount As Integer
  55.         biCompression As Long
  56.         biSizeImage As Long
  57.         biXPelsPerMeter As Long
  58.         biYPelsPerMeter As Long
  59.         biClrUsed As Long
  60.         biClrImportant As Long
  61. End Type

  62. Private Type BITMAPINFO
  63.         bmiHeader As BITMAPINFOHEADER
  64.         bmiColors(1) As RGBQUAD
  65.         ' we need two colors for monochrome bitmap
  66. End Type

  67. Private Type BITMAPFILEHEADER    '14 bytes
  68.   bfType As Integer
  69.   bfSize As Long
  70.   bfReserved1 As Integer
  71.   bfReserved2 As Integer
  72.   bfOffBits As Long
  73. End Type

  74. Private Declare Sub apiCopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
  75.     (destination As Any, source As Any, ByVal Length As Long)

  76. Private Declare Function apiCreateBitmap Lib "gdi32" Alias "CreateBitmap" _
  77. (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, _
  78. ByVal nBitCount As Long, lpBits As Any) As Long

  79. Private Declare Function apiGlobalAlloc Lib "kernel32" Alias "GlobalAlloc" _
  80. (ByVal wFlags As Long, ByVal dwBytes As Long) As Long

  81. Private Declare Function apiGlobalFree Lib "kernel32" Alias "GlobalFree" _
  82. (ByVal hMem As Long) As Long

  83. Private Declare Function apiGlobalLock Lib "kernel32" Alias "GlobalLock" _
  84. (ByVal hMem As Long) As Long

  85. Private Declare Function apiGlobalUnlock Lib "kernel32" Alias "GlobalUnlock" _
  86. (ByVal hMem As Long) As Long

  87. Private Declare Function apiGetStockObject Lib "gdi32" Alias "GetStockObject" _
  88. (ByVal nIndex As Long) As Long

  89. Private Declare Function apiGetDIBits Lib "gdi32" Alias "GetDIBits" (ByVal aHDC As Long, _
  90. ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, _
  91. lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long

  92. Private Declare Function apiSetDIBits Lib "gdi32" Alias "SetDIBits" (ByVal hdc As Long, _
  93. ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, _
  94. lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long

  95. Private Declare Function apiCreateIC Lib "gdi32" Alias "CreateICA" _
  96. (ByVal lpDriverName As String, ByVal lpDeviceName As String, _
  97. ByVal lpOutput As String, lpInitData As Any) As Long

  98. Private Declare Function apiSelectObject Lib "gdi32" _
  99. Alias "SelectObject" (ByVal hdc As Long, ByVal hObject As Long) As Long

  100. Private Declare Function apiSetTextAlign Lib "gdi32" Alias "SetTextAlign" _
  101. (ByVal hdc As Long, ByVal wFlags As Long) As Long

  102. Private Declare Function apiSetTextColor Lib "gdi32" Alias "SetTextColor" _
  103. (ByVal hdc As Long, ByVal crColor As Long) As Long

  104. Private Declare Function apiSetBkColor Lib "gdi32" Alias "SetBkColor" _
  105. (ByVal hdc As Long, ByVal crColor As Long) As Long

  106. Private Declare Function apiGetTextExtentPoint32 Lib "gdi32" _
  107. Alias "GetTextExtentPoint32A" _
  108. (ByVal hdc As Long, ByVal lpSZ As String, ByVal cbString As Long, _
  109. lpsize As Size) As Long

  110. Private Declare Function apiTextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As _
  111. Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal _
  112. nCount As Long) As Long

  113. Private Declare Function apiCreateFontIndirect Lib "gdi32" Alias _
  114.         "CreateFontIndirectA" (lpLogFont As LOGFONT) As Long

  115. Private Declare Function apiGetDC Lib "user32" _
  116.   Alias "GetDC" (ByVal hwnd As Long) As Long

  117. Private Declare Function apiReleaseDC Lib "user32" _
  118.   Alias "ReleaseDC" (ByVal hwnd As Long, _
  119.   ByVal hdc As Long) As Long

  120. Private Declare Function apiCreateCompatibleDC Lib "gdi32" _
  121.   Alias "CreateCompatibleDC" (ByVal hdc As Long) As Long

  122. Private Declare Function apiDeleteDC Lib "gdi32" _
  123.   Alias "DeleteDC" (ByVal hdc As Long) As Long

  124. Private Declare Function apiBitBlt Lib "gdi32" _
  125.   Alias "BitBlt" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, _
  126.   ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, _
  127.   ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

  128. Private Declare Function apiDeleteObject Lib "gdi32" _
  129.   Alias "DeleteObject" (ByVal hObject As Long) As Long

  130. Private Declare Function apiGetObject Lib "gdi32" Alias "GetObjectA" _
  131. (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long

  132. Private Declare Function apiGetDeviceCaps Lib "gdi32" _
  133.   Alias "GetDeviceCaps" (ByVal hdc As Long, ByVal nIndex As Long) As Long

  134. Private Declare Function apiMoveToEx Lib "gdi32" Alias "MoveToEx" _
  135. (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As Any) As Long
  136. 'above was lpPoint as POINTAPI, changed to Any to allow NULL

  137. ' For Terry Kreft's API Colro Dialog Function
  138. Private Const CC_SOLIDCOLOR = &H80

  139. Private Type COLORSTRUC
  140.   lStructSize As Long
  141.   hwnd As Long
  142.   hInstance As Long
  143.   rgbResult As Long
  144.   lpCustColors As String
  145.   Flags As Long
  146.   lCustData As Long
  147.   lpfnHook As Long
  148.   lpTemplateName As String
  149. End Type

  150. Private Declare Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" _
  151.   (pChoosecolor As COLORSTRUC) As Long

  152. Private Declare Function GetTempPath Lib "kernel32" _
  153. Alias "GetTempPathA" (ByVal nBufferLength As Long, _
  154. ByVal lpBuffer As String) As Long

  155. Private Declare Function GetTempFileName _
  156.   Lib "kernel32" Alias "GetTempFileNameA" _
  157.   (ByVal lpszPath As String, _
  158.   ByVal lpPrefixString As String, _
  159.   ByVal wUnique As Long, _
  160.   ByVal lpTempFileName As String) As Long

  161. Private Const MAX_PATH = 260

  162. Private Const BLACKNESS = &H42 ' (DWORD) dest = BLACK
  163.   'Number of pixels per logical inch along the screen width.
  164. Private Const LOGPIXELSX = 88
  165.   'Number of pixels per logical inch along the screen height.
  166. Private Const LOGPIXELSY = 90
  167.   'Width and height, in pixels, of the screen of the monitor.
  168.   'DIB color table identifiers
  169. Private Const DIB_RGB_COLORS = 0 '  color table in RGBs
  170.   'TextAlign Flags
  171. Private Const TA_UPDATECP = 1
  172. Private Const PI = 3.14159265

  173. Private Const PI_180 = PI / 180#
  174.   'Use True Type Fonts ONLY!
  175. Private Const OUT_TT_ONLY_PRECIS = 7

  176. Private Const PathLen = 255

  177. Private Const DEFAULT_PALETTE = 15
  178.   'Global Memory Flags
  179. Private Const GMEM_MOVEABLE = &H2
  180. Private Const GMEM_ZEROINIT = &H40
复制代码

(代码太多,待续)
7#
 楼主| 发表于 2015-7-9 11:33:19 | 只看该作者
  1. Function fCmdButTextPic(ctl As CommandButton, Optional ByVal BGColor As Long = 16777215) As Boolean
  2. If BGColor = 0 Then BGColor = Val(Left(ctl.Tag, InStr(1, ctl.Tag, ";")))
  3. '*******************************************
  4. 'DEVELOPED AND TESTED UNDER MICROSOFT ACCESS 97 VBA ONLY
  5. 'Supports TRUE TYPE FONTS ONLY!
  6. '
  7. 'Copyright: Stephen Lebans 1999  - May not be resold
  8. '           Shareware - Cost $0.01
  9. '           Please include this header in its entirety if you use
  10. '           this function within your code.
  11. '
  12. 'Name:      fCmdBut (Function) Design View
  13. '           fCmdButTextPic(function) Form View at Runtime
  14. '
  15. 'Version:   1.0a

  16. 'Author:    Stephen Lebans
  17. 'Email:     Stephen@lebans.com
  18. 'Web Site:  www.lebans.com
  19. 'Date:      Jan 26, 2000, 10:50:18 PM
  20. '
  21. On Error GoTo ErrHandler

  22. 'Pardon my mess....


  23. 'GDI Handles
  24. Dim hFont As Long, prevfont As Long
  25. Dim hwnd As Long
  26. Dim hdc As Long
  27. Dim hMemDC As Long
  28. Dim hBitmap As Long
  29. Dim holdbitmap As Long
  30. Dim hOrigBitmap As Long
  31. Dim hbitmapmono As Long
  32. Dim lngIC As Long

  33. 'To create our Rotated Font
  34. Dim strname As String
  35. Dim fontsize As Long
  36. Dim lnglength As Long, lngTemp As Long
  37. Dim stfsize As Size
  38. Dim lpSZ As SizeX2
  39. Dim lngXposition As Long
  40. Dim lngYposition As Long
  41. Dim lngRotation As Long
  42. Dim myfont As LOGFONT
  43. Dim lngXdpi As Long
  44. Dim lngScreenXdpi As Long
  45. Dim lngTextWidth As Long
  46. Dim lngTextHeight As Long
  47. Dim lngBackColor As Long
  48. Dim lngTextColor As Long

  49. 'building a better bitmap :-)
  50. Dim lpRect As RECT
  51. Dim MyBitmap As BITMAP
  52. Dim MyBitmapInfo As BITMAPINFO
  53. Dim MyBitmapInfoHeader As BITMAPINFOHEADER
  54. Dim MyRGBquad As RGBQUAD
  55. Dim lngNumColors As Long
  56. Dim lngAllocMem As Long
  57. Dim hlngMemory As Long
  58. Dim lngMemoryLock As Long


  59. 'Temp variables
  60. Dim lngRet As Long
  61. Dim intReturn As Integer

  62. ' For System Temp Folder
  63. ' and temp unique filename
  64. Dim strPath  As String * PathLen
  65. Dim strPathandFileName  As String
  66. Dim FileHeader As BITMAPFILEHEADER
  67. Dim Fnum As Integer


  68. 'Holds the actual bitmap file
  69. Dim varpicture() As Byte
  70. 'Form & Report Cntrol Objects
  71. Dim ctlCmdButton As Control
  72. Dim objFormReport As Object
  73.       
  74. Dim MyReport As Boolean
  75. 'False = screen  True = report

  76. Dim strTemp As String


  77.     Set ctlCmdButton = ctl
  78.    
  79.     Set objFormReport = ctlCmdButton.Parent
  80.      hwnd = objFormReport.hwnd
  81.   
  82.     'retrieve a handle to a display device context (DC)
  83.     'for the client area of the specified window
  84.     hdc = apiGetDC(hwnd)
  85.     'create a memory device context (DC) compatible
  86.     'with the specified device
  87.     hMemDC = apiCreateCompatibleDC(hdc)
  88.   
  89.          
  90.     'OK setup font and print into the supplied bitmap
  91.     'First grab text from control's name property
  92.     strname = IIf(ctlCmdButton.Caption = "", "?", ctlCmdButton.Caption)
  93.     'If Asc(strname) < 0 Then strname = strname & strname
  94.     'Escapement = rotation is specified in tenths of a degree
  95.     'user defined from Tag Property
  96.     If Len(ctl.Tag & "") = 0 Then
  97.    ' Use White as Default
  98.    lngRotation = 0
  99.    Else
  100.    strTemp = Mid(ctl.Tag, (InStr(1, ctl.Tag, ";") + 1))
  101.    lngRotation = Val(strTemp)
  102.     End If
  103.    
  104.    ' lngRotation = IIf(ctlCmdButton.Tag = "", 0, ctlCmdButton.Tag)
  105.     If lngRotation < 360 Then lngRotation = Abs(lngRotation)
  106.     If lngRotation > 360 Then lngRotation = 0
  107.    
  108.     myfont.lfClipPrecision = OUT_TT_ONLY_PRECIS
  109.     myfont.lfEscapement = Abs(lngRotation) * 10
  110.     myfont.lfFaceName = ctlCmdButton.FontName & Chr$(0)  'Null character at end
  111.          
  112. 'If MyReport = False Then
  113.     lngIC = apiCreateIC("DISPLAY", vbNullString, vbNullString, vbNullString)
  114.     'If the call to CreateIC didn't fail, then get the Screen X resolution.
  115.     If lngIC <> 0 Then
  116.         lngXdpi = apiGetDeviceCaps(lngIC, LOGPIXELSX)
  117.         lngScreenXdpi = lngXdpi
  118.         'Release the information context.
  119.         apiDeleteDC (lngIC)
  120.     Else
  121.         ' Something has gone wrong. Assume an average value.
  122.        lngXdpi = 120
  123.        lngScreenXdpi = lngXdpi
  124. End If
复制代码
8#
 楼主| 发表于 2015-7-9 11:34:15 | 只看该作者
  1. 'Copy font stuff from Text Control's property sheet
  2.     fontsize = ctlCmdButton.fontsize
  3.     myfont.lfWeight = ctlCmdButton.FontWeight
  4.     myfont.lfItalic = ctlCmdButton.FontItalic
  5.     myfont.lfUnderline = ctlCmdButton.FontUnderline
  6.     'Must be a negative figure for height or system will return
  7.     'closest match on character cell not glyph
  8.     myfont.lfHeight = (fontsize / 72) * -lngXdpi
  9.                  
  10.     hFont = apiCreateFontIndirect(myfont)
  11.     prevfont = apiSelectObject(hMemDC, hFont)
  12.                
  13.     'Let's get length and height of non rotated of output string
  14.     Dim lenStep As Integer, mystrTemp As String
  15.     For lenStep = 1 To Len(strname)
  16.         mystrTemp = Mid(strname, lenStep, 1)
  17.         strname = strname & IIf(Asc(mystrTemp) = AscB(mystrTemp), "", mystrTemp)
  18.     Next
  19.     lnglength = Len(strname)
  20.     lngTemp = apiGetTextExtentPoint32(hMemDC, strname, lnglength, stfsize)
  21.      
  22. With lpRect
  23.     'Compute the coords for the text control
  24.     .Left = 1
  25.     .top = 1
  26.     .right = ctlCmdButton.Width
  27.     .Bottom = ctlCmdButton.Height

  28.     'All previous measurements were in Twips.
  29.     'ConvertTwipsToPixels = lngTwips / nTwipsPerInch * lngPixelsPerInch
  30.     .Left = .Left / 1440 * lngScreenXdpi
  31.     .top = .top / 1440 * lngScreenXdpi
  32.     .Bottom = .Bottom / 1440 * lngScreenXdpi
  33.     .right = .right / 1440 * lngScreenXdpi
  34.   
  35.   ' If use wants Rotated Text we need to make
  36.   ' the Bitmap large enough to display it.
  37.   lpSZ = BoundBox(stfsize, lpRect, lngRotation)
  38.   If .right < lpSZ.widthX Then .right = lpSZ.widthX
  39.   If .Bottom < lpSZ.widthY Then .Bottom = lpSZ.widthY
  40.    
  41.     'Force alignment to - 32  pixels for Access monochrome bitmap
  42.     'We will be converting this bitmap to monochrome later on.
  43.     .right = ((.right + 31) And &HFFFFFE0)
  44.     '.right = ((stfsize.cx + 31) And &HFFFFFE0)
  45.    
  46.     lpSZ = BoundBox(stfsize, lpRect, lngRotation)
  47.   'If .right < lpSZ.cx Then .right = lpSZ.cx
  48.   'If .Bottom < lpSZ.cy Then .Bottom = lpSZ.cy
  49.   'End If
  50.    
  51.     'Create a bitmap compatible
  52.     'with the device associated with the specified device context
  53.     'with size same as the size of the label control BUT MONOCHROME
  54.     hBitmap = apiCreateBitmap(.right, .Bottom, 1, 1, ByVal 0&)
  55. End With
  56.       
  57.     'Select the Bitmap into the specified device context
  58.     hOrigBitmap = apiSelectObject(hMemDC, hBitmap)
  59.          
  60. With lpRect
  61.      'Set all pixels to BLACK - better safe than sorry
  62.      'because you just never know!
  63.       lngRet = apiBitBlt(hMemDC, 0&, 0&, .right - .Left, _
  64.              .Bottom - .top, hMemDC, .Left, .top, BLACKNESS)  '&H42)
  65. End With
复制代码
9#
 楼主| 发表于 2015-7-9 11:34:40 | 只看该作者
  1. ' Get ready to Print!
  2.     lngTextColor = apiSetTextColor(hMemDC, RGB(255, 255, 255)) 'White
  3.     lngBackColor = apiSetBkColor(hMemDC, RGB(0, 0, 0)) 'Black
  4.     ' I gave up on SetTextAlign and went with MoveToEx
  5.     lngTemp = apiSetTextAlign(hMemDC, TA_UPDATECP)
  6.    
  7.     lngRet = apiMoveToEx(hMemDC, lpSZ.cx, lpSZ.cy, ByVal 0&) '(1), y(1), ByVal 0&)
  8.     lngRet = apiTextOut(hMemDC, 0, 0, strname, Len(strname))
  9.    
  10.     'Clean up by deleting our created font.
  11.     hFont = apiSelectObject(hMemDC, prevfont)
  12.     apiDeleteObject (hFont)
  13.    
  14.     'OK..let's start to build our bitmapinfo structure
  15.     'Get our existing bitmap information for bitmapinfoheader
  16.     lngRet = apiGetObject(hBitmap, LenB(MyBitmap), MyBitmap)
  17.    
  18. With MyBitmapInfoHeader
  19.    .biSize = LenB(MyBitmapInfoHeader)
  20.    .biWidth = MyBitmap.bmWidth
  21.    .biHeight = MyBitmap.bmHeight
  22.    .biPlanes = 1
  23.    .biBitCount = MyBitmap.bmPlanes * MyBitmap.bmBitsPixel
  24.    .biCompression = 0
  25.    .biSizeImage = 0
  26.    .biXPelsPerMeter = 0 'lngXdpi ' * 39.370854
  27.    .biYPelsPerMeter = 0 'lngXdpi  ' * 39.370854
  28.    .biClrUsed = 0
  29.    .biClrImportant = 0
  30. End With
  31.       
  32.     'Set MyBitmapInfoHeader to MyBitmapInfo.bmiHeader
  33.     MyBitmapInfo.bmiHeader = MyBitmapInfoHeader
  34.       
  35.     'Deselect the bitmap out of the dc
  36.     'Microsoft says the Bitmap MUST NOT be selected into an existing device context
  37.     lngRet = apiSelectObject(hMemDC, hOrigBitmap)
  38.    
  39.     'Since we are converting to a monochrome bitmap we'll just
  40.     'leave room for 2 colors, Foreground and Background
  41.     lngNumColors = 2
  42.     lngAllocMem = lngNumColors * LenB(MyRGBquad)
  43.     'Will need above to perform total memory requirement calculation
  44.       
  45.     'Calculate biSizeImage
  46.     MyBitmapInfo.bmiHeader.biSizeImage = MyBitmap.bmWidthBytes * MyBitmap.bmHeight
  47.    
  48.     'Calculate total memory requirements
  49.     lngAllocMem = lngAllocMem + MyBitmapInfo.bmiHeader.biSize _
  50.     + MyBitmapInfo.bmiHeader.biSizeImage
  51.    
  52.     'Allocate Calculate total storage required
  53.     hlngMemory = apiGlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, _
  54.     lngAllocMem) ' + 100)  'Safety First!
  55.     lngMemoryLock = apiGlobalLock(hlngMemory)
  56.    
  57.     'Call DIBits with my allocated memory as pointer for the lbits parameter
  58.     'Will transfer bits to our memory block
  59.     'We offset by 48 bytes
  60.     '40 = MyBitmapInfoHeader structure
  61.     ' 8 = 2 RGB Quad structures for my color table
  62.     lngRet = apiGetDIBits(hMemDC, hBitmap, 0, MyBitmapInfoHeader.biHeight, _
  63.     ByVal lngMemoryLock + 48, MyBitmapInfo, DIB_RGB_COLORS)
  64.    
  65.     'Could probably use the original bitmap but I was having a lot
  66.     'of problems around here so I maintained the 2 bitmaps for debugging
  67.     'Create monochrome bitmap to receive the GetDIBits above
  68.     hbitmapmono = apiCreateBitmap(MyBitmapInfoHeader.biWidth, _
  69.     MyBitmapInfoHeader.biHeight, 1, 1, ByVal 0&)
  70.         
  71.     lngRet = apiSetDIBits(hMemDC, hbitmapmono, 0, MyBitmapInfoHeader.biHeight, _
  72.     ByVal lngMemoryLock + 48, MyBitmapInfo, DIB_RGB_COLORS)
  73.    
  74.     'We need to build a bitmapinfo structure in memory
  75.     '40 bytes bitmapinfo strucure
  76.     '8 bytes 2 RGB QUAD structures
  77.     'Followed by actual bitmap data of whatever size is required
  78.    
  79.     'copy MyBitmapInfoHeader structure to beginning of memory block
  80.     Call apiCopyMemory(ByVal lngMemoryLock, MyBitmapInfo.bmiHeader.biSize, 40)
  81.     'LenB(MyBitmapInfoHeader) = 40. I hardcoded because of trouble - not sure why.
  82.       
  83.     'SetDiBits writes into the bitmapinfo color table
  84.     'We have to set the 2 RGB quads to match the original
  85.     'Values the user chose for the text control
  86.     'I'd really rather leave the background transparent
  87.     'so user could simply specify the control's background color in
  88.     'Form-> Design view. This would then require an ActiveX control
  89.     'to redraw the text after the user selects a new background color
  90.     MyBitmapInfo.bmiColors(1).rgbBlue = UnRGB(ctlCmdButton.ForeColor, 2)
  91.     MyBitmapInfo.bmiColors(1).rgbGreen = UnRGB(ctlCmdButton.ForeColor, 1)
  92.     MyBitmapInfo.bmiColors(1).rgbRed = UnRGB(ctlCmdButton.ForeColor, 0)
  93.     MyBitmapInfo.bmiColors(1).rgbReserved = 0
  94.    
  95.     MyBitmapInfo.bmiColors(0).rgbBlue = UnRGB(BGColor, 2)
  96.     MyBitmapInfo.bmiColors(0).rgbGreen = UnRGB(BGColor, 1)
  97.     MyBitmapInfo.bmiColors(0).rgbRed = UnRGB(BGColor, 0)
  98.     MyBitmapInfo.bmiColors(0).rgbReserved = 0
  99.                        
  100.     'The most difficult problems cropped up with the CopyMemory sub.
  101.     'It's much easier in Assembler, or even C, to tell if you are
  102.     'working with a pointer, or a pointer to a pointer.
  103.     'ByRef...ByVal I'm still learning by trial and error.
  104.     'copy MyBitmapInfo.bmiColors(0 to 1) structure to memory block + 40
  105.     Call apiCopyMemory(ByVal lngMemoryLock + 40, MyBitmapInfo.bmiColors(0), 8)
  106.       
  107.     'DEBUG Leave in so next girl/guy can check and see if memcopy is working
  108.     'I miss my Assembly Debugger!
  109.     'Dim mys(60) as byte
  110.     'Call apiCopyMemory(mys(0), ByVal lngMemoryLock, 60)
  111.    
  112.     ReDim varpicture(lngAllocMem - 1) ' + 10) 'Safety First!
  113.     Call apiCopyMemory(varpicture(0), ByVal lngMemoryLock, lngAllocMem) ' + 10)
  114.    
  115.     'release memory lock
  116.     lngRet = apiGlobalUnlock(hlngMemory)
  117.         
  118.     'release memory block
  119.     lngRet = apiGlobalFree(hlngMemory)
  120.    
  121.    
  122.     ' Save the Bitmap to a disk file
  123.     With FileHeader
  124.       .bfType = &H4D42
  125.       .bfSize = Len(FileHeader) + Len(MyBitmapInfo) + MyBitmapInfo.bmiHeader.biSize
  126.       .bfOffBits = Len(FileHeader) + Len(MyBitmapInfo)
  127.     End With

  128.     ' Get next avail file handle
  129.     Fnum = FreeFile

  130.     ' Get the Systems Temp path
  131.     ' Returns Length of path(num characters in path)
  132.     lngRet = GetTempPath(PathLen, strPath)
  133.     ' Chop off NULLS and trailing ""
  134.     strPath = Left(strPath, lngRet) & Chr(0)
  135.    
  136.     ' Now need a unique Filename
  137.     ' locked from a previous aborted attemp.
  138.     ' Needs more work!
  139.     strPathandFileName = GetUniqueFilename(strPath, "SLC" & Chr(0), "BMP")
  140.    
  141.     Open strPathandFileName For Binary As Fnum
  142.     Put Fnum, , FileHeader
  143.     Put Fnum, , varpicture
  144.     Close Fnum
  145.    
  146.     'Set newly created controls properties
  147.     'to match properties the user setup in their label control.
  148.     'need to match TRANSPARENT background setting in next revision.
  149.     ctlCmdButton.Picture = strPathandFileName
  150.    
  151.     ' add other border/backcolor paramters here
  152.     'ctlCmdButton.Tag = "Rotated:" & lngRotation & " Degrees"
  153.    
  154.     ' If we have Rotated Text let's set the
  155.     ' Contol's dimensions to display all of
  156.     ' Rotated Text
  157.     If lngRotation <> 0 Then
  158.     With lpRect
  159.         If .right * (1440 / lngXdpi) > ctlCmdButton.Width Then _
  160.         ctlCmdButton.Width = .right * (1440 / lngXdpi)
  161.   
  162.         If .Bottom * (1440 / lngXdpi) > ctlCmdButton.Height Then _
  163.         ctlCmdButton.Height = .Bottom * (1440 / lngXdpi)
  164.   End With
  165.   End If
  166.    
  167.    
  168.     'Normal Function Clean up
  169.     lngRet = apiDeleteObject(hBitmap)
  170.     lngRet = apiDeleteObject(hbitmapmono)
  171.     Set ctlCmdButton = Nothing
  172.     Set objFormReport = Nothing
  173.    
  174.     'Add any other cleanup code here.
  175.     Call apiDeleteDC(hMemDC)
  176.     Call apiReleaseDC(hwnd, hdc)
  177.    
  178.     ' Delete Temp file
  179.     Kill strPathandFileName
  180.    
  181.     'Signal Function return OK
  182.     fCmdButTextPic = True
  183.    
  184. ExitHere:
  185.     'Perform any additional cleanup your code requires
  186.    
  187. Exit Function
复制代码
10#
 楼主| 发表于 2015-7-9 11:35:04 | 只看该作者
  1. ErrHandler:
  2.     'Oh oh, we've been bad..very bad
  3.     fCmdButTextPic = False
  4.     Resume ExitHere
  5.   
  6. End Function


  7. ' Here's our Entry point from a Custom Menu Item
  8. ' in Form Design View

  9. Public Function CmdBut()

  10. 'Form & Report Cntrol Objects
  11. Dim ctl As Control

  12. 'This object is either a Form or Report
  13. Dim objFormReport As Object
  14.       
  15. Dim MyReport As Boolean
  16. 'False = screen  True = report

  17. Dim strTemp As String
  18. Dim lngColor As Long
  19. Dim boolTemp As Boolean
  20. Dim lngRet As Long
  21.     '************WARNING**********************************************
  22. 'Do not step through in DEBUG mode until the StepOK LABEL
  23. 'Obviously the desired Screen.ActiveControl assignment would fail!

  24. On Error Resume Next
  25.     Set ctl = Screen.ActiveControl
  26.     If ctl Is Nothing Then
  27.         lngRet = MsgBox("Sorry - you MUST select a Command Button Control!")
  28.         GoTo ErrHandler
  29.     End If
  30. StepOK:

  31.     If Not TypeOf ctl Is CommandButton Then
  32.         lngRet = MsgBox("Sorry - you MUST select a Command Button Control!")
  33.         GoTo ErrHandler
  34.     End If

  35.     Set objFormReport = ctl.Parent
  36.     If objFormReport Is Nothing Then
  37.         lngRet = MsgBox("Sorry - you MUST be in Form Design View!")
  38.         GoTo ErrHandler
  39.     End If

  40.     'See if we are not in Form Design View
  41.        If objFormReport.CurrentView <> 0 Then
  42.         lngRet = MsgBox("Sorry - you MUST be in Form Design View!")
  43.         GoTo ErrHandler
  44.         End If
  45.    
  46.       
  47.    ' Check Tag property to see if a Color is specified
  48.    ' User allowed to place 2 items in Tag
  49.    ' First is Color then Rotation in Degree
  50.    ' seperated by ";"
  51.    'If Len(ctl.Tag & "") = 0 Then
  52.    ' Use White as Default
  53.    'lngColor = RGB(255, 255, 255)
  54.    'Else
  55.    'strTemp = Left(ctl.Tag, (InStr(1, ctl.Tag, ";") - 1))
  56.    'lngColor = Val(strTemp)
  57.    'End If
  58.    
  59.    ' Uncomment above if you don't want to use Color Picker
  60.    ' let's call Color Picker Dialog
  61.    lngColor = aDialogColor()
  62.    boolTemp = fCmdButTextPic(ctl, lngColor)
  63.    
  64. ErrHandler:
  65.    
  66.    Set ctl = Nothing

  67. 'This object is either a Form or Report
  68. Set objFormReport = Nothing
  69. End Function
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-4-19 13:20 , Processed in 0.113143 second(s), 35 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表