|
|
// *********************************************************
// ファイルからビットマップをロード
// ターゲットウインドウが設定されている必要があります
// 戻り値 : true 成功, flase 失敗
// *********************************************************
BOOL LboxBitmap::LoadFromFile( LboxString *LPath )
{
return LboxBitmap::LoadFromFile( LPath->szLboxString );
}
BOOL LboxBitmap::LoadFromFile( LPTSTR lpPath )
{
if ( this->hWnd == NULL ) {
return false;
}
if ( this->hBitmap != NULL ) {
DeleteObject( this->hBitmap );
this->hBitmap = NULL;
}
this->hBitmap =
(HBITMAP)LoadImage(
0,
lpPath,
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE
);
if ( this->hBitmap == NULL ) {
return false;
}
int nRet;
nRet = GetObject(
this->hBitmap,
sizeof(BITMAP),
&(this->Info)
);
if ( nRet == 0 ) {
DeleteObject( this->hBitmap );
this->hBitmap = NULL;
return false;
}
RedrawWindow(
this->hWnd,
NULL,
NULL,
RDW_INVALIDATE | RDW_ERASENOW
);
return true;
}
| |
|
|
|
|
// *********************************************************
// リソースからビットマップをロード
// ターゲットウインドウが設定されている必要があります
// 戻り値 : true 成功, flase 失敗
// *********************************************************
BOOL LboxBitmap::LoadFromResource( WORD nId )
{
if ( this->hWnd == NULL ) {
return false;
}
if ( this->hBitmap != NULL ) {
DeleteObject( this->hBitmap );
this->hBitmap = NULL;
}
this->hBitmap =
(HBITMAP)LoadImage(
LboxGetInstance( this->hWnd ),
MAKEINTRESOURCE(nId),
IMAGE_BITMAP,
0,
0,
LR_DEFAULTCOLOR
);
if ( this->hBitmap == NULL ) {
return false;
}
int nRet;
nRet = GetObject(
this->hBitmap,
sizeof(BITMAP),
&(this->Info)
);
if ( nRet == 0 ) {
DeleteObject( this->hBitmap );
this->hBitmap = NULL;
return false;
}
RedrawWindow(
this->hWnd,
NULL,
NULL,
RDW_INVALIDATE | RDW_ERASENOW
);
return true;
}
| |
|
|
|
|
// *********************************************************
// ビットマップをファイルに保存
// 戻り値 : true 成功, flase 失敗
// *********************************************************
BOOL LboxBitmap::SaveToFile( LboxString *LPath )
{
return LboxBitmap::SaveToFile( LPath->szLboxString );
}
BOOL LboxBitmap::SaveToFile( LPTSTR lpPath )
{
if ( this->hBitmap == NULL ) {
return false;
}
if ( this->pDib != NULL ) {
this->FreeDib();
}
if ( !(LboxBitmap::DibFromBitmap()) ) {
return false;
}
BITMAPFILEHEADER bfh;
bfh.bfType = 0x4d42;
bfh.bfSize = this->dwDibSize + sizeof(BITMAPFILEHEADER);
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfOffBits =
sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER) +
this->dwPaletteSize;
HANDLE hFile;
hFile = CreateFile(
lpPath,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if ( hFile == NULL ) {
return false;
}
DWORD dwSize;
WriteFile(
hFile,
&bfh,
sizeof( BITMAPFILEHEADER ),
&dwSize,
NULL
);
WriteFile(
hFile,
this->pDib,
this->dwDibSize,
&dwSize,
NULL
);
CloseHandle( hFile );
return true;
}
| |
|
|
|
|
// *********************************************************
// 描画
// 戻り値 : true 成功, flase 失敗
// *********************************************************
BOOL LboxBitmap::Draw( HDC hDc )
{
if ( this->hWnd == NULL ) {
return false;
}
if ( this->hBitmap == NULL ) {
return false;
}
if ( this->hWnd != WindowFromDC( hDc ) ) {
return false;
}
RECT rt;
GetClientRect( this->hWnd, &rt );
HDC hDCMem;
HGDIOBJ hOld;
// メモリデバイスコンテキストを作成
hDCMem = CreateCompatibleDC( hDc );
if ( hDCMem == NULL ) {
return false;
}
// メモリデバイスに、ビットマップを選択
hOld = SelectObject( hDCMem, this->hBitmap );
if ( hOld == NULL ) {
DeleteDC( hDCMem );
return false;
}
HBRUSH hBrush;
HBRUSH hBrushOld;
if ( this->hBack != NULL ) {
hBrush = this->hBack;
}
else {
hBrush = (HBRUSH)GetStockObject( WHITE_BRUSH );
}
hBrushOld = (HBRUSH)SelectObject( hDc, hBrush );
int w,h;
if ( this->Info.bmWidth < rt.right - rt.left ) {
w = this->Info.bmWidth;
PatBlt(
hDc,
this->Info.bmWidth, 0,
rt.right - rt.left - w,
rt.bottom - rt.top,
PATCOPY
);
}
else {
w = rt.right - rt.left;
}
if ( this->Info.bmHeight < rt.bottom - rt.top ) {
h = this->Info.bmHeight;
PatBlt(
hDc,
0, this->Info.bmHeight,
rt.right - rt.left,
rt.bottom - rt.top - h,
PATCOPY
);
}
else {
h = rt.bottom - rt.top;
}
SelectObject( hDc, hBrushOld );
BitBlt(
hDc,
0,
0,
w,
h,
hDCMem,
0,
0,
SRCCOPY
);
SelectObject( hDCMem, hOld );
DeleteDC( hDCMem );
return true;
}
| |
|
|
|
|
// *********************************************************
// 伸縮描画
// 戻り値 : true 成功, flase 失敗
// *********************************************************
BOOL LboxBitmap::StretchDraw( HDC hDc )
{
if ( this->hWnd == NULL ) {
return false;
}
if ( this->hBitmap == NULL ) {
return false;
}
if ( this->hWnd != WindowFromDC( hDc ) ) {
return false;
}
RECT rt;
GetClientRect( this->hWnd, &rt );
HDC hDCMem;
HGDIOBJ hOld;
// メモリデバイスコンテキストを作成
hDCMem = CreateCompatibleDC( hDc );
if ( hDCMem == NULL ) {
return false;
}
// メモリデバイスに、ビットマップを選択
hOld = SelectObject( hDCMem, this->hBitmap );
if ( hOld == NULL ) {
DeleteDC( hDCMem );
return false;
}
StretchBlt(
hDc,
0,
0,
rt.right - rt.left,
rt.bottom - rt.top,
hDCMem,
0,
0,
this->Info.bmWidth,
this->Info.bmHeight,
SRCCOPY
);
SelectObject( hDCMem, hOld );
DeleteDC( hDCMem );
return true;
}
| |
|
|
|
|
// *********************************************************
// 対象領域より大きい場合は縮小描画
// 戻り値 : true 成功, flase 失敗
// *********************************************************
BOOL LboxBitmap::FitDraw( HDC hDc )
{
if ( this->hWnd == NULL ) {
return false;
}
if ( this->hBitmap == NULL ) {
return false;
}
if ( this->hWnd != WindowFromDC( hDc ) ) {
return false;
}
RECT rt;
GetClientRect( this->hWnd, &rt );
HDC hDCMem;
HGDIOBJ hOld;
// メモリデバイスコンテキストを作成
hDCMem = CreateCompatibleDC( hDc );
if ( hDCMem == NULL ) {
return false;
}
// メモリデバイスに、ビットマップを選択
hOld = SelectObject( hDCMem, this->hBitmap );
if ( hOld == NULL ) {
DeleteDC( hDCMem );
return false;
}
HBRUSH hBrush;
HBRUSH hBrushOld;
if ( this->hBack != NULL ) {
hBrush = this->hBack;
}
else {
hBrush = (HBRUSH)GetStockObject( WHITE_BRUSH );
}
hBrushOld = (HBRUSH)SelectObject( hDc, hBrush );
int w,h;
if ( this->Info.bmWidth < rt.right - rt.left ) {
w = this->Info.bmWidth;
PatBlt(
hDc,
this->Info.bmWidth, 0,
rt.right - rt.left - w,
rt.bottom - rt.top,
PATCOPY
);
}
else {
w = rt.right - rt.left;
}
if ( this->Info.bmHeight < rt.bottom - rt.top ) {
h = this->Info.bmHeight;
PatBlt(
hDc,
0, this->Info.bmHeight,
rt.right - rt.left,
rt.bottom - rt.top - h,
PATCOPY
);
}
else {
h = rt.bottom - rt.top;
}
SelectObject( hDc, hBrushOld );
if ( this->Info.bmWidth > rt.right - rt.left ||
this->Info.bmHeight > rt.bottom - rt.top ) {
StretchBlt(
hDc,
0,
0,
w,
h,
hDCMem,
0,
0,
this->Info.bmWidth,
this->Info.bmHeight,
SRCCOPY
);
}
else {
BitBlt(
hDc,
0,
0,
w,
h,
hDCMem,
0,
0,
SRCCOPY
);
}
SelectObject( hDCMem, hOld );
DeleteDC( hDCMem );
return true;
}
| |
|
|
|
|
// *********************************************************
// 画面の部分ビットマップの取得
// 戻り値 : true 成功, flase 失敗
// *********************************************************
// デスクトップ
BOOL LboxBitmap::GetFromScreen( void )
{
RECT rt;
GetWindowRect( GetDesktopWindow(), &rt );
return this->GetFromScreen( &rt );
}
// ウインドウ
BOOL LboxBitmap::GetFromScreen( HWND hTarget )
{
RECT rt;
GetWindowRect( hTarget, &rt );
return this->GetFromScreen( &rt );
}
// コントロール
BOOL LboxBitmap::GetFromScreen( HWND hDlg, int nID )
{
RECT rt;
GetWindowRect( GetDlgItem( hDlg, nID ), &rt );
return this->GetFromScreen( &rt );
}
// 大きさ指定
BOOL LboxBitmap::GetFromScreen( LPRECT lpRect )
{
if ( IsRectEmpty( lpRect ) ) {
return false;
}
HDC hScrDC,hMemDC; // 画面DCとメモリDC
hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
hMemDC = CreateCompatibleDC(hScrDC);
HBITMAP hCur,hOld; // ビットマップのハンドル
int nX,nY,nX2,nY2; // 取得する長方形の座標
int nWidth,nHeight; // DIBの幅と高さ
int xScrn,yScrn; // 画面の解像度
nX = lpRect->left;
nY = lpRect->top;
nX2 = lpRect->right;
nY2 = lpRect->bottom;
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
if ( nX < 0 ) {
nX = 0;
}
if ( nY < 0 ) {
nY = 0;
}
if ( nX2 > xScrn ) {
nX2 = xScrn;
}
if ( nY2 > yScrn ) {
nY2 = yScrn;
}
nWidth = nX2 - nX;
nHeight = nY2 - nY;
hCur = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
hOld = (HBITMAP)SelectObject(hMemDC, hCur);
BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY) ;
hCur = (HBITMAP)SelectObject(hMemDC, hOld);
DeleteDC(hScrDC);
DeleteDC(hMemDC);
if ( this->hBitmap != NULL ) {
DeleteObject( this->hBitmap );
}
if ( this->pDib != NULL ) {
HeapFree(
this->hHeap,
HEAP_NO_SERIALIZE,
this->pDib
);
this->pDib = NULL;
}
this->hBitmap = hCur;
GetObject(
this->hBitmap,
sizeof(BITMAP),
&(this->Info)
);
if ( this->hWnd != NULL ) {
RedrawWindow(
this->hWnd,
NULL,
NULL,
RDW_INVALIDATE | RDW_ERASENOW
);
}
return true;
}
| |
|
|
|
|
// *********************************************************
// 16 ビットカラー以上の DIB を取得します
// 戻り値 : true 成功, flase 失敗
// *********************************************************
BOOL LboxBitmap::GetFromDib( LPVOID lpDib )
{
if ( lpDib == NULL ) {
return false;
}
if ( this->hWnd == NULL ) {
return false;
}
HBITMAP hCur;
LPBITMAPINFOHEADER lpBih;
lpBih = (LPBITMAPINFOHEADER)lpDib;
if ( lpBih->biBitCount < 16 ) {
return false;
}
HDC hDc;
hDc = GetDC( this->hWnd );
hCur = CreateDIBitmap(
hDc,
lpBih,
(DWORD)CBM_INIT,
(LPTSTR)lpBih + lpBih->biSize,
(LPBITMAPINFO)lpBih,
DIB_RGB_COLORS
);
if ( hCur == NULL ) {
ReleaseDC( this->hWnd, hDc );
return false;
}
ReleaseDC( this->hWnd, hDc );
if ( this->hBitmap != NULL ) {
DeleteObject( this->hBitmap );
}
if ( this->pDib != NULL ) {
HeapFree(
this->hHeap,
HEAP_NO_SERIALIZE,
this->pDib
);
this->pDib = NULL;
}
this->hBitmap = hCur;
GetObject(
this->hBitmap,
sizeof(BITMAP),
&(this->Info)
);
RedrawWindow(
this->hWnd,
NULL,
NULL,
RDW_INVALIDATE | RDW_ERASENOW
);
return true;
}
| |
|
|
|
|
#define WIDTHBYTES(i) ((i+31)/32*4)
// *********************************************************
// ビットマップのハンドルより、DIB を作成
// 戻り値 : true 成功, flase 失敗
// *********************************************************
BOOL LboxBitmap::DibFromBitmap( void )
{
if ( this->hBitmap == NULL ) {
return false;
}
if ( this->pDib != NULL ) {
HeapFree(
this->hHeap,
HEAP_NO_SERIALIZE,
this->pDib
);
this->pDib = NULL;
}
HPALETTE hPal;
hPal = (HPALETTE)GetStockObject(DEFAULT_PALETTE);
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = this->Info.bmWidth;
bi.biHeight = this->Info.bmHeight;
bi.biPlanes = 1;
if ( this->wBitCount == 0 ) {
bi.biBitCount = this->Info.bmPlanes * this->Info.bmBitsPixel;
}
else {
bi.biBitCount = this->wBitCount;
}
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
DWORD dwSize;
switch( bi.biBitCount ) {
case 1:
dwSize = 2;
break;
case 4:
dwSize = 16;
break;
case 8:
dwSize = 256;
break;
default:
dwSize = 0;
break;
}
dwSize = dwSize * sizeof(RGBQUAD);
this->dwPaletteSize = dwSize;
dwSize += bi.biSize;
HDC hDc;
hDc = CreateCompatibleDC( NULL );
hPal = SelectPalette( hDc, hPal, false );
RealizePalette( hDc );
this->pDib = HeapAlloc(
this->hHeap,
HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY,
dwSize
);
if ( this->pDib == NULL ) {
SelectPalette( hDc, hPal, false );
DeleteDC( hDc );
return false;
}
CopyMemory( this->pDib, &bi, sizeof( BITMAPINFOHEADER ) );
GetDIBits(
hDc,
this->hBitmap,
0,
(UINT)bi.biHeight,
NULL,
(LPBITMAPINFO)this->pDib,
DIB_RGB_COLORS
);
CopyMemory( &bi, this->pDib, sizeof( BITMAPINFOHEADER ) );
if ( bi.biSizeImage == 0 ) {
bi.biSizeImage =
WIDTHBYTES((DWORD)bi.biWidth * bi.biBitCount) * bi.biHeight;
CopyMemory( this->pDib, &bi, sizeof( BITMAPINFOHEADER ) );
}
this->dwDibSize = dwSize + bi.biSizeImage;
LPVOID lpMem;
lpMem = HeapReAlloc(
this->hHeap,
HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY,
this->pDib,
this->dwDibSize
);
if ( lpMem == NULL ) {
HeapFree(
this->hHeap,
HEAP_NO_SERIALIZE,
this->pDib
);
this->pDib = NULL;
SelectPalette( hDc, hPal, false );
DeleteDC( hDc );
return false;
}
this->pDib = lpMem;
GetDIBits(
hDc,
this->hBitmap,
0,
(UINT)bi.biHeight,
(LPTSTR)(this->pDib) + dwSize,
(LPBITMAPINFO)this->pDib,
DIB_RGB_COLORS
);
SelectPalette( hDc, hPal, false );
DeleteDC( hDc );
return true;
}
| |
|
|
|
|
// *********************************************************
// ビットマップの複製
// 戻り値 : true 成功, flase 失敗
// *********************************************************
HBITMAP LboxBitmap::Duplicate( HBITMAP hBase )
{
if ( hBase == NULL ) {
return NULL;
}
if ( this->hWnd == NULL ) {
return NULL;
}
BITMAP bm;
// 複製元のビットマップ情報
GetObject( hBase, sizeof(BITMAP), &bm );
// 複製先のビットマップハンドル
HBITMAP hDup;
// ウインドウのデバイスコンテキスト
HDC hDc;
// 複製元のハンドル
HDC hDCMemFrom;
HGDIOBJ hOldFrom;
// メモリデバイスコンテキストを作成
hDc = GetDC( this->hWnd );
hDCMemFrom = CreateCompatibleDC( hDc );
ReleaseDC( this->hWnd, hDc );
if ( hDCMemFrom == NULL ) {
return NULL;
}
// メモリデバイスに、複製元ビットマップを選択
hOldFrom = SelectObject( hDCMemFrom, hBase );
if ( hOldFrom == NULL ) {
DeleteDC( hDCMemFrom );
return NULL;
}
// 複製先のハンドル
HDC hDCMemTo;
HGDIOBJ hOldTo;
// メモリデバイスコンテキストと新しいビットマップを作成
hDc = GetDC( this->hWnd );
hDup = CreateCompatibleBitmap(hDc, bm.bmWidth, bm.bmHeight);
hDCMemTo = CreateCompatibleDC( hDc );
ReleaseDC( this->hWnd, hDc );
if ( hDCMemTo == NULL ) {
if ( hDup != NULL ) {
DeleteObject( hDup );
}
SelectObject( hDCMemFrom, hOldFrom );
DeleteDC( hDCMemFrom );
return NULL;
}
// メモリデバイスに、ビットマップを選択
hOldTo = SelectObject( hDCMemTo, hDup );
if ( hOldTo == NULL ) {
if ( hDup != NULL ) {
DeleteObject( hDup );
}
SelectObject( hDCMemFrom, hOldFrom );
DeleteDC( hDCMemFrom );
DeleteDC( hDCMemTo );
return NULL;
}
// 転送
BitBlt(
hDCMemTo,
0,
0,
bm.bmWidth,
bm.bmHeight,
hDCMemFrom,
0,
0,
SRCCOPY
);
// 後始末
SelectObject( hDCMemFrom, hOldFrom );
DeleteDC( hDCMemFrom );
// 目的のビットマップ
hDup = (HBITMAP)SelectObject( hDCMemTo, hOldTo );
DeleteDC( hDCMemTo );
return hDup;
}
| |
|
|
|
ダイアログにピクチャコントロールを貼り付けて使用しています。ID は、IDC_PICTURE です
|
#include "stdafx.h"
#include "App_01.h"
App_01 *CurApp;
static LboxCommdlg *Commdlg; // 共有ダイアログ
static LboxString *FilePath; // ファイルパス用
static LboxBitmap *Bmp;
// *********************************************************
// ダイアログの処理
// *********************************************************
LRESULT CALLBACK Dialog_01(
HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
{
switch( message ) {
// ダイアログの初期処理
case WM_INITDIALOG:
bEnd = false;
// アプリケーションオブジェクト
CurApp = new App_01( hDlg );
// コモンダイアログ
Commdlg = new LboxCommdlg( hDlg );
Commdlg->lpstrFilter = "BMP,*.bmp,全て,*.*";
Commdlg->lpstrDefExt = "bmp";
// 文字列オブジェクト
FilePath = new LboxString( );
// *************************************
// 定義
// *************************************
Bmp = new LboxBitmap();
Bmp->SetTargetWindow( hDlg, IDC_PICTURE );
Bmp->CreateBackBrush( 123, 211, 48 );
// 初期フォーカス
Dlg->EditFocus( IDC_EDIT );
return FALSE;
// *****************************************
// 描画
// *****************************************
case WM_CTLCOLORSTATIC:
Bmp->FitDraw( (HDC)wParam );
break;
case WM_COMMAND:
// 終了処理
if( LOWORD(wParam) == IDCANCEL ) {
// ダイアログの終了処理
bEnd = true;
delete Bmp;
delete FilePath;
delete Commdlg;
delete CurApp;
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
// *************************************
// 保存
// *************************************
if ( LOWORD(wParam) == IDM_SAVEFILE ) {
if ( Commdlg->SaveFileName( FilePath ) ) {
Dlg->EditSetText( IDC_READONLY, FilePath );
// 24ビットカラーで保存
Bmp->wBitCount = 24;
Bmp->SaveToFile( FilePath );
}
}
// *************************************
// 開く
// *************************************
if ( LOWORD(wParam) == IDM_OPENFILE ) {
if ( Commdlg->OpenFileName( FilePath ) ) {
Dlg->EditSetText( IDC_READONLY, FilePath );
Bmp->LoadFromFile( FilePath );
}
}
break;
}
return FALSE;
}
| |
|
|
|