zoukankan      html  css  js  c++  java
  • gdi+学习笔记

    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;
    }



    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    hdu 1593 find a way to escape
    bzoj4561: [JLoi2016]圆的异或并
    hdu 3511 Prison Break
    hdu 5299 Circles Game
    ORA-15025: could not open disk "/dev/asm***"--转载
    修改目的端trail文件的最大大小--转载
    MYSQL PERFORMANCE_SCHEMA HINTS
    Oracle 收缩表大小 Oracle Shrink Table --转载
    InnoDB: Error number 24 means ‘Too many open files’.--转载
    使用asmcmdcp命令把datafile从文件系统移动(move)到asm磁盘组中 针对11gR2
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/6616342.html
Copyright © 2011-2022 走看看