以前看到三原色的图案,一直很好奇是如何画出来。后来终于搞清楚了,其实很简单,实际上就是RGB三个分量的"位与"运算。
下面给出Win32绘制三原色图案的例子,特此记录在此:
#include <windows.h> LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow)
{
static TCHAR szAppName[]=TEXT("RGB_SRCPAINT");
HWND hWnd;
MSG msg;
WNDCLASS wc= {sizeof(WNDCLASS)};
wc.style=CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc=WndProc;
wc.cbClsExtra=;
wc.cbWndExtra=;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName=szAppName;
if(!RegisterClass(&wc)) {
MessageBox(NULL,TEXT("error"),szAppName,MB_ICONERROR|MB_OK);
return ;
}
hWnd=CreateWindow(szAppName,TEXT("RGB"),WS_OVERLAPPEDWINDOW,
,,,,NULL,NULL,hInstance,NULL); //
ShowWindow(hWnd,iCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg,NULL,,)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//
void Circle(HDC dc,int ox,int oy,int r)
{
Ellipse(dc,ox-r,oy-r,ox+r,oy+r);
}
//
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
//
HDC hdc,hMemDC;
HBITMAP hBitmap;
HBRUSH hbrush;
RECT rc= {,,,};
PAINTSTRUCT ps;
int ox=,oy=,r=;
//
switch (message) {
case WM_PAINT :
hdc=BeginPaint(hWnd,&ps);
//MemDC
hMemDC = CreateCompatibleDC(hdc);
hBitmap = CreateCompatibleBitmap(hdc,,);
SelectObject(hMemDC,hBitmap);
//ClearScreen with Black
hbrush = CreateSolidBrush(RGB(, , ));
SelectObject(hMemDC,hbrush);
FillRect(hdc,&rc,hbrush);
//RED
hbrush = CreateSolidBrush(RGB(, , ));
SelectObject(hMemDC,hbrush);
//Ellipse(hMemDC,0+200,0+200+30,100+200,100+200+30);
Circle(hMemDC,ox,oy+0.8*r,r);
BitBlt(hdc,,,,,hMemDC,,,SRCPAINT);
//GREEN
hbrush = CreateSolidBrush(RGB(, , ));
SelectObject(hMemDC,hbrush);
//Ellipse(hMemDC,0+200-26,0+200-15,100+200-26,100+200-15);
Circle(hMemDC,ox-0.866*0.8*r,oy-0.5*0.8*r,r);
BitBlt(hdc,,,,,hMemDC,,,SRCPAINT);
//BLUE
hbrush = CreateSolidBrush(RGB(, , ));
SelectObject(hMemDC,hbrush);
//Ellipse(hMemDC,0+200+26,0+200-15,100+200+26,100+200-15);
Circle(hMemDC,ox+0.866*0.8*r,oy-0.5*0.8*r,r);
BitBlt(hdc,,,,,hMemDC,,,SRCPAINT);
//
DeleteObject(hBitmap);
DeleteDC( hMemDC );
EndPaint (hWnd, &ps) ; break;
case WM_DESTROY :
PostQuitMessage();
break ;
}
return DefWindowProc (hWnd, message, wParam, lParam) ;
}