📄 advbitmap.cpp
字号:
int n = 0;
HDC hMemDC = CreateCompatibleDC(hDC);
for (int resid=3; resid<=100; resid++)
{
HICON hIcon = (HICON) LoadImage(hMod, MAKEINTRESOURCE(resid), IMAGE_ICON, 48, 48, LR_DEFAULTCOLOR);
if ( hIcon )
{
int x = (n%2)*420 + 20;
int y = (n/2)*60 + 20;
DrawIcon(hDC, x, y, hIcon); n++;
MaskBltDrawIcon(hDC, x+56*5, y, hIcon);
TransparentBltDrawIcon(hDC, x+56*6, y, hIcon);
ICONINFO iconinfo;
GetIconInfo(hIcon, & iconinfo);
DestroyIcon(hIcon);
BITMAP bmp;
GetObject(iconinfo.hbmMask, sizeof(bmp), & bmp);
HGDIOBJ hOld = SelectObject(hMemDC, iconinfo.hbmMask);
BitBlt(hDC, x+56, y, bmp.bmWidth, bmp.bmHeight, hMemDC, 0, 0, SRCCOPY);
SelectObject(hMemDC, iconinfo.hbmColor);
BitBlt(hDC, x+56*2, y, bmp.bmWidth, bmp.bmHeight, hMemDC, 0, 0, SRCCOPY);
SelectObject(hMemDC, iconinfo.hbmMask);
BitBlt(hDC, x+56*3, y, bmp.bmWidth, bmp.bmHeight, hMemDC, 0, 0, SRCAND);
SelectObject(hMemDC, iconinfo.hbmColor);
BitBlt(hDC, x+56*3, y, bmp.bmWidth, bmp.bmHeight, hMemDC, 0, 0, SRCINVERT);
MaskBitmapNT(hDC, x+56*4, y, bmp.bmWidth, bmp.bmHeight, iconinfo.hbmMask, hMemDC);
SelectObject(hMemDC, hOld);
DeleteObject(iconinfo.hbmMask);
DeleteObject(iconinfo.hbmColor);
}
}
DeleteObject(hMemDC);
}
void ColorBitmap(HDC hDC, int x, int y, int w, int h, HDC hMemDC, HBRUSH hBrush)
{
// P^(S&(P^D)), if (S) D else P
HGDIOBJ hOldBrush = SelectObject(hDC, hBrush);
BitBlt(hDC, x, y, w, h, hMemDC, 0, 0, 0xB8074A);
SelectObject(hDC, hOldBrush);
}
void TestColoring(HDC hDC, HINSTANCE hInstance)
{
HBITMAP hPttrn;
HBITMAP hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_CONFUSE));
BITMAP bmp;
GetObject(hBitmap, sizeof(bmp), & bmp);
SetTextColor(hDC, RGB(0, 0, 0));
SetBkColor(hDC, RGB(0xFF, 0xFF, 0xFF));
HDC hMemDC = CreateCompatibleDC(NULL);
HGDIOBJ hOld = SelectObject(hMemDC, hBitmap);
for (int i=0; i<5; i++)
{
HBRUSH hBrush;
switch (i)
{
case 0: hBrush = CreateSolidBrush(RGB(0xFF, 0, 0)); break;
case 1: hBrush = CreateSolidBrush(RGB(0, 0xFF, 0)); break;
case 2: hPttrn = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_PATTERN01));
hBrush = CreatePatternBrush(hPttrn);
DeleteObject(hPttrn);
break;
case 3: hBrush = CreateHatchBrush(HS_DIAGCROSS, RGB(0, 0, 0xFF)); break;
case 4: hPttrn = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_WOOD01));
hBrush = CreatePatternBrush(hPttrn);
DeleteObject(hPttrn);
}
ColorBitmap(hDC, i*30+10-2, i*5+10-2, bmp.bmWidth, bmp.bmHeight, hMemDC, (HBRUSH)GetStockObject(WHITE_BRUSH));
ColorBitmap(hDC, i*30+10+2, i*5+10+2, bmp.bmWidth, bmp.bmHeight, hMemDC, (HBRUSH)GetStockObject(DKGRAY_BRUSH));
ColorBitmap(hDC, i*30+10, i*5+10, bmp.bmWidth, bmp.bmHeight, hMemDC, hBrush);
DeleteObject(hBrush);
}
BitBlt(hDC, 240, 25, bmp.bmWidth, bmp.bmHeight, hMemDC, 0, 0, SRCCOPY);
SelectObject(hMemDC, hOld);
DeleteObject(hBitmap);
DeleteObject(hMemDC);
}
void TestLoadImage(HDC hDC, HINSTANCE hInstance)
{
HBITMAP hBitmap[3];
const nID [] = { IDB_MOSQUIT1, IDB_MOSQUIT2, IDB_MOSQUIT3 };
for (int i=0; i<3; i++)
hBitmap[i] = (HBITMAP) LoadImage(hInstance, MAKEINTRESOURCE(nID[i]), IMAGE_BITMAP, 0, 0,
LR_LOADTRANSPARENT | LR_CREATEDIBSECTION );
BITMAP bmp;
GetObject(hBitmap[0], sizeof(bmp), & bmp);
HDC hMemDC = CreateCompatibleDC(hDC);
SelectObject(hDC, GetSysColorBrush(COLOR_WINDOW));
int lastx = -1;
int lasty = -1;
HRGN hRgn = CreateRectRgn(0, 0, 0, 0);
for (i=0; i<600; i++)
{
SelectObject(hMemDC, hBitmap[i%3]);
int newx = i;
int newy = abs(200-i%400);
if ( lastx!=-1 )
{
SetRectRgn(hRgn, newx, newy, newx+bmp.bmWidth, newy + bmp.bmHeight);
ExtSelectClipRgn(hDC, hRgn, RGN_DIFF);
PatBlt(hDC, lastx, lasty, bmp.bmWidth, bmp.bmHeight, PATCOPY);
SelectClipRgn(hDC, NULL);
}
BitBlt(hDC, newx, newy, bmp.bmWidth, bmp.bmHeight, hMemDC, 0, 0, SRCCOPY);
lastx = newx; lasty = newy;
}
DeleteObject(hRgn);
DeleteObject(hMemDC);
DeleteObject(hBitmap[0]);
DeleteObject(hBitmap[1]);
DeleteObject(hBitmap[2]);
}
void TestAlphaBlending(HDC hDC, HINSTANCE hInstance)
{
const int size = 100;
// Solid block constant alpha blending
for (int i=0; i<3; i++)
{
RECT rect = { i*(size+10) + 20, 20+size/3, i*(size+10) + 20 + size, 20+size/3 + size };
const COLORREF Color[] = { RGB(0xFF, 0, 0), RGB(0, 0xFF, 0), RGB(0, 0, 0xFF) };
HBRUSH hBrush = CreateSolidBrush(Color[i]);
FillRect(hDC, & rect, hBrush); // three original solid rectangle
DeleteObject(hBrush);
BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255/2, 0 }; // constant alpha 0.5
AlphaBlend(hDC, 360+((3-i)%3)*size/3, 20+i*size/3, size, size, // blend destination
hDC, i*(size+10)+20, 20+size/3, size, size, blend); // original solid rectangle
}
// HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 1));
// RECT rect = { 100, 100, 500, 200 };
// FillRect(hDC, & rect, hBrush);
// DeleteObject(hDC);
}
void Label(HDC hDC, int x, int y, const TCHAR * mess)
{
TextOut(hDC, x, y, mess, _tcslen(mess));
}
void TestPalette(HDC hDC, HINSTANCE hInstance)
{
TCHAR temp[64];
if ( GetDeviceCaps(hDC, RASTERCAPS ) && RC_PALETTE )
Label(hDC, 10, 10, "Palette Supported");
else
Label(hDC, 10, 10, "Palette not supported");
wsprintf(temp, "SIZEPALETTE %d", GetDeviceCaps(hDC, SIZEPALETTE));
Label(hDC, 10, 30, temp);
wsprintf(temp, "NUMRESERVED %d", GetDeviceCaps(hDC, NUMRESERVED));
Label(hDC, 10, 50, temp);
wsprintf(temp, "COLORRES %d", GetDeviceCaps(hDC, COLORRES));
Label(hDC, 10, 70, temp);
PALETTEENTRY entries[256];
int no = GetSystemPaletteEntries(hDC, 0, 256, entries);
for (int i=0; i<no; i++)
{
wsprintf(temp, "%02x %02x %02x %02x", entries[i].peRed, entries[i].peGreen, entries[i].peBlue, entries[i].peFlags);
Label(hDC, 10 + ( i % 8) * 100, 100 + (i/8) * 20, temp);
}
}
class KTestView : public KScrollCanvas
{
KAirBrush brush;
void AirBrush(HWND hWnd, int x, int y)
{
HDC hDC = GetDC(hWnd);
brush.Apply(hDC, x, y);
ReleaseDC(hWnd, hDC);
}
virtual void OnDraw(HDC hDC, const RECT * rcPaint)
{
switch ( m_nViewOpt )
{
case IDM_VIEW_ROPCHART:
TestRop3(m_hInst, hDC);
break;
case IDM_VIEW_ICON:
TestIcons(hDC);
break;
case IDM_VIEW_COLORBITMAP:
TestColoring(hDC, m_hInst);
break;
case IDM_VIEW_LOADIMAGE:
TestLoadImage(hDC, m_hInst);
break;
case IDM_VIEW_ALPHABLEND:
TestAlphaBlending(hDC, m_hInst);
break;
case IDM_VIEW_PALETTE:
TestPalette(hDC, m_hInst);
}
}
virtual LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch( uMsg )
{
case WM_CREATE:
m_hWnd = hWnd;
m_hViewMenu = LoadMenu(m_hInst, MAKEINTRESOURCE(IDR_DIBVIEW));
SetSize(1200, 1200, 5, 5, true);
return 0;
case WM_LBUTTONDOWN:
AirBrush(hWnd, LOWORD(lParam), HIWORD(lParam));
return 0;
case WM_MOUSEMOVE:
if ( wParam & MK_LBUTTON )
AirBrush(hWnd, LOWORD(lParam), HIWORD(lParam));
else
{
HDC hDC = GetDC(hWnd);
COLORREF cr = GetPixel(hDC, LOWORD(lParam), HIWORD(lParam));
TCHAR mess[32];
wsprintf(mess, "RGB(%02x, %02x, %02x)", GetRValue(cr), GetGValue(cr), GetBValue(cr));
m_pStatus->SetText(1, mess);
ReleaseDC(hWnd, hDC);
}
return 0;
case WM_PAINT:
case WM_SIZE:
case WM_HSCROLL:
case WM_VSCROLL:
return KScrollCanvas::WndProc(hWnd, uMsg, wParam, lParam);
case WM_COMMAND:
switch ( LOWORD(wParam) )
{
case IDM_VIEW_ROPCHART:
case IDM_VIEW_ICON:
case IDM_VIEW_COLORBITMAP:
case IDM_VIEW_LOADIMAGE:
case IDM_VIEW_ALPHABLEND:
case IDM_VIEW_PALETTE:
if ( LOWORD(wParam)!= m_nViewOpt )
{
m_nViewOpt = LOWORD(wParam);
SetSize(700, 800, 5, 5, true);
InvalidateRect(hWnd, NULL, TRUE);
}
return 0;
}
return 0;
default:
return CommonMDIChildProc(hWnd, uMsg, wParam, lParam, m_hViewMenu, 3);
}
}
void GetWndClassEx(WNDCLASSEX & wc)
{
memset(& wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = NULL;
wc.hIcon = LoadIcon(m_hInst, MAKEINTRESOURCE(IDI_IMAGE));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = NULL;
wc.hIconSm = NULL;
}
HMENU m_hViewMenu;
int m_nViewOpt;
public:
KTestView(void)
{
m_hViewMenu = NULL;
m_nViewOpt = IDM_VIEW_ROPCHART;
}
bool Initialize(HINSTANCE hInstance, KStatusWindow * pStatus)
{
m_hInst = hInstance;
m_pStatus = pStatus;
RegisterClass(_T("TestView"), hInstance);
brush.Create(32, 32, RGB(0xC0, 0x80, 0));
return true;
}
};
const int Translate[] =
{
IDM_FILE_CLOSE,
IDM_FILE_EXIT,
IDM_WINDOW_TILE,
IDM_WINDOW_CASCADE,
IDM_WINDOW_ARRANGE,
IDM_WINDOW_CLOSEALL
};
class KMyMDIFRame : public KMDIFrame
{
int m_nCount;
void CreateMDIChild(KScrollCanvas * canvas, const TCHAR * klass, const TCHAR * filename, const TCHAR * title)
{
MDICREATESTRUCT mdic;
TCHAR Temp[MAX_PATH];
if ( ! canvas->GetTitle(filename, Temp) )
{
m_nCount ++;
wsprintf(Temp, title, m_nCount);
}
mdic.szClass = klass;
mdic.szTitle = Temp;
mdic.hOwner = m_hInst;
mdic.x = CW_USEDEFAULT;
mdic.y = CW_USEDEFAULT;
mdic.cx = CW_USEDEFAULT;
mdic.cy = CW_USEDEFAULT;
mdic.style = WS_VISIBLE | WS_BORDER;
mdic.lParam = (LPARAM) canvas;
SendMessage(m_hMDIClient, WM_MDICREATE, 0, (LPARAM) & mdic);
}
void CreateDIBView(const TCHAR * pFileName)
{
KDIBView * pDIBView = new KDIBView();
if ( pDIBView )
if ( pDIBView->Initialize(pFileName, m_hInst, m_pStatus, m_hWnd) )
CreateMDIChild(pDIBView, _T("DIBView"), pFileName, _T("DIB %d"));
else
delete pDIBView;
}
void CreateDIBView(BITMAPINFO * pDIB, const TCHAR * pTitle)
{
KDIBView * pDIBView = new KDIBView();
if ( pDIBView )
if ( pDIBView->Initialize(pDIB, m_hInst, m_pStatus, m_hWnd) )
CreateMDIChild(pDIBView, _T("DIBView"), pTitle, _T("DIB %d"));
else
delete pDIBView;
}
void CreateTestView(void)
{
KTestView * pTestView = new KTestView();
if ( pTestView )
if ( pTestView->Initialize(m_hInst, m_pStatus) )
CreateMDIChild(pTestView, _T("TestView"), NULL, _T("Test %d"));
else
delete pTestView;
}
virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam)
{
switch ( LOWORD(wParam) )
{
case IDM_FILE_NEW:
CreateDIBView(NULL);
return TRUE;
case IDM_FILE_TEST:
CreateTestView();
return TRUE;
case IDM_FILE_OPEN:
{
KFileDialog fo;
if ( fo.GetOpenFileName(m_hWnd, "bmp", "Bitmap Files") )
CreateDIBView(fo.m_TitleName);
return TRUE;
}
}
return FALSE;
}
virtual LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT lr = KMDIFrame::WndProc(hWnd, uMsg, wParam, lParam);
if ( uMsg == WM_USER )
{
CreateDIBView((BITMAPINFO *) wParam, (const TCHAR *) lParam);
}
// if ( uMsg == WM_CREATE )
// {
// SetWindowLong(m_hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
// SetLayeredWindowAttributes(m_hWnd, 0, 0xC0, LWA_ALPHA);
// }
return lr;
}
void GetWndClassEx(WNDCLASSEX & wc)
{
KMDIFrame::GetWndClassEx(wc);
wc.hIcon = LoadIcon(m_hInst, MAKEINTRESOURCE(IDI_IMAGE));
}
public:
KMyMDIFRame(HINSTANCE hInstance, const TBBUTTON * pButtons, int nCount,
KToolbar * pToolbar, KStatusWindow * pStatus) :
KMDIFrame(hInstance, pButtons, nCount, pToolbar, pStatus, Translate)
{
m_nCount = 0;
}
};
const TBBUTTON tbButtons[] =
{
{ STD_FILENEW, IDM_FILE_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, { 0, 0 }, IDS_FILENEW, 0 },
{ STD_FILEOPEN, IDM_FILE_OPEN, TBSTATE_ENABLED, TBSTYLE_BUTTON, { 0, 0 }, IDS_FILEOPEN, 0 }
};
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int nShow)
{
KToolbar toolbar;
KStatusWindow status;
KMyMDIFRame frame(hInst, tbButtons, 2, & toolbar, & status);
TCHAR title[MAX_PATH];
_tcscpy(title, "AdvBitmap");
frame.CreateEx(0, _T("ClassName"), title,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, LoadMenu(hInst, MAKEINTRESOURCE(IDR_MAIN)), hInst);
frame.ShowWindow(nShow);
frame.UpdateWindow();
frame.MessageLoop();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -