Click to See Complete Forum and Search --> : How to access object from its pointer


Carl
September 7th, 2001, 02:19 AM
In a VB application, I want to add all symbol TrueType font facename to a combobox in a form. I use EnumFontFamiliesEx API function and
EnumFontFamExProc callback function to implement it. My code is like:
In form:

Dim udtLogFont as LOGFONT
udtLogFont.lfCharSet = SYMBOL_CHARSET
udtLogFont.lfPitchAndFamily = 0
EnumFontFamiliesEx me.hdc, udtLogFont, AddressOf EnumFontFamExProc, ObjPtr(cboFont), 0




In module:

public Function EnumFontFamExProc(udtElfe as ENUMLOGFONTEX, udtNtme as NEWTEXTMETRICEX, byval nFontType as Integer, lParam as ComboBox) as
Integer
Dim sFaceName as string, sFontName as string

on error resume next

If nFontType = TRUETYPE_FONTTYPE then
sFaceName = StrConv(udtElfe.elfLogFont.lfFaceName, vbUnicode)
sFontName = Left(sFaceName, InStr(sFaceName, vbNullChar) - 1)
'Now how I can get the combobox object from lParam?
'..................
End If
EnumFontFamExProc = 1
End Function




In EnumFontFamExProc, lParam just is ObjPtr(cboFont). How I can call AddItem method of cboFont Object? In other words, How I can get the
combobox object from lParam? I read the article "Not reference counting" in MSDN, it has some code to get object by CopyMemory, I do so but illegal operation is caused. Any help or suggestions is appreciated.

DSJ
September 7th, 2001, 09:26 AM
Instead of trying to pass an object pointer, pass the window handle of the combobox and use the sendmessage API to add the fontnames....


private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (byval hwnd as Long, byval wMsg as Long, byval wParam as Long, byval lParam as Any) as Long

private Const CB_ADDSTRING = &H143

public Function EnumFontFamExProc(udtElfe as ENUMLOGFONTEX, udtNtme as NEWTEXTMETRICEX, byval nFontType as Integer, byval lParam as Long) as Integer
Dim sFaceName as string, sFontName as string
on error resume next
If nFontType = TRUETYPE_FONTTYPE then
sFaceName = StrConv(udtElfe.elfLogFont.lfFaceName, vbUnicode)
sFontName = Left(sFaceName, InStr(sFaceName, vbNullChar) - 1)
'Now how I can get the combobox object from lParam?
'..................
SendMessage lParam, CB_ADDSTRING, 0, sFontName
End If
EnumFontFamExProc = 1
End Function