RECT rect; //获得客户区坐标 GetClientRect(&rect); //Graphics作图对象 Graphics g(m_hWnd);
画线:
Pen pen(Color(255, 0, 0, 0)); g.DrawLine(&pen, rect.left, rect.top, rect.right, rect.bottom); g.DrawLine(&pen, rect.left, rect.bottom, rect.right, rect.top);
画字符串:
//画字符串 SolidBrush brush(Color(255, 0, 0, 255)); FontFamily fontFamily(TEXT("Times New Roman")); Font font(&fontFamily, 24, FontStyleRegular, UnitPixel); PointF pt(10.0f, 10.0f); g.DrawString(L"Hello World", -1, &font, pt, &brush);
画矩形:
Pen pen(Color(255, 0, 255, 0)); while (rect.left < rect.right && rect.top < rect.bottom) { g.DrawRectangle(&pen, rect.left, rect.top, rect.right, rect.bottom); rect.left += 5; rect.top += 5; rect.right -= 10; rect.bottom -= 10; }
填充区域:
//填充矩形 SolidBrush brush(Color(255, 0, 255, 0)); brush.SetColor(Color(255, 0, 0, 255)); g.FillRectangle(&brush, rect.left, rect.top, rect.right, rect.bottom);
MainDlg.h
// MainDlg.h : interface of the CMainDlg class // ///////////////////////////////////////////////////////////////////////////// #pragma once class CMainDlg : public CDialogImpl<CMainDlg> { public: enum { IDD = IDD_MAINDLG }; BEGIN_MSG_MAP(CMainDlg) MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog) MESSAGE_HANDLER(WM_PAINT, OnPaint) COMMAND_ID_HANDLER(IDCANCEL, OnCancel) END_MSG_MAP() // Handler prototypes (uncomment arguments if needed): // LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) // LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) // LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/) LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/); LRESULT OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/); LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/); };MainDlg.cpp
// MainDlg.cpp : implementation of the CMainDlg class // ///////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "resource.h" #include "MainDlg.h" #include <atlimage.h> #include <time.h> #include <gdiplus.h> #pragma comment (lib,"Gdiplus.lib") using namespace Gdiplus; ULONG_PTR gdiplusToken; LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) { // center the dialog on the screen CenterWindow(); // set icons HICON hIcon = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON)); SetIcon(hIcon, TRUE); HICON hIconSmall = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON)); SetIcon(hIconSmall, FALSE); GdiplusStartupInput gdiplusStartupInput; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); return TRUE; } LRESULT CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) { EndDialog(wID); GdiplusShutdown(gdiplusToken); return 0; } LRESULT CMainDlg::OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) { /* srand((unsigned)time(NULL)); CPaintDC dc(this->m_hWnd); CBrush brush; CRect rect; GetClientRect(&rect); InvalidateRect(&rect, FALSE); brush.CreateSolidBrush(RGB(rand() % 255, rand() % 255, rand() % 255)); dc.FillRect(&rect, brush); */ RECT rect; //获得客户区坐标 GetClientRect(&rect); //Graphics作图对象 Graphics g(m_hWnd); //画线 //Pen pen(Color(255, 0, 0, 0)); //g.DrawLine(&pen, rect.left, rect.top, rect.right, rect.bottom); //g.DrawLine(&pen, rect.left, rect.bottom, rect.right, rect.top); //画字符串 /* SolidBrush brush(Color(255, 0, 0, 255)); FontFamily fontFamily(TEXT("Times New Roman")); Font font(&fontFamily, 24, FontStyleRegular, UnitPixel); PointF pt(10.0f, 10.0f); g.DrawString(L"Hello World", -1, &font, pt, &brush); */ //画矩形 Pen pen(Color(255, 0, 255, 0)); while (rect.left < rect.right && rect.top < rect.bottom) { g.DrawRectangle(&pen, rect.left, rect.top, rect.right, rect.bottom); rect.left += 5; rect.top += 5; rect.right -= 10; rect.bottom -= 10; } //填充矩形 SolidBrush brush(Color(255, 0, 255, 0)); brush.SetColor(Color(255, 0, 0, 255)); g.FillRectangle(&brush, rect.left, rect.top, rect.right, rect.bottom); return TRUE; }