📄 imageviewer.cpp
字号:
#include "stdafx.h"
#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::ComponentModel;
using namespace System::Drawing;
using namespace System::Windows::Forms;
__gc class ImageViewerForm : public Form
{
public:
ImageViewerForm();
protected:
void OnPaint(PaintEventArgs *e);
private:
//Event handlers for menu pFileMenu clicks
void OnExit (Object* sender, EventArgs *e);
void OnAbout (Object* sender, EventArgs *e);
void OnOpen (Object* sender, EventArgs *e);
//Your member variable for the image
Bitmap *m_pImage;
};
int __stdcall WinMain()
{
Application::Run(new ImageViewerForm());
return 0;
}
ImageViewerForm::ImageViewerForm()
{
// Set the title bar text.
Text = "Image Viewer";
// Create a top-level menu for your frame window.
MainMenu* pMenu = new MainMenu();
// Add a menu option to the File menu.
MenuItem* pFileMenu = pMenu->MenuItems->Add("File");
// Add the File menu items now that you have the File
// item added. Notice that the event handlers for these
// items are also specified as the items are added to
// the menu.
pFileMenu->MenuItems->Add("Open...",
new EventHandler(this,
&ImageViewerForm::OnOpen));
pFileMenu->MenuItems->Add("Exit",
new EventHandler(this,
&ImageViewerForm::OnExit));
// Add another top-level menu option and its submenu.
pFileMenu = pMenu->MenuItems->Add("Help");
pFileMenu->MenuItems->Add("About...",
new EventHandler(this,
&ImageViewerForm::OnAbout));
// Set the current Form object's menu
// to the newly created menu.
Menu = pMenu;
}
void ImageViewerForm::OnExit(Object *sender, EventArgs *e)
{
// Simply call the Form object's Close method.
Close();
}
void ImageViewerForm::OnAbout(Object *sender, EventArgs *e)
{
MessageBox::Show("This is version 1.0 of the Managed C++ "
"ImageViewer. For the most up-to-date "
"version, please be sure and check "
"www.TheCodeChannel.com",
"About ImageViewer");
}
void ImageViewerForm::OnOpen(Object *sender, EventArgs *e)
{
// The OpenFileDialog is an encapsulation of the common
// File Open dialog box.
OpenFileDialog* pOpenFileDlg = new OpenFileDialog();
// You can filter here just as you can with the standard
// Win32 API or the MFC CFileDialog class.
pOpenFileDlg->Filter
= "JPEG files (*.jpg;*.jpeg)|*.jpg;*.jpeg";
// Display the File Open dialog box so that the user
// can select an image file to be opened.
// If user has clicked OK...
if (DialogResult::OK == pOpenFileDlg->ShowDialog())
{
// Retrieve the dialog box's pstrFileName FileName property, which
// is the name of the file the user selected to open.
String *pstrFileName = pOpenFileDlg->FileName;
// Now that you have a valid filename, set the
// ImageViewerForm image member to an instantiation
// of the Bitmap class using the filename.
m_pImage = new Bitmap(pstrFileName);
// Here I'm setting the form to scroll if the image
// is too large to be accommodated. Note the setting of the
// minimum size to the image size.
AutoScroll=true;
AutoScrollMinSize = m_pImage->Size;
// Invalidate the form to cause a repaint.
Invalidate();
}
}
void ImageViewerForm::OnPaint(PaintEventArgs *e)
{
// If you've loaded an image...
if (m_pImage)
{
// Get the Graphics object associated with your
// form's DC (device context).
Graphics *pGraphics = e->Graphics;
// Use the Graphics::DrawImage function to
// draw the image.
pGraphics->DrawImage(m_pImage,
AutoScrollPosition.X,
AutoScrollPosition.Y,
m_pImage->Width,
m_pImage->Height);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -