Re: Creating drag images from NSView snapshots
Re: Creating drag images from NSView snapshots
- Subject: Re: Creating drag images from NSView snapshots
- From: Ken Tozier <email@hidden>
- Date: Sun, 1 Jul 2007 10:02:17 -0400
Thanks Ron
This is what I ended up doing to get a snapshot of the view. It does
basically what I want except for the transparency part.
- (void) mouseDragged:(NSEvent *) inEvent
{
NSRect imgFrame = NSMakeRect(0, 0, [self frame].size.width,
[self frame].size.height);
[self lockFocus];
NSBitmapImageRep *selfBitmap = [[[NSBitmapImageRep alloc]
initWithFocusedViewRect: imgFrame] autorelease];
[self unlockFocus];
NSImage *selfImage = [[[NSImage alloc] initWithSize:
imgFrame.size] autorelease];
NSPasteboard *pboard = [NSPasteboard
pasteboardWithName:NSDragPboard];
[selfImage addRepresentation: selfBitmap];
[pboard declareTypes: [NSArray arrayWithObject: NSTIFFPboardType]
owner: self];
[pboard setData:[selfImage TIFFRepresentation] forType:
NSTIFFPboardType];
[self dragImage: selfImage
at: NSMakePoint(0, 0)
offset: NSMakeSize(0.0, 0.0)
event: inEvent
pasteboard: pboard
source: self
slideBack: YES];
}
Ken
On Jul 1, 2007, at 9:15 AM, Ron Fleckner wrote:
Date: Sat, 30 Jun 2007 22:20:05 -0400
From: Ken Tozier <email@hidden>
Hi
I have a custom view that I want to have one of those nice
semitransparent snapshots during drags but can't find any info on how
to actually create these snapshots. I'm sure this has been done a
million times but I must not be googling for the correct terms as
most of the hits I'm getting relate to Windows XP/Vista. How do you
do you get a quick snapshot of an NSView?
Thanks in advance
Ken
Ken,
the following is from Hillegass' "Cocoa Programming for Mac OS X"
second edition. I'm pretty sure it's OK to copy and send this, as
the code is available publicly.
// This method does a lot of stuff, but essentially, creates an
image to show
// as the drag operation takes place, and also creates the drag
operation at
// the end of the method. The drag operation is controlled by
// -dragImage:at:offset:event:pasteboard:source:slideBack.
- (void)mouseDragged:(NSEvent *)event
{
NSRect imageBounds;
NSPasteboard *pb;
NSImage *anImage;
NSSize s;
NSPoint p;
// Get the size of the string
s = [string sizeWithAttributes:attributes];
// Create the image that will be dragged
anImage = [[NSImage alloc] initWithSize:s];
// Create a rect in which you will draw the letter in the image
imageBounds.origin = NSMakePoint(0,0);
imageBounds.size = s;
// Draw the letter on the image
[anImage lockFocus];
[self drawStringCenteredIn:imageBounds];
[anImage unlockFocus];
// Get the location of the drag event
p = [self convertPoint:[event locationInWindow] fromView:nil];
// Drag from the centre of the image
p.x = p.x - s.width / 2;
p.y = p.y - s.height / 2;
// Get the pasteboard
pb = [NSPasteboard pasteboardWithName:NSDragPboard];
// Put the string on the pasteboard
[self writeStringToPasteboard:pb];
// Start the drag
[self dragImage:anImage
at:p
offset:NSMakeSize(0, 0)
event:event
pasteboard:pb
source:self
slideBack:YES];
[anImage release];
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden