📄 cardwindow.cpp
字号:
//
// CardLib - CardWindow class
//
// Freeware
// Copyright J Brown 2001
//
#include <windows.h>
#include <tchar.h>
#include <stdlib.h>
#include "globals.h"
#include "cardlib.h"
#include "cardbutton.h"
#include "cardregion.h"
#include "cardwindow.h"
#include "cardcolor.h"
extern HPALETTE __holdplacepal;
HPALETTE UseNicePalette(HDC hdc, HPALETTE hPalette)
{
HPALETTE hOld;
hOld = SelectPalette(hdc, hPalette, FALSE);
RealizePalette(hdc);
return hOld;
}
void RestorePalette(HDC hdc, HPALETTE hOldPal)
{
SelectPalette(hdc, hOldPal, TRUE);
}
HPALETTE MakePaletteFromCols(COLORREF cols[], int nNumColours);
void PaintRect(HDC hdc, RECT *rect, COLORREF colour);
HBITMAP CreateSinkBmp(HDC hdcCompat, HDC hdc, COLORREF col, int width, int height);
void GetSinkCols(COLORREF crBase, COLORREF *fg, COLORREF *bg, COLORREF *sh1, COLORREF *sh2);
void LoadCardBitmaps();
void FreeCardBitmaps();
static TCHAR szCardName[] = _T("CardWnd32");
static bool fRegistered = false;
static LONG uCardBitmapRef = 0;
void RegisterCardWindow()
{
WNDCLASSEX wc;
//Window class for the main application parent window
wc.cbSize = sizeof(wc);
wc.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;
wc.lpfnWndProc = CardWindow::CardWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = sizeof(CardWindow *);
wc.hInstance = GetModuleHandle(0);
wc.hIcon = 0;
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = 0;
wc.lpszMenuName = 0;
wc.lpszClassName = szCardName;
wc.hIconSm = 0;
RegisterClassEx(&wc);
}
CardWindow::CardWindow() : m_hWnd(0)
{
HDC hdc = GetDC(0);
nNumButtons = 0;
nNumCardRegions = 0;
nNumDropZones = 0;
nBackCardIdx = 53;
ResizeWndCallback = 0;
hbmBackImage = 0;
hdcBackImage = 0;
srand((unsigned)GetTickCount());
//All colours (buttons, highlights, decks)
//are calculated off this single base colour
crBackgnd = PALETTERGB(0,80,0);//PALETTERGB(0,64,100);
// If uCardBitmapRef was previously zero, then
// load the card bitmaps
if(1 == InterlockedIncrement(&uCardBitmapRef))
{
LoadCardBitmaps();
__hPalette = CreateCardPalette();
__hdcPlaceHolder = CreateCompatibleDC(hdc);
__holdplacepal = UseNicePalette(__hdcPlaceHolder, __hPalette);
__hbmPlaceHolder = CreateSinkBmp(hdc, __hdcPlaceHolder, crBackgnd, __cardwidth, __cardheight);
}
ReleaseDC(0, hdc);
//register the window class if necessary
if(!fRegistered)
{
fRegistered = true;
RegisterCardWindow();
}
}
BOOL CardWindow::Create(HWND hwndParent, DWORD dwExStyle, DWORD dwStyle, int x, int y, int width, int height)
{
if(m_hWnd)
return FALSE;
//Create the window associated with this object
m_hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, szCardName, 0,
WS_CHILD | WS_VISIBLE,
0,0,100,100,
hwndParent, 0, GetModuleHandle(0), this);
return TRUE;
}
BOOL CardWindow::Destroy()
{
DestroyWindow(m_hWnd);
m_hWnd = 0;
return TRUE;
}
CardWindow::~CardWindow()
{
if(m_hWnd)
DestroyWindow(m_hWnd);
DeleteAll();
if(0 == InterlockedDecrement(&uCardBitmapRef))
{
FreeCardBitmaps();
DeleteObject(__hbmPlaceHolder);
DeleteDC (__hdcPlaceHolder);
RestorePalette(__hdcPlaceHolder, __holdplacepal);
if(__hPalette)
DeleteObject(__hPalette);
}
}
bool CardWindow::DeleteAll()
{
int i;
for(i = 0; i < nNumCardRegions; i++)
{
delete Regions[i];
}
for(i = 0; i < nNumButtons; i++)
{
delete Buttons[i];
}
for(i = 0; i < nNumDropZones; i++)
{
delete dropzone[i];
}
nNumCardRegions = nNumButtons = nNumDropZones = 0;
return true;
}
void CardWindow::SetBackColor(COLORREF cr)
{
crBackgnd = cr;
int i;
//
// Create the exact palette we need to render the buttons/stacks
//
RestorePalette(__hdcPlaceHolder, __holdplacepal);
if(__hPalette)
DeleteObject(__hPalette);
__hPalette = CreateCardPalette();
//
// re-create the place-holder!
HDC hdc = GetDC(m_hWnd);
DeleteObject(__hbmPlaceHolder);
__holdplacepal = UseNicePalette(__hdcPlaceHolder, __hPalette);
__hbmPlaceHolder = CreateSinkBmp(hdc, __hdcPlaceHolder, crBackgnd, __cardwidth, __cardheight);
//SelectObject(__hdcPlaceHolder, __hbmPlaceHolder);
//reset all buttons to same colour
for(i = 0; i < nNumButtons; i++)
{
if(Buttons[i]->GetStyle() & CB_PUSHBUTTON)
{
Buttons[i]->SetBackColor(ColorScaleRGB(crBackgnd, RGB(255,255,255), 0.1));
}
else
{
Buttons[i]->SetBackColor(crBackgnd);
}
}
for(i = 0; i < nNumCardRegions; i++)
{
Regions[i]->SetBackColor(crBackgnd);
}
ReleaseDC(m_hWnd, hdc);
}
COLORREF CardWindow::GetBackColor()
{
return crBackgnd;
}
CardButton* CardWindow::CardButtonFromPoint(int x, int y)
{
CardButton *bptr = 0;
POINT pt;
pt.x = x;
pt.y = y;
//Search BACKWARDS...to reflect the implicit Z-order that
//the button creation provided
for(int i = nNumButtons - 1; i >= 0; i--)
{
bptr = Buttons[i];
if(PtInRect(&bptr->rect, pt) && bptr->fVisible)
return bptr;
}
return 0;
}
CardRegion* CardWindow::CardRegionFromPoint(int x, int y)
{
POINT pt;
pt.x = x;
pt.y = y;
//Search BACKWARDS...to reflect the implicit Z-order that
//the stack creation provided
for(int i = nNumCardRegions - 1; i >= 0; i--)
{
if(Regions[i]->IsPointInStack(x, y))
return Regions[i];
}
return 0;
}
//
// Forward all window messages onto the appropriate
// class instance
//
LRESULT CALLBACK CardWindow::CardWndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
CardWindow *cw = (CardWindow *)GetWindowLong(hwnd, 0);
return cw->WndProc(hwnd, iMsg, wParam, lParam);
}
void CardWindow::Paint(HDC hdc)
{
int i;
RECT rect;
HPALETTE hOldPal;
hOldPal = UseNicePalette(hdc, __hPalette);
//
// Clip the card stacks so that they won't
// get painted over
//
for(i = 0; i < nNumCardRegions; i++)
{
Regions[i]->Clip(hdc);
}
//
// Clip the buttons
//
for(i = 0; i < nNumButtons; i++)
{
Buttons[i]->Clip(hdc);
}
// Now paint the whole screen with background colour,
//
GetClientRect(m_hWnd, &rect);
//PaintRect(hdc, &rect, MAKE_PALETTERGB(crBackgnd));
PaintCardRgn(hdc, 0, 0, rect.right, rect.bottom, 0, 0);
SelectClipRgn(hdc, NULL);
// Don't let cards draw over buttons, so clip buttons again
//
for(i = 0; i < nNumButtons; i++)
{
Buttons[i]->Clip(hdc);
}
// Paint each card stack in turn
//
for(i = 0; i < nNumCardRegions; i++)
{
Regions[i]->Render(hdc);
}
// Paint each button now
//
SelectClipRgn(hdc, NULL);
for(i = 0; i < nNumButtons; i++)
{
Buttons[i]->Redraw();
}
RestorePalette(hdc, hOldPal);
}
LRESULT CALLBACK CardWindow::WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
CREATESTRUCT *cs;
static CardButton *buttonptr = 0;
static CardRegion *stackptr = 0;
int x, y, i;
switch(iMsg)
{
case WM_NCCREATE:
// When we created this window, we passed in the
// pointer to the class object (CardWindow *) in the
// call to CreateWindow.
cs = (CREATESTRUCT *)lParam;
//
// associate this class with the window
//
SetWindowLong(hwnd, 0, (LONG)cs->lpCreateParams);
return 1;
case WM_NCDESTROY:
// Don't delete anything here..
break;
case WM_SIZE:
nWidth = LOWORD(lParam);
nHeight = HIWORD(lParam);
//
// reposition all the stacks and buttons
// in case any of them are centered, right-justified etc
//
for(i = 0; i < nNumCardRegions; i++)
{
Regions[i]->AdjustPosition(nWidth, nHeight);
}
for(i = 0; i < nNumButtons; i++)
{
Buttons[i]->AdjustPosition(nWidth, nHeight);
}
//
// Call the user-defined resize proc AFTER all the stacks
// have been positioned
//
if(ResizeWndCallback)
ResizeWndCallback(nWidth, nHeight);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
Paint(hdc);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -