绘图模式--SetROP2

来源:互联网 发布:java最简单小游戏程序 编辑:程序博客网 时间:2024/05/17 22:00

设备内容中定义的绘图方式也影响显示器上所画线的外观。设想画这样一条直线,它的色彩由画笔色彩和画线区域原来的色彩共同决定。设想用同一种画笔在白色表面上画出黑线而在黑色表面上画出白线,而且不用知道表面是什么色彩。这样的功能对您有用吗?通过绘图方式的设定,这些都可以实作。

      当Windows使用画笔来画线时,它实际上执行画笔图素与目标位置处原来图素之间的某种位布尔运算。图素间的位布尔运算叫做「位映像运算」,简称为「ROP」。由于画一条直线只涉及两种图素(画笔和目标),因此这种布尔运算又称为「二元位映像运算」,简记为「ROP2」。Windows定义了16种ROP2代码,表示Windows组合画笔图素和目标图素的方式。在内定设备内容中,绘图方式定义为R2_COPYPEN,这意味着Windows只是将画笔图素复制到目标图素,这也是我们通常所熟知的。此外,还有15种ROP2码。在此处就不列出其余的15中ROP2码了,在《Windows程序设计》一书中有详解。

      现在通过程序进行详解解释:

[cpp] view plaincopyprint?
  1. View Code   
  2.  #include <windows.h>  
  3.    
  4.  LRESULT CALLBACK WndProc(HWNDUINTWPARAMLPARAM);  
  5.  int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, PSTR szCmdLine, int iCmdShow)  
  6.  {  
  7.      static TCHAR szAppName[] = TEXT("FillRect");  
  8.      HWND hwnd;  
  9.      MSG msg;  
  10.      WNDCLASS wndclass;  
  11.    
  12.      wndclass.style = CS_HREDRAW | CS_VREDRAW;  
  13.      wndclass.lpfnWndProc = WndProc;  
  14.      wndclass.cbClsExtra = 0;  
  15.      wndclass.cbWndExtra = 0;  
  16.      wndclass.hInstance = hInstance;  
  17.      wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);  
  18.      wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);  
  19.      wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);  
  20.      wndclass.lpszClassName = szAppName;  
  21.      wndclass.lpszMenuName = NULL;  
  22.    
  23.      if (!RegisterClass(&wndclass))  
  24.      {  
  25.          MessageBox(NULL, TEXT("This program requires Windows NT!"),    szAppName, MB_ICONERROR);  
  26.          return 0;  
  27.      }  
  28.    
  29.      hwnd = CreateWindow(szAppName,   
  30.          TEXT("FillRect"),   
  31.          WS_OVERLAPPEDWINDOW,  
  32.          CW_USEDEFAULT,  
  33.          CW_USEDEFAULT,  
  34.          CW_USEDEFAULT,  
  35.          CW_USEDEFAULT,  
  36.          NULL,  
  37.          NULL,  
  38.          hInstance,  
  39.          NULL);  
  40.      ShowWindow(hwnd, iCmdShow);  
  41.      UpdateWindow(hwnd);  
  42.    
  43.      while (GetMessage(&msg, NULL, 0, 0))  
  44.      {  
  45.          TranslateMessage(&msg);  
  46.          DispatchMessage(&msg);  
  47.      }  
  48.      return msg.wParam;  
  49.  }  
  50.    
  51.  LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)  
  52.  {  
  53.      HDC hdc;  
  54.      PAINTSTRUCT ps;  
  55.      HBRUSH hBrush;  
  56.      RECT rectBrush;  
  57.      HPEN hPen;  
  58.    
  59.      static int cxClient, cyClient;  
  60.      switch(iMsg)  
  61.      {  
  62.      case WM_SIZE:  
  63.          cxClient = LOWORD(lParam);  
  64.          cyClient = HIWORD(lParam);  
  65.          return 0;  
  66.      case WM_PAINT:  
  67.          hdc = BeginPaint(hwnd, &ps);  
  68.    
  69.          SetROP2(hdc, R2_MASKPEN);  
  70.    
  71.          // Rectangle  
  72.          hPen = CreatePen(PS_SOLID, 2, RGB(0, 255, 0));  
  73.          SelectObject(hdc, hPen);  
  74.          Rectangle(hdc, cxClient / 2 - 50, cyClient / 2 - 50, cxClient / 2 + 50, cyClient / 2 + 50);  
  75.    
  76.          // Ellipse  
  77.          Ellipse(hdc, cxClient / 2 - 100, cyClient / 2 - 50, cxClient / 2 + 100, cyClient / 2 + 50);  
  78.    
  79.          // FillRect  
  80.          hBrush = CreateSolidBrush(RGB(255, 0, 0));  
  81.          SetRect(&rectBrush, cxClient / 2 - 25, cyClient / 2 - 25, cxClient / 2 + 25, cyClient / 2 + 25);  
  82.          FillRect(hdc, &rectBrush, hBrush);  
  83.          EndPaint(hwnd, &ps);  
  84.          DeleteObject(hPen);  
  85.          return 0;  
  86.      case WM_DESTROY:  
  87.          PostQuitMessage(0);  
  88.          return 0;  
  89.      }  
  90.      return DefWindowProc(hwnd, iMsg, wParam, lParam);  
  91.  }  

在代码中,有SetROP2(hdc, R2_MASKPEN);这样一句,当去掉这句话时运行程序,结果如下:

      此时程序的运行结果中,矩形的边框被椭圆遮挡住,但这并不是我们想要的结果,所以,设置绘图模式,便可以解决这个问题。调用SetROP2(hdc, R2_MASKPEN);便可以完成绘图模式的设置,可以调用GetROP2获取当前设备内容使用的绘图模式。当加上SetROP2(hdc, R2_MASKPEN);之后的运行效果如下:

     好了,全文讲解完毕!!!希望大家对我的博客提出至诚的建议~~~

0 0
原创粉丝点击