應用dc畫筆劃矩形、直線與橢圓示例。本站提示廣大學習愛好者:(應用dc畫筆劃矩形、直線與橢圓示例)文章只能為提供參考,不一定能成為您想要的結果。以下是應用dc畫筆劃矩形、直線與橢圓示例正文
WindowDraw.cpp
/*簡略的應用dc畫筆劃出直線,矩形,橢圓*/
#include<Windows.h>
#include<tchar.h>
//聲明窗口函數
LRESULT CALLBACK WindowProc (
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
//進口函數WinMain
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
//界說窗口類
WNDCLASS wndclass;
wndclass.lpfnWndProc=WindowProc;//指定窗口函數
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpszClassName=_T("我的窗體");
wndclass.hInstance=hInstance;
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);//獲得尺度鼠標光標
wndclass.hIcon=0;
wndclass.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wndclass.lpszMenuName=0;
//注冊窗口類
if(RegisterClass(&wndclass)==0)
{
MessageBox(0,_T("注冊窗口類掉敗"),_T("我的窗體"),MB_OK);
return 0;
}
//創立窗體實列
HWND hWnd = CreateWindow(_T("我的窗體"),_T("窗體畫圖"),WS_OVERLAPPEDWINDOW,100,100,500,400,0,0,hInstance,0);
//顯示和更新窗體
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
//新聞輪回
MSG msg;
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
//界說窗口函數
LRESULT CALLBACK WindowProc (
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_PAINT://呼應畫圖新聞
{
PAINTSTRUCT ps;
//取得DC
HDC hDC = BeginPaint(hwnd,&ps);
//創立實線,寬度為1,白色的筆
HPEN hPen=CreatePen(PS_SOLID,1,RGB(255,0,0));
//將筆選入DC
HPEN hOldPen=(HPEN)SelectObject(hDC,hPen);
//繪制白色的直線
MoveToEx(hDC,10,10,NULL);
LineTo(hDC,90,50);
//創立一個藍色的刷子
HBRUSH hBrush = CreateSolidBrush(RGB(0,0,255));
HBRUSH hOldBrush= (HBRUSH)SelectObject(hDC,hBrush);
//繪制矩形,由於筆沒有換,所以繪制的是白色邊框,藍色區域的矩形
Rectangle(hDC,100,100,200,170);
//繪制橢圓,由於筆和刷子都沒換,所以繪制的是白色的邊框,藍色區域的橢圓
Ellipse(hDC,200,230,260,300);
//輸入文字
TextOut(hDC,200,30,_T("畫圖測試"),4);
//恢復畫圖對象
SelectObject(hDC,hOldPen);
SelectObject(hDC,hOldBrush);
//刪除畫圖對象
DeleteObject(hPen);
DeleteObject(hBrush);
//釋放DC
EndPaint(hwnd,&ps);
}
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}