#include <windows.h>
#include <strsafe.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
void DrawRectangle(HWND hwnd); int cxClient, cyClient; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
//声明全局数据:类名
static TCHAR szClassName[] = TEXT("MyWindows");
HWND hwnd;
MSG msg; //注册窗口类
WNDCLASS wndclass; wndclass.hInstance = hInstance;
wndclass.lpszClassName = szClassName;
wndclass.cbClsExtra = ;
wndclass.cbWndExtra = ;
wndclass.lpfnWndProc = WndProc;
wndclass.lpszMenuName = NULL;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.style = CS_HREDRAW; if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("this program must run in Windows NT!"), szClassName, MB_ICONERROR);
return ;
} hwnd = CreateWindow(
szClassName,
TEXT("MyFirstPractice"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
); ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd); // while (GetMessage(&msg, NULL, 0, 0)) 阻塞函数 这个NULL是指接收当前窗口,包括非客户区的信息,如果设为hwnd则(指的是当前客户区)处理非客户区时,不会响应去退出程序,所以会在后台存在 ???
// {
// TranslateMessage(&msg);
// DispatchMessage(&msg);
// } // #define PM_NOREMOVE 0x0000
// #define PM_REMOVE 0x0001
// #define PM_NOYIELD 0x0002
// #if(WINVER >= 0x0500)
// #define PM_QS_INPUT (QS_INPUT << 16)
// #define PM_QS_POSTMESSAGE ((QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER) << 16)
// #define PM_QS_PAINT (QS_PAINT << 16)
// #define PM_QS_SENDMESSAGE (QS_SENDMESSAGE << 16) while (TRUE)
{
if (PeekMessage(&msg,NULL,,,PM_REMOVE))//PM_REMOVE接收后删除 PM_NOREMOVE获取后不删除(偷窥)
{
if (msg.message==WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
//rand() //0~MAX_RAND 0-32767
//绘制矩形,用到消息处理中的数据(全局)
DrawRectangle(hwnd);
}
} return msg.wParam;
} LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
// static int cxClient, cyClient; //static是指数据在这个函数中为全局,不是在程序中 switch (message)
{
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect); EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage();
return ;
} return DefWindowProc(hwnd, message, wParam, lParam);
} void DrawRectangle(HWND hwnd)
{
// TCHAR szBuffer[100];
// StringCchPrintf(szBuffer, 100, L"%d %d", cxClient, cyClient);
// MessageBox(NULL, szBuffer, L"Info", NULL);
HBRUSH hBrush;
HDC hdc;
RECT rect; if (cxClient == || cyClient == )
return;
hdc = GetDC(hwnd);
SetRect(&rect, rand() % cxClient, rand() % cyClient, rand() % cxClient, rand() % cyClient);
hBrush = CreateSolidBrush(RGB(rand() % , rand() % , rand() % ));
FillRect(hdc, &rect, hBrush);
DeleteObject(hBrush);
ReleaseDC(hwnd, hdc);
}

学习windows编程 day4 之 绘制随机矩形和peekMessage的更多相关文章

  1. 学习windows编程 day4 之 矩形的操作

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...

  2. 学习windows编程 day4 之 多边矩形填充

    #include <windows.h> #include <math.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT message, ...

  3. 学习windows编程 day4 之 盯裆猫

    写着写着就困了.... 看这些测量数据就算了,是对各种函数的练习 #include <windows.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT ...

  4. 学习windows编程 day4 之视口和窗口

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...

  5. 学习windows编程 day4 之 设置画刷

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...

  6. 学习windows编程 day4 之 自定义映射

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...

  7. 学习windows编程 day4 之 映射模式

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...

  8. 有一定基础的 C++ 学习者该怎样学习 Windows 编程?

    人的心理有个奇异的特性:一项知识一旦学会之后,学习过程中面临的困惑和不解非常快就会忘得干干净净,似乎一切都是自然而然,本来就该这种.因此,关于「怎样入门」这类问题,找顶尖高手来回答,未必能比一个刚入门 ...

  9. 我为什么学习Windows编程

    前一段时间在看TCP/IP,在图书馆里面找了不少的书,其中有几本书还是不错的.比如: <Windows网络与通信程序设计(第二版)> 王艳平著 <WinSock网络编程经络> ...

随机推荐

  1. 分布式版本控制系统Git的安装与使用(作业2)

    (本次作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2103) 分布式版本控制系统Git的安装与使用 一.安装Git b ...

  2. 爬虫时http错误提示

    在爬虫,请求网站的时候,有时候出现域名报错,所出现的代码所对应的意思:

  3. Activiti For Eclipse(Mars)插件配置

    Activiti BPMN 2.0 designer : http://www.activiti.org/designer/update/

  4. JMX configuration for Tomcat

    Window下执行步骤: D:\apache-tomcat-7.0.57\bin\catalina.bat set CATALINA_OPTS=-Dcom.sun.management.jmxremo ...

  5. cxGrid导出Excel货币符号问题

    cxGrid导出到Excel,对于Currency类型总是加上了货币符号,可以修改导出文件设置来去掉: 在cxXLSExport.pas文件中,修改: procedure TcxXLSExportPr ...

  6. node的consoidate的插件统一

    使用consolidate.ejs.的这种形式. let express = require('express'); let app = express(); app.set('views','返回的 ...

  7. Oracle 和 SQLSERVER 重新获取统计信息的方法

    1. Oracle 重新获取统计信息的命令 exec dbms_stats.gather_schema_stats(ownname =>) # 需要修改 ownername options 指定 ...

  8. WebService概述

    一.WebService介绍 什么是WebService? 一言以蔽之:WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 所谓跨编程语言和跨操作平台,就是说服务端程序采用java编 ...

  9. RSS & Server-Sent Events & HTML5 Notification API

    RSS Rich Site Summary https://en.wikipedia.org/wiki/RSS https://www.lifewire.com/what-is-rss-2483592 ...

  10. 一个很初级的错误 Destructor忘记override导致内存泄露

      TxxObj= class    public     Destructor Destroy(); override;!!!此处若无override,将导致内存泄露     end; Destru ...