// DIB.cpp : 定义应用程序的入口点。
//
//
#include "stdafx.h"
#include "DIB.h"
#include"DIBFILE.h"
#include "DIB.h"
#include"DIBFILE.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
static TCHAR szAppName[] = TEXT("Grays2");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
static TCHAR szAppName[] = TEXT("Grays2");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(nullptr, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("Shades of Gray #2"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hInstance, nullptr);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(nullptr, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("Shades of Gray #2"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hInstance, nullptr);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HPALETTE hPalette;
static int cxClient, cyClient;
HBRUSH hBrush;
HDC hdc;
int i;
LOGPALETTE *plp;
PAINTSTRUCT ps;
RECT rect;
switch (message)
{
case WM_CREATE:
plp = (LOGPALETTE *)malloc(sizeof(LOGPALETTE) + 64 * sizeof(PALETTEENTRY));
plp->palVersion = 0x0300;
plp->palNumEntries = 65;
for (i = 0; i < 65; i++)
{
plp->palPalEntry[i].peRed = (BYTE)min(255, 4 * i);
plp->palPalEntry[i].peGreen = (BYTE)min(255, 4 * i);
plp->palPalEntry[i].peBlue = (BYTE)min(255, 4 * i);
plp->palPalEntry[i].peFlags = 0;
}
hPalette = CreatePalette(plp);
free(plp);
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
InvalidateRect(hwnd, NULL, true);//WM_SIZE消息在WM_PAINT之后
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SelectPalette(hdc, hPalette, FALSE);
RealizePalette(hdc);
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HPALETTE hPalette;
static int cxClient, cyClient;
HBRUSH hBrush;
HDC hdc;
int i;
LOGPALETTE *plp;
PAINTSTRUCT ps;
RECT rect;
switch (message)
{
case WM_CREATE:
plp = (LOGPALETTE *)malloc(sizeof(LOGPALETTE) + 64 * sizeof(PALETTEENTRY));
plp->palVersion = 0x0300;
plp->palNumEntries = 65;
for (i = 0; i < 65; i++)
{
plp->palPalEntry[i].peRed = (BYTE)min(255, 4 * i);
plp->palPalEntry[i].peGreen = (BYTE)min(255, 4 * i);
plp->palPalEntry[i].peBlue = (BYTE)min(255, 4 * i);
plp->palPalEntry[i].peFlags = 0;
}
hPalette = CreatePalette(plp);
free(plp);
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
InvalidateRect(hwnd, NULL, true);//WM_SIZE消息在WM_PAINT之后
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SelectPalette(hdc, hPalette, FALSE);
RealizePalette(hdc);
for (i = 0; i < 65; i++)
{
rect.left = i*cxClient / 64;
rect.top = 0;
rect.right = (i + 1)*cxClient / 64;
rect.bottom = cyClient;
{
rect.left = i*cxClient / 64;
rect.top = 0;
rect.right = (i + 1)*cxClient / 64;
rect.bottom = cyClient;
hBrush = CreateSolidBrush(PALETTERGB(min(255, 4 * i), min(255, 4 * i), min(255, 4 * i)));
FillRect(hdc, &rect, hBrush);
DeleteObject(hBrush);
}
EndPaint(hwnd, &ps);
return 0;
case WM_QUERYNEWPALETTE:
if (!hPalette)
{
return FALSE;
}
hdc = GetDC(hwnd);
SelectPalette(hdc, hPalette, FALSE);
RealizePalette(hdc);
InvalidateRect(hwnd, NULL, TRUE);
ReleaseDC(hwnd, hdc);
return TRUE;
case WM_PALETTECHANGED:
if (!hPalette || (HWND)wParam == hwnd)
{
break;
}
hdc = GetDC(hwnd);
SelectPalette(hdc, hPalette, FALSE);
RealizePalette(hdc);
UpdateColors(hdc);
ReleaseDC(hwnd, hdc);
break;
case WM_DESTROY:
DeleteObject(hPalette);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
FillRect(hdc, &rect, hBrush);
DeleteObject(hBrush);
}
EndPaint(hwnd, &ps);
return 0;
case WM_QUERYNEWPALETTE:
if (!hPalette)
{
return FALSE;
}
hdc = GetDC(hwnd);
SelectPalette(hdc, hPalette, FALSE);
RealizePalette(hdc);
InvalidateRect(hwnd, NULL, TRUE);
ReleaseDC(hwnd, hdc);
return TRUE;
case WM_PALETTECHANGED:
if (!hPalette || (HWND)wParam == hwnd)
{
break;
}
hdc = GetDC(hwnd);
SelectPalette(hdc, hPalette, FALSE);
RealizePalette(hdc);
UpdateColors(hdc);
ReleaseDC(hwnd, hdc);
break;
case WM_DESTROY:
DeleteObject(hPalette);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
修改版本在WM_CREATE中获取客户区大小
// DIB.cpp : 定义应用程序的入口点。
//
//
#include "stdafx.h"
#include "DIB.h"
#include"DIBFILE.h"
#include "DIB.h"
#include"DIBFILE.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
static TCHAR szAppName[] = TEXT("Grays2");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
static TCHAR szAppName[] = TEXT("Grays2");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(nullptr, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("Shades of Gray #2"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hInstance, nullptr);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(nullptr, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("Shades of Gray #2"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hInstance, nullptr);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HPALETTE hPalette;
static int cxClient, cyClient;
HBRUSH hBrush;
HDC hdc;
int i;
LOGPALETTE *plp;
PAINTSTRUCT ps;
RECT rect;
switch (message)
{
case WM_CREATE:
plp = (LOGPALETTE *)malloc(sizeof(LOGPALETTE) + 64 * sizeof(PALETTEENTRY));
plp->palVersion = 0x0300;
plp->palNumEntries = 65;
for (i = 0; i < 65; i++)
{
plp->palPalEntry[i].peRed = (BYTE)min(255, 4 * i);
plp->palPalEntry[i].peGreen = (BYTE)min(255, 4 * i);
plp->palPalEntry[i].peBlue = (BYTE)min(255, 4 * i);
plp->palPalEntry[i].peFlags = 0;
}
hPalette = CreatePalette(plp);
free(plp);
RECT cRect;//获取客户区的大小
GetClientRect(hwnd, &cRect);
cxClient = cRect.right - cRect.left;
cyClient = cRect.bottom - cRect.top;
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SelectPalette(hdc, hPalette, FALSE);
RealizePalette(hdc);
for (i = 0; i < 65; i++)
{
rect.left = i*cxClient / 64;
rect.top = 0;
rect.right = (i + 1)*cxClient / 64;
rect.bottom = cyClient;
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HPALETTE hPalette;
static int cxClient, cyClient;
HBRUSH hBrush;
HDC hdc;
int i;
LOGPALETTE *plp;
PAINTSTRUCT ps;
RECT rect;
switch (message)
{
case WM_CREATE:
plp = (LOGPALETTE *)malloc(sizeof(LOGPALETTE) + 64 * sizeof(PALETTEENTRY));
plp->palVersion = 0x0300;
plp->palNumEntries = 65;
for (i = 0; i < 65; i++)
{
plp->palPalEntry[i].peRed = (BYTE)min(255, 4 * i);
plp->palPalEntry[i].peGreen = (BYTE)min(255, 4 * i);
plp->palPalEntry[i].peBlue = (BYTE)min(255, 4 * i);
plp->palPalEntry[i].peFlags = 0;
}
hPalette = CreatePalette(plp);
free(plp);
RECT cRect;//获取客户区的大小
GetClientRect(hwnd, &cRect);
cxClient = cRect.right - cRect.left;
cyClient = cRect.bottom - cRect.top;
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SelectPalette(hdc, hPalette, FALSE);
RealizePalette(hdc);
for (i = 0; i < 65; i++)
{
rect.left = i*cxClient / 64;
rect.top = 0;
rect.right = (i + 1)*cxClient / 64;
rect.bottom = cyClient;
hBrush = CreateSolidBrush(PALETTERGB(min(255, 4 * i), min(255, 4 * i), min(255, 4 * i)));
FillRect(hdc, &rect, hBrush);
DeleteObject(hBrush);
}
EndPaint(hwnd, &ps);
return 0;
case WM_QUERYNEWPALETTE:
if (!hPalette)
{
return FALSE;
}
hdc = GetDC(hwnd);
SelectPalette(hdc, hPalette, FALSE);
RealizePalette(hdc);
InvalidateRect(hwnd, NULL, TRUE);
ReleaseDC(hwnd, hdc);
return TRUE;
case WM_PALETTECHANGED:
if (!hPalette || (HWND)wParam == hwnd)
{
break;
}
hdc = GetDC(hwnd);
SelectPalette(hdc, hPalette, FALSE);
RealizePalette(hdc);
UpdateColors(hdc);
ReleaseDC(hwnd, hdc);
break;
case WM_DESTROY:
DeleteObject(hPalette);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
FillRect(hdc, &rect, hBrush);
DeleteObject(hBrush);
}
EndPaint(hwnd, &ps);
return 0;
case WM_QUERYNEWPALETTE:
if (!hPalette)
{
return FALSE;
}
hdc = GetDC(hwnd);
SelectPalette(hdc, hPalette, FALSE);
RealizePalette(hdc);
InvalidateRect(hwnd, NULL, TRUE);
ReleaseDC(hwnd, hdc);
return TRUE;
case WM_PALETTECHANGED:
if (!hPalette || (HWND)wParam == hwnd)
{
break;
}
hdc = GetDC(hwnd);
SelectPalette(hdc, hPalette, FALSE);
RealizePalette(hdc);
UpdateColors(hdc);
ReleaseDC(hwnd, hdc);
break;
case WM_DESTROY:
DeleteObject(hPalette);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}