win32学习之 --------GDI使用 代码记录
LRESULT QMainFrame::onPaint(WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps;
HDC hDC = ::BeginPaint(m_hWnd, &ps);
RECT rcClient;
GetClientRect(&rcClient);
assert(hDC);
{
/**输出位图
*/
HDC hMewDc = ::CreateCompatibleDC(hDC);//创建一个新的HDC
HBITMAP hBitmap = ::LoadBitmap((HINSTANCE)GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP1));
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMewDc, hBitmap);//画布与HDC关联
assert(hBitmap);
{
BITMAP bm; //有图片的高宽信息
GetObject(hBitmap, sizeof(BITMAP), &bm);
::BitBlt(hDC, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, hMewDc, bm.bmWidth/2, bm.bmHeight/2, SRCCOPY);//把缓存中的画布内容显示出来 倒数第三 第二个参数是指 显示的开始位置
::StretchBlt(hDC, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, hMewDc, 0, 0,bm.bmWidth,bm.bmHeight, SRCCOPY);//能操作图片的放大 与 缩小
}
::SelectObject(hMewDc, (HGDIOBJ)hOldBitmap);
::DeleteObject((HGDIOBJ)hBitmap);
::DeleteDC(hMewDc);
/**画字体
DrawText(hDC,_T("test"), _tcslen(_T("test")),&rcClient,DT_CENTER | DT_VCENTER | DT_WORD_ELLIPSIS | DT_SINGLELINE );
COLORREF clroldTextColor = SetTextColor(hDC,RGB(255,0,0));//设置字段的颜色
COLORREF clrBackColor = SetBkColor(hDC,RGB(0,255,0));//设置字段的背景
HFONT hFont = CreateFont(
15,
0,
0,
0,
500,
FALSE,
FALSE,
0,
ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_SWISS,
_T("arial")
);//创建一个字体
HFONT hOldFont =(HFONT) ::SelectObject(hDC, (HGDIOBJ)hFont);//创建字体
TextOut(hDC, 0, 0, _T("test"), _tcslen(_T("test")));
::SelectObject(hDC, (HGDIOBJ)hOldFont);//还原字体
DeleteObject((HGDIOBJ)hFont);//删除创建字体
SetTextColor(hDC, clroldTextColor);//还原字段颜色 设置
SetBkColor(hDC, clrBackColor);//还原字段背景色 设置
*/
/**输出点
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
SetPixel(hDC, i, j, RGB(0, 0, 255));//画一个像素点
}
}
*/
/** 画线
HPEN hPen= CreatePen(PS_SOLID, 3, RGB(255, 0, 0));//产生一个画笔
HPEN hOldPen = (HPEN)::SelectObject(hDC, (HGDIOBJ)hPen);//设置画笔
{
MoveToEx(hDC, 0, 0, NULL);//设置画线的开始点
LineTo(hDC, rcClient.right, rcClient.bottom);//设置画线的结束点
}
::SelectObject(hDC, (HGDIOBJ)hOldPen);//还原画笔
DeleteObject((HGDIOBJ)hPen); //记得删除 使得GDI对象不被增加
*/
/**画面
RECT rc2;
rc2.left = 0;
rc2.right = 100;
rc2.top = 0;
rc2.bottom = 50;
HPEN hPen = (HPEN)::GetStockObject(NULL_PEN);//调用系统中的画笔 画无边框画面
//HPEN hPen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0));//产生一个画笔 来修改边框
HPEN hOldPen = (HPEN)::SelectObject(hDC, (HGDIOBJ)hPen);//设置画笔
{
HBRUSH hBrush = ::CreateSolidBrush(RGB(0, 0, 255));//创建一个画刷
HBRUSH hOldBrush = (HBRUSH)::SelectObject(hDC, (HGDIOBJ)hBrush);
{
::Rectangle(hDC, rc2.left, rc2.top, rc2.right, rc2.bottom);
}
::SelectObject(hDC, (HGDIOBJ)hOldBrush);//还原画刷
DeleteObject((HGDIOBJ)hBrush); //记得删除 使得GDI对象不被增加
}
::SelectObject(hDC, (HGDIOBJ)hOldPen);//还原画笔
DeleteObject((HGDIOBJ)hPen); //记得删除 使得GDI对象不被增加
*/
/**双缓存画图
1、在内存中创建与画布一致的缓冲区
2、在缓冲区画图
3、将缓冲区位图拷贝到当前画布上
4、释放内存缓冲区
HDC hMewDc = ::CreateCompatibleDC(hDC);//创建一个新的HDC
assert(hMewDc);
{
HBITMAP hCompatibleBitmap = ::CreateCompatibleBitmap(hDC, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);//创建一个画布
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMewDc, hCompatibleBitmap);//画布与HDC关联
assert(hCompatibleBitmap);
{
RECT rc2;
rc2.left = 0;
rc2.right = 100;
rc2.top = 0;
rc2.bottom = 50;
HPEN hPen = (HPEN)::GetStockObject(NULL_PEN);//调用系统中的画笔 画无边框画面
//HPEN hPen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0));//产生一个画笔 来修改边框
HPEN hOldPen = (HPEN)::SelectObject(hMewDc, (HGDIOBJ)hPen);//设置画笔
{
HBRUSH hBrush = ::CreateSolidBrush(RGB(0, 0, 255));//创建一个画刷
HBRUSH hOldBrush = (HBRUSH)::SelectObject(hMewDc, (HGDIOBJ)hBrush);
{
::Rectangle(hMewDc, rc2.left, rc2.top, rc2.right, rc2.bottom);
}
::SelectObject(hMewDc, (HGDIOBJ)hOldBrush);//还原画刷
DeleteObject((HGDIOBJ)hBrush); //记得删除 使得GDI对象不被增加
}
::SelectObject(hMewDc, (HGDIOBJ)hOldPen);//还原画笔
::DeleteObject((HGDIOBJ)hPen); //记得删除 使得GDI对象不被增加
::BitBlt(hDC, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, hMewDc, 0, 0, SRCCOPY);//把缓存中的画布内容显示出来
}
::SelectObject(hMewDc, (HGDIOBJ)hOldBitmap);
::DeleteObject((HGDIOBJ)hCompatibleBitmap);
}
::DeleteDC(hMewDc);
*/
}
::EndPaint(m_hWnd, &ps);
return TRUE;
}