16,466
社区成员
发帖
与我相关
我的任务
分享
// MyPal.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#define PALETTESIZE 256 /* Number of entries in the system palette */
typedef struct tagRGPT
{ /* Values for setting Min Max info */
INT iInfo[10];
} RGPT, *LPRGPT;
// Foward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HANDLE hPal; /* Handle to the application's logical palette */
static INT nSizeX; /* Width of the application window */
static INT nSizeY; /* Height of the application window */
PLOGPALETTE pLogPal; /* Pointer to program's logical palette */
INT nXBorder; /* Width of window border */
INT nXTitle; /* Width of title bar */
INT nYTitle; /* Height of title bar */
BOOL bCaptureOn; /* Indicates if mouse capture is on */
INT iIndex; /* Last index selected in palette */
CHAR szTitlebuf[90];/* Buffer for pixel and palette info. text */
HDC hDCGlobal; /* The Screen DC */
INT iNumColors; /* Number of colors supported by device */
INT iRasterCaps; /* Raster capabilities */
RECT rClientRect; /* Client rectangle coordinates */
DWORD dwPal[PALETTESIZE]; /* Stores palette entries for later lookup */
INT iGlobalXOffset;
INT iGlobalYOffset;
INT iYMiddle;
VOID ShowColor (HWND hWnd,HDC hDC)
{
HBRUSH hBrush, hOldBrush;
hBrush = CreateSolidBrush ( PALETTEINDEX(iIndex) );
hOldBrush = (HBRUSH)SelectObject (hDC,hBrush) ;
GetWindowRect (hWnd, (LPRECT)&rClientRect);
PatBlt ( hDC, rClientRect.left + nXTitle + nXBorder + 1, rClientRect.top + nXBorder,
nXTitle, nYTitle,PATCOPY);
PatBlt(hDC, rClientRect.right - ( 3 * nXTitle + nXBorder + 2),
rClientRect.top + nXBorder,nXTitle,nYTitle,PATCOPY);
SelectObject (hDC, hOldBrush);
DeleteObject (hBrush) ;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
static CHAR szAppName [] = "MyPal";
HWND hWnd;
WNDCLASS wndclass;
MSG msg ;
INT xScreen;
INT yScreen;
UNREFERENCED_PARAMETER( lpCmdLine );
if (!hPrevInstance)
{
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = (WNDPROC) WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, szAppName);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject (BLACK_BRUSH);
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName;
if (!RegisterClass (&wndclass))
return FALSE ;
}
/* Do some global initializations */
xScreen = GetSystemMetrics (SM_CXSCREEN);
yScreen = GetSystemMetrics (SM_CYSCREEN);
nXBorder = (INT)GetSystemMetrics (SM_CXFRAME);
nXTitle = (INT)GetSystemMetrics (SM_CXSIZE);
nYTitle = (INT)GetSystemMetrics (SM_CYSIZE);
iIndex = 0;
bCaptureOn = FALSE;
hDCGlobal = GetDC (NULL);//Screen
iRasterCaps = GetDeviceCaps(hDCGlobal, RASTERCAPS);
iRasterCaps = (iRasterCaps & RC_PALETTE) ? TRUE : FALSE;
if (iRasterCaps)
{
iNumColors = GetDeviceCaps(hDCGlobal, SIZEPALETTE);
}
else
{//-1>8bits
iNumColors = GetDeviceCaps( hDCGlobal, NUMCOLORS);
if (iNumColors==-1) iNumColors=256;
}
ReleaseDC (NULL,hDCGlobal);
nSizeX = ((xScreen - 2*nXBorder) / PALETTESIZE) * PALETTESIZE;
/* create the app. window */
hWnd = CreateWindow (szAppName, "My Physical Palette ", WS_OVERLAPPEDWINDOW,
(xScreen-nSizeX) / 2 - nXBorder, yScreen - ( 16 * GetSystemMetrics (SM_CYCAPTION)),
nSizeX + 2 * nXBorder, 4 * GetSystemMetrics (SM_CYCAPTION),NULL, NULL, hInstance,
NULL);
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;
INT iLoop;
INT nStart;
HBRUSH hBrush;
HBRUSH hOldBrush;
POINT pt;
static INT nIncr;
static DWORD dwColor;
static DWORD dwLastColor;
static INT i, x;
switch (message)
{
case WM_DESTROY:/* delete the handle to the logical palette if it has any color entries and quit. */
if (pLogPal->palNumEntries) DeleteObject (hPal);
PostQuitMessage (0) ;
break ;
case WM_CREATE: /* Allocate enough memory for a logical palette with PALETTESIZE entries
* and set the size and version fields of the logical palette structure.*/
pLogPal = (NPLOGPALETTE) LocalAlloc (LMEM_FIXED,(sizeof (LOGPALETTE) + (sizeof (PALETTEENTRY) * (PALETTESIZE))));
if(!pLogPal)
{
MessageBox(hWnd, "<WM_CREATE> Not enough memory for palette.", NULL, MB_OK | MB_ICONHAND);
PostQuitMessage (0) ;
break;
}
pLogPal->palVersion = 0x300;
pLogPal->palNumEntries = PALETTESIZE; /* fill in intensities for all palette entry colors */
for (iLoop = 0; iLoop < PALETTESIZE; iLoop++)
{
//Use the default system color:
pLogPal->palPalEntry[iLoop].peRed = iLoop;
pLogPal->palPalEntry[iLoop].peFlags = PC_EXPLICIT;
//Use a custom color:
// pLogPal->palPalEntry[iLoop].peBlue = iLoop;
// pLogPal->palPalEntry[iLoop].peGreen = 100;//iLoop=grey;
// pLogPal->palPalEntry[iLoop].peRed = 0;//iLoop=grey;
// pLogPal->palPalEntry[iLoop].peFlags = 0;//PC_EXPLICIT;
}
/* create a logical color palette according the information
* in the LOGPALETTE structure. */
hPal = CreatePalette ((LPLOGPALETTE) pLogPal) ;
break;
case WM_GETMINMAXINFO:
((LPRGPT)lParam)->iInfo[6] = nXBorder * 2 + PALETTESIZE;
((LPRGPT)lParam)->iInfo[7] = nXBorder * 2 + nYTitle*3;
return DefWindowProc (hWnd, message, wParam, lParam) ;
break;
case WM_PAINT: /* Divide client width into equal-sized parts, one per palette
* entry, and re-calculate client width so that it will display
* exactly as many vertical bars as there are palette entries. */
GetClientRect(hWnd,(LPRECT) &rClientRect);
nSizeX = (rClientRect.right - rClientRect.left);
nSizeX = (nSizeX/iNumColors) * iNumColors;
nSizeY = rClientRect.bottom - rClientRect.top;
GetWindowRect(hWnd,(LPRECT) &rClientRect);
/* Adjust window width so that it can display exactly
* as many vertical bars( of equal width) as there are palette
* colors. */
SetWindowPos( hWnd,(HWND)NULL, 0, 0, nSizeX + 2*nXBorder, rClientRect.bottom - rClientRect.top,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
hDC = BeginPaint(hWnd, &ps);
/* Select the palette into the window device context and
make the Palette Manager map the logical palette to the
system palette (realize it). */
SelectPalette (hDC,(HPALETTE)hPal, 1);
//could not used !
//n=RealizePalette (hDC);//=0 ??
/* Calculate width of each color bar to be displayed */
nIncr = nSizeX / iNumColors;
/* Paint the individual bars separately on the app. window */
for (nStart = iLoop = 0; iLoop < iNumColors; iLoop++)
{/* Since this app. uses a logical palette, use the
* PALETTEINDEX macro to specify the palette entry
* index instead of using an explicit RGB value. */
hBrush = CreateSolidBrush (PALETTEINDEX (iLoop));
dwPal[iLoop] = GetNearestColor (hDC, PALETTEINDEX (iLoop) );
hOldBrush = (HBRUSH)SelectObject (hDC,hBrush) ;
PatBlt (hDC, nStart, 0, nIncr, nSizeY, PATCOPY);
nStart += nIncr;
SelectObject (hDC, hOldBrush);
DeleteObject (hBrush) ;
}
wsprintf (szTitlebuf, "MyPal Colors= %d", iNumColors);
SetWindowText (hWnd, (LPSTR)szTitlebuf);
EndPaint(hWnd,&ps);
break ;
case WM_MBUTTONDOWN:
//MessageBox(0,"","haha",MB_OK);
ClientToScreen(hWnd, &pt);
/* Get RGB value (color) of pixel under mouse coordinate */
dwColor = GetPixel(hDCGlobal, pt.x, pt.y);
/* If color value already exists in palette lookup table, obtain it's index. */
for (i=0 ; i < iNumColors ; i++)
{
if ( dwColor == dwPal[i] )
{
break;
}
}
iIndex = i;
/* If previous color value was not identical to current one,
* display color boxes on either side of title bar,
* the R, G, B values and palette index of current color. */
if (dwColor != dwLastColor)
{
wsprintf ( szTitlebuf, "MyPal Colors=%d Index=%d R=%3u G=%3u B=%3u",iNumColors,
iIndex,(WORD)(BYTE) GetRValue (dwColor),(WORD)(BYTE) GetGValue (dwColor),
(WORD)(BYTE) GetBValue (dwColor));
SetWindowText (hWnd, (LPSTR)szTitlebuf);
ShowColor (hWnd, hDCGlobal);
dwLastColor = dwColor;
}
break;
...
}
return 0L ;
}