⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitmaptransferdlg.cpp

📁 Visual_C++[1].NET_Bible1 Visual_C++宝典书中的全部源码
💻 CPP
字号:
// BitmapTransferDlg.cpp : implementation file
//

#include "stdafx.h"
#include "BitmapTransfer.h"
#include "BitmapTransferDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
public:
  afx_msg void OnBnClickedOk();
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  ON_BN_CLICKED(IDOK, OnBnClickedOk)
END_MESSAGE_MAP()


// CBitmapTransferDlg dialog



CBitmapTransferDlg::CBitmapTransferDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBitmapTransferDlg::IDD, pParent)
  , m_x(0)
  , m_y(0)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBitmapTransferDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  DDX_Control(pDX, ID_GRPBITMAPFROMCLIPBOARD, m_grpBitmapFromClipboard);
  DDX_Control(pDX, IDC_EDT_BITMAPTEXT, m_edtBitmapText);
}

BEGIN_MESSAGE_MAP(CBitmapTransferDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
  ON_BN_CLICKED(ID_BTNPASTEBITMAP, OnBnClickedBtnpastebitmap)
  ON_BN_CLICKED(ID_BTNCOPYBITMAP, OnBnClickedBtncopybitmap)
  ON_BN_CLICKED(ID_BTNPASTEBITMAP2, OnBnClickedBtnpastebitmap2)
END_MESSAGE_MAP()


// CBitmapTransferDlg message handlers

BOOL CBitmapTransferDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CBitmapTransferDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CBitmapTransferDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CBitmapTransferDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CBitmapTransferDlg::OnBnClickedBtnpastebitmap()
{
 // Test to see if we can open the clipboard first.
 if (OpenClipboard())
 {
  // Determine if the data on the Clipboard is a bitmap.
  // Failure to do so will almost certainly lead to a
  // GPF if the data on the Clipboard is not a bitmap
  // but is treated as such.
  if (::IsClipboardFormatAvailable(CF_BITMAP))
  {
   //Get the clipboard data. Notice the CF_BITMAP value
   // being passed.
   HBITMAP handle = (HBITMAP)GetClipboardData(CF_BITMAP);
   
   // At this point, we can assume that we have a handle
   // to a bitmap object since we've checked the Clipboard
   // data type first. Now, using the CBitmap::FromHandle 
   // static member function will return a pointer to
   // a CBitmap object wrapper for that bitmap data.
   CBitmap* bm = CBitmap::FromHandle(handle);

   // Since we are going to manually place the bitmap 
   // on the dialog, we need to create a device context.
   // First, we get the current client device context (cdc).
   CClientDC cdc(this);
   
   // Now, we create a memory device context that is compatible
   // with the client device context. A memory device context
   // is a block of memory that represents a display surface 
   // onto which we can draw the bitmap. For a more complete 
   // discussion on device contexts, you can return to 
   // Chapter 8 for a refresher.
   CDC dc;
   dc.CreateCompatibleDC(&cdc);
   
   // The CDC.SelectObject member function enables you to 
   // select a GDI object into the memory device context.
   dc.SelectObject(bm);

   // Since we want to display the bitmap inside the dialog's
   // group control, we simply retrieve it coordinates using 
   // its GetWindowRect member function, which then copies
   // its size and position into a structure of type RECT.
   RECT rect;
   m_grpBitmapFromClipboard.GetWindowRect(&rect);

   // The ScreenToClient function will translate the 
   // positioning values from screen coordinates to the 
   // dialog's client coordinates. Once again, this is 
   // covered in more detail in Chapter 8.
   ScreenToClient(&rect);
   
   // Now we "bit-blit" the image onto the device context. 
   // Notice that the group control's values are used to 
   // both position and size the bitmap.
   cdc.BitBlt(rect.left + 10, rect.top + 20, 
              (rect.right - rect.left) - 10,
              (rect.bottom - rect.top) - 40, 
              &dc,
              0,0,
              SRCCOPY);

  }
  else
  { 
   // Basic message stating that the data on the Clipboard
   // is not a bitmap image.
   AfxMessageBox("There is no BITMAP data on the Clipboard.");
  }

  // Finally, when finished I simply close the Clipboard
  // which has the effect of unlocking it so that other
  // applications can examine or modify its contents.
  CloseClipboard();
 }
}

void CBitmapTransferDlg::OnBnClickedBtncopybitmap()
{
 // Test to see if we can open the clipboard first.
 if (OpenClipboard())
 {
  // Empty the Clipboard. This also has the effect
  // of allowing Windows to free the memory associated
  // with any data that is in the Clipboard
  EmptyClipboard();
  
  //Create a new bitmap object.
  CBitmap* pNewBitmap = new CBitmap();
  
  // Obtain the client device context.
  CClientDC cdc(this);
  
  // Create a compatibile memory device context
  // onto which we'll draw the new image.
  CDC dc;
  dc.CreateCompatibleDC(&cdc);
  
  // Arbitrary size and positioning of the image.
  CRect client(0, 0, 200, 200);
  
  // Initialize a bitmap that is compatible with
  // the client device context.
  pNewBitmap->CreateCompatibleBitmap(&cdc,
                                      client.Width(),
                                      client.Height());
                                      
  // Select our new bitmap image into the memory
  // device context.
  dc.SelectObject(pNewBitmap);

  // Obtain the user text from the dialog's edit control.
  CString strBitmapText;
  m_edtBitmapText.GetWindowText(strBitmapText);

  // The DrawImage function makes the actual GDI calls
  // to do all the drawing. This way the function 
  // is abstracted from this code and can be used 
  // in other contexts. All I have to pass it is the 
  // device context to use and the text to draw.
  DrawImage(&dc, strBitmapText);

  // Once the image has been created, simply copy the image
  // to the Clipboard using the SetClipboard function. 
  // Notice that I need only specify the format as 
  // CF_BITMAP and pass a handle to the bitmap. This handle
  // is a member variable of the CBitmap object.
  SetClipboardData(CF_BITMAP, pNewBitmap->m_hObject);
  
  // Finally, when finished I simply close the Clipboard
  // which has the effect of unlocking it so that other
  // applications can examine or modify its contents.
  CloseClipboard();

  // Once the data has been copied to the Clipboard and
  // the Clipboard has been closed, it is safe to delete the
  //locally allocated memory for the CBitmap object.
  delete pNewBitmap;
 }

 // Display a confirmation message that the bitmap is on 
 // the Clipboard.
 MessageBox("The bitmap has been copied to clipboard. "
            "Click the Paste button to display "
            "the bitmap on this dialog", 
            NULL, 
            MB_OK);
}

void CBitmapTransferDlg::DrawImage(CDC * pDC, CString pText) 
{
 // Here I simply picked an arbitrary size for the new image
 CRect rectDrawingArea(0,0,200,200);

 // First, I'll fill the area with a light gray colour
 pDC->FillSolidRect(rectDrawingArea,RGB(192,192,192));

 // Now, I create a pen object which will be used
 // to draw the lines on the image. The parameters
 // being passed simply indicate that I want a 
 // a solid pen (PS_SOLID) as opposed to dashed
 // or dotted lines, a width of 3 pixels and the 
 // colour blue (RGB=0,0,255).
 CPen pen;
 pen.CreatePen(PS_SOLID, 3, RGB(0,0,255));

 // As you saw in the Paste Bitmap code and as you
 // read in Chapter 8, in order to use this new 
 // GDI object (the pen), I need to select that object
 // into the device context. I'll save the current
 // pen so that when finished I can restore the
 // current pen.
 CPen * oldpen = pDC->SelectObject(&pen);
 
 // These are simple GDI calls to draw a tic-tac-toe 
 // type diagram.
 pDC->MoveTo(70, 10);
 pDC->LineTo(70, 190);
 
 pDC->MoveTo(130, 10);
 pDC->LineTo(130, 190);

 pDC->MoveTo(10, 70);
 pDC->LineTo(190, 70);

 pDC->MoveTo(10, 130);
 pDC->LineTo(190, 130);

 // When finished using the pen, simply select the previous
 // pen back into the current device context.
 pDC->SelectObject(oldpen);

 // Now delete the pen that was used to draw the image.
 pen.DeleteObject();

 // Using the device context object's TextOut member function,
 // draw the text over the image at three arbitrary positions.
 pDC->TextOut(10, 10, pText);
 pDC->TextOut(20, 50, pText);
 pDC->TextOut(50, 100, pText);
}

void CAboutDlg::OnBnClickedOk()
{
	CWaitCursor wait;

  CString strUrl = "http://www.thecodechannel.com/redirect.asp?u=/&s=vcnb";
  if (32 >= (int)ShellExecute(NULL, "open", strUrl, NULL, NULL, SW_SHOWNORMAL))
	{
		AfxMessageBox("::ShellExecute failed to open this link!");
	}
}

void CBitmapTransferDlg::OnBnClickedBtnpastebitmap2()
{
  CAboutDlg().DoModal();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -