高級GDI圖形編程(2)

1.使用畫刷
(1)HBRUSH brush_1 = NULL;
brush_1 = GetStockObject(LTGRAY_BRUSH);

(2)創建純色畫刷
函數原型:HBRUSH CreateSolidBrush(COLORREF crColor);//brush color

(3)將畫刷選定到圖象設備描述表中
HBRUSH old_brush = NULL;
old_brush = SelectObject(hdc,green_brush);

(4)刪除畫刷
DeleteObject(green_brush);

(5)創建一個帶陰影線的畫刷:
函數原型HBRUSH CreateHatchBrush(int fnStyle,
                                COLORREF clrref)


2直接繪製點
(1)函數原型
COLORREF SetPixel(HDC hdc,
                  int x,
                  int y,
                  COLORREF crColor);
該函數在窗口中使用HDC以及(x,y)座標和顏色,然後直接繪製上該象素點,並返回實際繪製的顏色。

(2)在繪製時,請注意應該用GetWindowDc()而不是GetDc,因爲前者僅在用戶區檢索,而後者在整個窗口檢索.
例子:
 int x = rand()%1024;
 int y = rand()%768;
        SetPixel(hdc,x,y,RGB(rand()%255,rand()%255,rand()%255));
        (以上爲一個循環)

3繪製線條
(1)GDi繪製線條一般分爲三個步驟:
a.創建畫筆,並在圖形設備描述表中選定畫筆。所有線條都將使用該畫筆來繪製
b.設定該線條的初試位置。
c.從起使位置到終點位置繪製線條(該終點爲下一段線條的初試位置)
d.如果想繪製更多的線條,重複步驟3

(2)設定初試位置的函數
a.原型:BOOL MoveToEx(HDC hdc,
                     int x,
                     int y,
                     LPPOINT lpPoint);
b.MoveToEx(hdc,10,10,NULL);
c.假如要保存最後一個位置:
  POINT last_pos;
  MoveToEx(hdc,10,10,&last_pos);

(3)一旦設定了線條的初試位置,可以調用LineTo()函數來繪製一端線條:
BOOl LineTo(HDC hdc,
            int xEnd,
            int yEnd);

4繪製矩形
(1)函數原型:BOOL Rectangle(HDC hdc,
                        int nLeftRect,
                        int nTopRect,
                        int nRightRect,
                        int nBottomRect)
注意:你要注意一個非常重要的細節。傳遞到Rectangle()函數的座標是該矩形的邊界框。這就意味着如果線條樣式爲NULL的話,將會得到一個實心的矩形,而沒有四個邊界。

(2)還有兩個其他的繪製矩形更專用的函數:FillRect和FrameRect具體查閱MSDN

(3)例子:hdc = GetDC(hwnd);

    RECT rect; // used to hold rect info

    // create a random rectangle
    rect.left   = rand()%WINDOW_WIDTH;
    rect.top    = rand()%WINDOW_HEIGHT;
    rect.right  = rand()%WINDOW_WIDTH;
    rect.bottom = rand()%WINDOW_HEIGHT;

    // create a random brush
    HBRUSH hbrush = CreateSolidBrush(RGB(rand()%256,rand()%256,rand()%256));

    // draw either a filled rect or a wireframe rect
    if ((rand()%2)==1)
        FillRect(hdc,&rect,hbrush);
    else
        FrameRect(hdc,&rect,hbrush);

    // now delete the brush
    DeleteObject(hbrush);
   
    // release the device context
    ReleaseDC(hwnd,hdc);

     // main game processing goes here
    if (KEYDOWN(VK_ESCAPE))
       SendMessage(hwnd, WM_CLOSE, 0,0);
      
 } // end while

// return to Windows like this
return(msg.wParam);    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章