(1)绘制点

利用GDI绘制点的时候不需要画笔和画刷。利用函数SetPixel。

The SetPixel function sets the pixel at the specified coordinates to the specified color.

COLORREF SetPixel(_In_  HDC hdc,_In_  int X,_In_  int Y,_In_  COLORREF crColor
);

Parameters
hdc [in]  A handle to the device context.
X [in]  The x-coordinate, in logical units, of the point to be set. Y [in]   The y-coordinate, in logical units, of the point to be set. crColor [in]   The color to be used to paint the point. To create a COLORREF color value, use the RGB macro.Return valueIf the function succeeds, the return value is the RGB value that the function sets the pixel to. This value may differ from the color specified by crColor; that occurs when an exact match for the specified color cannot be found.

If the function fails, the return value is -1.

例子:初始化窗口为400 x 400, 我们在200 x 200的客户区域产生随机的位置和颜色。程序如下:

// INCLUDES ///
#define WIN32_LEAN_AND_MEAN  // just say no to MFC#include <windows.h>   // include all the windows headers
#include <windowsx.h>  // include useful macros
#include <mmsystem.h>  // very important and include WINMM.LIB too!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>// DEFINES // defines for windows
#define WINDOW_CLASS_NAME "WINCLASS1"#define WINDOW_WIDTH  400
#define WINDOW_HEIGHT 400// MACROS /#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)// GLOBALS
HWND      main_window_handle = NULL; // globally track main window

HINSTANCE hinstance_app      = NULL; // globally track hinstance
char buffer[80];                     // general printing buffer// FUNCTIONS //
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT        ps;        // used in WM_PAINT
HDC                hdc;    // handle to a device context
char buffer[80];        // used to print strings// what is the message
switch(msg){    case WM_CREATE: {// do initialization stuff here// return successreturn(0);} break;case WM_PAINT: {// simply validate the window hdc = BeginPaint(hwnd,&ps);     // end paintingEndPaint(hwnd,&ps);// return successreturn(0);} break;case WM_DESTROY: {// kill the application, this sends a WM_QUIT message PostQuitMessage(0);// return successreturn(0);} break;default:break;} // end switch// process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));} // end WinProc// WINMAIN
int WINAPI WinMain(    HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lpcmdline,int ncmdshow)
{WNDCLASSEX winclass; // this will hold the class we create
HWND       hwnd;     // generic window handle
MSG           msg;         // generic message
HDC        hdc;      // graphics device context// first fill in the window class stucture
winclass.cbSize         = sizeof(WNDCLASSEX);
winclass.style            = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc    = WindowProc;
winclass.cbClsExtra        = 0;
winclass.cbWndExtra        = 0;
winclass.hInstance        = hinstance;
winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName    = NULL;
winclass.lpszClassName    = WINDOW_CLASS_NAME;
winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);// save hinstance in global
hinstance_app = hinstance;// register the window class
if (!RegisterClassEx(&winclass))return(0);// create the window
if (!(hwnd = CreateWindowEx(NULL,                // extended styleWINDOW_CLASS_NAME,   // class"Pixel Plotting Demo", // titleWS_OVERLAPPEDWINDOW | WS_VISIBLE,0,0,      // initial x,yWINDOW_WIDTH, // initial widthWINDOW_HEIGHT,// initial heightNULL,      // handle to parent NULL,      // handle to menuhinstance,// instance of this applicationNULL)))    // extra creation parms
return(0);// save main window handle
main_window_handle = hwnd;// enter main event loop, but this time we use PeekMessage()
// instead of GetMessage() to retrieve messages
while(TRUE){// test if there is a message in queue, if so get itif (PeekMessage(&msg,NULL,0,0,PM_REMOVE)){ // test if this is a quitif (msg.message == WM_QUIT)break;// translate any accelerator keysTranslateMessage(&msg);// send the message to the window procDispatchMessage(&msg);} // end if// draw some pixels each cycle// get the dc for the windowhdc = GetDC(hwnd);// draw 1000 pixelsfor (int index=0; index < 2000; index++){// get random positionint x = rand()%200;int y = rand()%200;COLORREF color = RGB(rand()%255,rand()%255,rand()%255);SetPixel(hdc, x,y, color);} // end for index// release the dc
      ReleaseDC(hwnd, hdc);// main game processing goes hereif (KEYDOWN(VK_ESCAPE))SendMessage(hwnd, WM_CLOSE, 0,0);} // end while// return to Windows like this
return(msg.wParam);} // end WinMain///

所绘制的结果如下,注意代码中400 x 400 是指整个窗口的大小,包括标题区和边框。200 x 200的区域是从左上角的窗口客户区开始计算的。

]

转载于:https://www.cnblogs.com/bruce81/archive/2013/02/17/2914585.html

Windows编程-基本图元(1)相关推荐

  1. 如何使对话框中接收到WM_CHAR消息(Windows编程)

    我们大家都知道,对话框是有的时候捕获不到WM_CHAR消息的.比如,你想使对话框里的Edit控件所键入的全部变为大写.我们毫不犹豫的写到:    #include //Declare the Dial ...

  2. 使用c++进行Windows编程中各种操作文件的方法 【转】

    https://www.zhaokeli.com/article/8232.html 使用c++进行Windows编程中各种操作文件的方法 [转] 来源:赵克立博客 分类: C/C++ 标签:--发布 ...

  3. 【Windows编程】系列第三篇:文本字符输出

    2019独角兽企业重金招聘Python工程师标准>>> 上一篇我们展示了如何使用Windows SDK创建基本控件,本篇来讨论如何输出文本字符. 在使用Win32编程时,我们常常要输 ...

  4. Windows编程一日一练(1)

    首先,编程和旅游是我个人人生两大爱好,所以,每天空闲的时候我喜欢敲敲代码,或者看看别人写的程序,实在是很惬意.今天想看看window编程,争取坚持每日一练.        windows程序主要是由微 ...

  5. Windows 编程

    Delphi 利用Object Pascal 和可视控件库(VCL)对底层的Windows API 进行了完美的封装,所以很少需要使用基础Pascal 语言来建立Windows应用程序,也无需直接调用 ...

  6. 一个物理学家学习Windows编程的感受

    最近学习用Visual C++ 作为平台的Windows 编程,感觉到学习这种编程技术的过程与学习物理的思维差异非常大.最明显的,比如对于物理课本,在学习的时候值得一个字一个词的认真去读,有时候需要读 ...

  7. C++,C++编程,Windows编程,MFC

    编程 我们日常生活中接触到的电子类产品中的应用都是由编程而来 为什么编程,偷懒 我们通过编程驱使(指挥,命令)的是电信号 为什么上面说编程是偷懒,电的发现,给人们带来了便利,人们在各个方面驱使(换成& ...

  8. Windows编程初步(一)

    第一节:安装帮助文档 使用C语言进行Windows开发,必然使用到WindowsSDK.Windows SDK指Windows开发人员工具包,是为使用C语言开发Windows程序所提供的工具集.里面提 ...

  9. 学好Windows编程要看的书籍

    本文是接着前面的学好C++要阅读的书籍而写的,如果想了解C++的学习的话请先看学好C++要阅读的书籍:http://blog.csdn.net/a809146548/article/details/4 ...

最新文章

  1. 微信小程序navigateTo /redirectTo/navigateBack 三者区别
  2. 供应商管理(Supplier Management)
  3. linux配置静态ip
  4. mysql 怎么实现组内排名_MySQL 实现排名(分组排名)
  5. android自定义水波纹,android 自定义view-水波纹进度球
  6. unity模型任意无限切割插件
  7. 接口测试如何在json中引用mock变量
  8. 老船履带工具使用方法_PS中各个工具的使用方法与技巧
  9. 猜数游戏--MOOC中习题
  10. 通过计算机和网络进行整个商务贸易活动,电子商务概论
  11. PPT无法插入视频,验证编码解码器
  12. 12V升压100V,12V升压200V,300V电源转换升压模块
  13. 请告诉孩子:努力读书,是为将来拥有更多选择的权利,而不是被迫谋生
  14. Graphene(石墨烯)区块传播技术能够实现10倍的更高效率
  15. 冰蝎加密 WebShell 过杀软
  16. 本周大新闻|华为发布BB观影眼镜,Geenee AR试穿加入AI生成玩法
  17. 江苏2021高考成绩查询全省排名,2021江苏高考成绩排名顺序查询,江苏省高考全省排名怎么查...
  18. 期末入门题库-C#实现
  19. WebVR简介和常用资源链接
  20. Python学习起来难不难?

热门文章

  1. java 线程重入,java synchronized加载加锁-线程可重入详解及实例代码
  2. tomcat 占用 dos
  3. excel删除重复数据保留一条_Excel一键删除重复数据,你居然还用逐条排查?
  4. python单元测试_python单元测试
  5. python中编完类后到实例编写_Python 和 JavaScript 的区别是什么?
  6. js结束当前循环关键词_干货||什么是事件循环机制
  7. sqlserver Split 开放写法有兴趣的学习一下
  8. adb模拟按键home_ADB——模拟手机按键输入
  9. R中因子分析的得分计算
  10. 关联规则挖掘算法综述