|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionRecently, I had cause to save a DIB to the clipboard. I tried numerous methods over a four-week period, but none of them worked (many thanks to Chris Losinger and Christian Graus for their willingness to try and provide me with guidance, an answer, or both). This article describes how I finally attained my goal, but also falls neatly into the category It Ain't Pretty But It Works. Trials and TribulationsSince the reason I need to do this is fairly well out of the scope of this article, suffice it to say that my
starting point was a At first, I wandered around trying to create a bitmap handle with the info I had. I was sure I was doing it right, but with failure after failure where moving that bitmap to the clipboard was concerned, I (naturally) assumed I must have really mucked it up, so I went looking for help, and ended up using Chris Maunder's CDIBSectionLite class. Chris's class contains all the code necessary to turn my captured bitmap into a DIBSection, but it lacked the code to move the DIB to the clipboard. This turned out to be a great place to start. However, no matter what I tried, I couldn't get the bitmap to the clipboard. How It all Came TogetherAfter doinking around for a week trying to coerce the desired functionality out of my code (and CDIBSectionLite), I called the Microsoft Software Developer Hotline (using one of two available free incidents on my MSDN subscription). The guy at MS steered me to a sample that evidently comes with the compiler called wincap32. This sample program will capture the contents of the selected window and convert it to a DIB, and store it on the clipboard. Perfect. After changing a one or two #include statements, and changing the name of a file, I compiled my code, ran the resulting program, and bingo - DIB on the clipboard! What I didThe first thing I did was to add this function to the HANDLE CDIBSectionLite::PutOnClipboard()
{
HANDLE hResult = NULL;
if (::OpenClipboard(NULL))
{
::EmptyClipboard();
::GdiFlush();
// borrowed these variables from the sample app
HDIB hDib = NULL;
HBITMAP hBitmap = NULL;
HPALETTE ghPal = NULL;
if (m_hBitmap)
{
// call the function that converts the bitmap to a DIB
hDib = BitmapToDIB(m_hBitmap, ghPal);
if (hDib)
{
// ahhh, the sweet smell of success
hResult = ::SetClipboardData(CF_DIB, hDib);
if (hResult == NULL)
{
_ShowLastError();
}
}
else
{
MessageBeep(0);
}
}
::CloseClipboard();
}
return hResult;
}
From the Microsoft sample application, I copied the following files into my project directory: DIBUTIL.C
DIBUTIL.H
DIBAPI.H
I then renamed the DIBUTIL.C file to DIBUTIL.CPP. After that, I had to make the following changes in that file so that it would compile. The sample app compiled as is, but because I included stdafx.h, the compiler puked on some type mismatches, hence the following changes: Line 30: #include <windows.h> to: #include "stdafx.h" Line 381: lpbi = GlobalLock(hDIB); to: lpbi = (LPSTR)GlobalLock(hDIB); Line 524: lpDIBHdr = GlobalLock(hDIB); to: lpDIBHdr = (LPSTR)GlobalLock(hDIB); Line 608: hPal = GetStockObject(DEFAULT_PALETTE); to: hPal = (HPALETTE)GetStockObject(DEFAULT_PALETTE); Finally, I was using the whole shebang as follows: // I needed to construct the BITMAPINFO structure to be passed to the // CDIBSectionLite object would be happy HBITMAP hBitmap; BITMAPINFO bmi; memset(&bmi, 0, sizeof(BITMAPINFO)); // use the BITMAPINFOHEADER structure that we captured bmi.bmiHeader = cb.bih; CDIBSectionLite dib; // use the BITMAPINFO struct that we created above and the bits we captured dib.SetBitmap(&bmi, cb.pBuffer); dib.PutOnClipboard(); In The EndIt works. I'm not really interested in culling out just the stuff I need from the files I harvested from the Microsoft sample app, so I intend on just leaving it all in there. This is an exercise I am leaving to the reader. The zip file accompanying this article contains both the modified Due to the size of the bitmaps I'm capturing, I didn't equip
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||