c++ - WinAPI: Correctly copying a HBITMAP to the clipboard -


i have difficulties trying copy hbitmap clipboard. hbitmap created colorref array , able display correctly. here how created:

colorref* colors = new colorref[imagesize[0] * imagesize[1]];  (int = 0; < imagesize[1]; i++) {     (int j = 0; j < imagesize[0]; j++) {         colors[imagesize[0] * + j] = rgb(/* ... */);     } }  // create bitmap hbitmap hbitmap = createbitmap(imagesize[0], imagesize[1], 1, 32, (void*)colors);  delete[] colors; 

in order copy bitmap clipboard, use small piece of code:

openclipboard(hwnd); emptyclipboard();  setclipboarddata(cf_bitmap, hbitmap);  closeclipboard(); 

when execute app, able copy bitmap , paste somewhere, example in ms paint. if try copy second time, clipboard content can't pasted anymore unless first piece of code above executed again.

in msdn documentation, said

if setclipboarddata succeeds, system owns object identified hmem parameter.

i don't understand means, guess source of problem. found example of function want here, not seem use same kind of variables. example, using strings time, can found here.

i not sure how translate last example case. point me in right direction?

a comment deleted helped me find answer. have copy hbitmap hbitmap before calling setclipboarddata. way, copied bitmap can sent clipboard, , original bitmap kept later.

to copy bitmap, used code can found in copying bitmap bitmap. in code, looks this:

// create new bitmap hbitmap hbitmap_copy = createbitmap(imagesize[0], imagesize[1], 1, 32, null);  // copy source bitmap new 1     hdc srcdc = createcompatibledc(getdc(null)); hdc newdc = createcompatibledc(getdc(null));  hbitmap srcbitmap = (hbitmap)selectobject(srcdc, hbitmap); hbitmap newbitmap = (hbitmap)selectobject(newdc, hbitmap_copy);  bitblt(newdc, 0, 0, imagesize[0], imagesize[1], srcdc, 0, 0, srccopy);  selectobject(srcdc, srcbitmap); selectobject(newdc, newbitmap);  deletedc(srcdc); deletedc(newdc);  // hbitmap_copy can copied clipboard openclipboard(hwnd); emptyclipboard();     setclipboarddata(cf_bitmap, hbitmap_copy);  closeclipboard(); 

i can copy displayed bitmap many times want!


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -