Private Function PNGfromAttrString() as Ptr
// 文字列を指定してクラスオブジェクトを取得する。最初に一回宣言しておけばよい。
Declare Function NSClassFromString Lib "Cocoa" (aClassName As CFStringRef) As Ptr
// TextAreaの取得
declare function documentView lib "Cocoa" selector "documentView" (obj_id as Integer) as Ptr // Return NSTextView*
Dim pnt1 As Ptr = documentView(TextArea1.Handle) // ef.Handle = NSScrollView*
// NSTextStorageの取得>NSAttributedStringの取得
declare function textStorage lib "Cocoa" selector "textStorage" (obj_id as Ptr) As Ptr // Return NSTextStorage*
Dim pnt2 As Ptr = textStorage(pnt1)
// サイズのセット (スクロールバー、ルーラーの領域分をそれぞれ引いている。数値は現物合わせのため、誤差を含む可能性あり。)
Dim rect As CGRect
if vFlg=false then // 横
rect = CGRectMake(me.Left+TextArea1.Left,me.Top+TextArea1.Top+kTopMgnH,TextArea1.Width-kRightMgnH,TextArea1.Height-kBottomMgnH)
else // 縦
rect = CGRectMake(me.Left+TextArea1.Left+kLeftMgnV,me.Top+TextArea1.Top,TextArea1.Width-kRightMgnV,TextArea1.Height-kBottomMgnV)
end if
// ウィンドウナンバーの取得
Declare Function windowNumber Lib "Cocoa" Selector "windowNumber" (receiver As Integer) As Integer
Dim window_id As Integer = windowNumber(me.Handle)
// ウィンドウのキャプチャー(Bitwise.ShiftLeft(1,3) = kCGWindowListOptionIncludingWindow (1 << 3)、0 = kCGWindowImageDefault)
Declare Function CGWindowListCreateImage Lib "Carbon" (rect As CGRect, winopt As Integer, winid As Integer, imgopt As Integer) As Ptr
Dim cgimage As Ptr = CGWindowListCreateImage(rect, Bitwise.ShiftLeft(1,3), window_id, 0)
// NSBitmapImageRep初期化
Dim bitmapImageRep As Ptr = NSClassFromString("NSBitmapImageRep")
Declare Function alloc Lib "Cocoa" Selector "alloc" (receiver As Ptr) As Ptr
bitmapImageRep = alloc(bitmapImageRep)
Declare Function initWithCGImage Lib "Cocoa" Selector "initWithCGImage:" (receiver As Ptr, img As Ptr) As Ptr
bitmapImageRep = initWithCGImage(bitmapImageRep, cgimage)
// falseをNSNumber形式に変換
Dim numb As Ptr = NSClassFromString("NSNumber")
Declare Function numberWithBool Lib "Cocoa" Selector "numberWithBool:" (receiver As Ptr, path As Boolean) As Ptr
numb = numberWithBool(numb, false)
// PNG用オプションのセット
Dim dict As Ptr = NSClassFromString("NSDictionary")
Declare Function dictionaryWithObject Lib "Cocoa" Selector "dictionaryWithObject:forKey:" (receiver As Ptr, objt As Ptr, key As CFStringRef) As Ptr
dict = dictionaryWithObject(dict, numb, "NSImageInterlaced")
// PNG出力(NSPNGFileType = 4)
Declare Function representationUsingType Lib "Cocoa" Selector "representationUsingType:properties:" (receiver As Ptr, type As Integer, prop As Ptr) As Ptr
Dim pngData As Ptr = representationUsingType(bitmapImageRep, 4, dict)
// clean up
Declare Sub release Lib "Cocoa" Selector "release" (receiver As Ptr)
release(bitmapImageRep)
// PNG形式で返す
return pngData
End Function