WM_size在WM_PAINT之后执行

Posted xiaomi-daming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WM_size在WM_PAINT之后执行相关的知识,希望对你有一定的参考价值。

// DIB.cpp : 定义应用程序的入口点。
//
#include "stdafx.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;
 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);
 while (GetMessage(&msg, NULL, 0, 0))
 {
  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);
  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);
}
 

以上是关于WM_size在WM_PAINT之后执行的主要内容,如果未能解决你的问题,请参考以下文章

诊断神秘 WM_PAINT 消息

Invalidate() InvalidateRect() 与 UpdateWindow()

做完棋盘之后却无法完成左键点击棋盘,而切换棋盘BMP图片

调整窗口大小以适应 WM_PAINT 之后的内容

VC SDK中关于WM_PAINT的新手问题

Invalidate()mfc函数使用,摘录自csdn以为网友回答。