您的位置:首页 > 其它

ubuntu下codeblocks起步(九)-下

2007-10-31 11:40 369 查看
ubuntu下codeblocks起步(九)-bmp位图

分别为各子菜单项添加响应函数,cpp文件内容如下:
/***************************************************************
* Name: OSImageMain.cpp
* Purpose: Code for Application Frame
* Author: ()
* Created: 2007-10-23
* Copyright: ()
* License:
**************************************************************/

#include "OSImageMain.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(OSImageFrame)
#include <wx/bitmap.h>
#include <wx/font.h>
#include <wx/fontenum.h>
#include <wx/fontmap.h>
#include <wx/image.h>
#include <wx/intl.h>
#include <wx/settings.h>
#include <wx/string.h>
//*)
#include <wx/filedlg.h>
#include <wx/wx.h>
//helper functions

#define BYTE unsigned char
enum wxbuildinfoformat {
short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
wxString wxbuild(wxVERSION_STRING);

if (format == long_f )
{
#if defined(__WXMSW__)
wxbuild << _T("-Windows");
#elif defined(__UNIX__)
wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
wxbuild << _T("-Unicode build");
#else
wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
}
return wxbuild;
}

//(*IdInit(OSImageFrame)
const long OSImageFrame::idMenuQuit = wxNewId();
const long OSImageFrame::idMenuOpen = wxNewId();
const long OSImageFrame::idMenuPSave = wxNewId();
const long OSImageFrame::idMenuDraw = wxNewId();
const long OSImageFrame::idMenuBSave = wxNewId();
const long OSImageFrame::idMenuAbout = wxNewId();
const long OSImageFrame::ID_STATUSBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(OSImageFrame,wxFrame)
//(*EventTable(OSImageFrame)
//*)
END_EVENT_TABLE()

OSImageFrame::OSImageFrame(wxWindow* parent,wxWindowID id)
{
//(*Initialize(OSImageFrame)
wxMenuBar* MenuBar1;
wxMenu* Menu1;
wxMenuItem* MenuItem1;
wxMenuItem* MenuItem6;
wxMenu* Menu4;
wxMenu* Menu2;
wxMenuItem* MenuItem2;

Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
SetClientSize(wxSize(501,450));
MenuBar1 = new wxMenuBar();
Menu1 = new wxMenu();
MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit/tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
Menu1->Append(MenuItem1);
MenuBar1->Append(Menu1, _("&File"));
Menu3 = new wxMenu();
MenuItem3 = new wxMenuItem(Menu3, idMenuOpen, _("打开"), wxEmptyString, wxITEM_NORMAL);
Menu3->Append(MenuItem3);
MenuItem6 = new wxMenuItem(Menu3, idMenuPSave, _("保存"), wxEmptyString, wxITEM_NORMAL);
Menu3->Append(MenuItem6);
MenuBar1->Append(Menu3, _("位图"));
Menu4 = new wxMenu();
MenuItem4 = new wxMenuItem(Menu4, idMenuDraw, _("画图"), wxEmptyString, wxITEM_NORMAL);
Menu4->Append(MenuItem4);
MenuItem5 = new wxMenuItem(Menu4, idMenuBSave, _("保存"), wxEmptyString, wxITEM_NORMAL);
Menu4->Append(MenuItem5);
MenuBar1->Append(Menu4, _("像素"));
Menu2 = new wxMenu();
MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About/tF1"), _("Show info about this application"), wxITEM_NORMAL);
Menu2->Append(MenuItem2);
MenuBar1->Append(Menu2, _("Help"));
SetMenuBar(MenuBar1);
StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
int StatusBar1__widths[1] = { -1 };
int StatusBar1__styles[1] = { wxSB_NORMAL };
StatusBar1->SetFieldsCount(1,StatusBar1__widths);
StatusBar1->SetStatusStyles(1,StatusBar1__styles);
SetStatusBar(StatusBar1);
Connect(wxID_ANY,wxEVT_PAINT,(wxObjectEventFunction)&OSImageFrame::OnPaint);
Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&OSImageFrame::OnQuit);
Connect(idMenuOpen,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&OSImageFrame::OnMenuItem3Selected);
Connect(idMenuPSave,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&OSImageFrame::OnMenuItem6Selected);
Connect(idMenuDraw,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&OSImageFrame::OnMenuItem4Selected);
Connect(idMenuBSave,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&OSImageFrame::OnMenuItem5Selected);
Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&OSImageFrame::OnAbout);
//*)

}

OSImageFrame::~OSImageFrame()
{
//(*Destroy(OSImageFrame)
//*)
}

void OSImageFrame::OnQuit(wxCommandEvent& event)
{
Close();
}

void OSImageFrame::OnAbout(wxCommandEvent& event)
{
wxString msg = wxbuildinfo(long_f);
wxMessageBox(msg, _("Welcome to..."));
}

//“位图->打开”子菜单
void OSImageFrame::OnMenuItem3Selected(wxCommandEvent& event)
{
wxFileDialog OpenDlg(this, _T("打开位图"),wxEmptyString, wxEmptyString, _T("All files(*.*)|*.*|BMP files (*.bmp)|*.bmp"),wxFD_OPEN|wxFD_MULTIPLE);
OpenDlg.ShowModal();
OFileName = OpenDlg.GetPath();
m_bmp.LoadFile(OFileName,wxBITMAP_TYPE_BMP); //装载图片

Refresh();
Update();
}
//OnPaint函数,显示位图
void OSImageFrame::OnPaint(wxPaintEvent& event)
{
wxPaintDC dc(this);
dc.DrawBitmap(m_bmp, 0, 0, true); //显示图片
}
//“位图->保存”子菜单
void OSImageFrame::OnMenuItem6Selected(wxCommandEvent& event)
{
wxFileDialog SaveDlg(this, _T("保存位图"),wxEmptyString, wxEmptyString, _T("All files(*.*)|*.*|BMP files (*.bmp)|*.bmp"),wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
if ( SaveDlg.ShowModal() == wxID_OK )
{
SFileName = SaveDlg.GetPath();
m_bmp.SaveFile(SFileName,wxBITMAP_TYPE_BMP,NULL);
}
}
//“像素->画图”子菜单
void OSImageFrame::OnMenuItem4Selected(wxCommandEvent& event)
{
wxPen pen(wxColour(255,0,0), 1, wxSOLID);
wxPaintDC dc(this);
dc.SetPen(pen);
dc.DrawLine( 0 ,0,200,200);
dc.GetSize(&width, &height) ;
}

//“像素->保存”子菜单
void OSImageFrame::OnMenuItem5Selected(wxCommandEvent& event)
{
wxString str,str1;
width = width - (width%4);
//write into header
m_BMPHeader.bfType=0x4D42;
//指示 整个BMP文件字节数,其中0x36是文件头本身的长度
m_BMPHeader.bfSize=3*width*height+0x36;
m_BMPHeader.bfReserved1=0x0;
m_BMPHeader.bfReserved2=0x0;
m_BMPHeader.bfOffBits=0x36; //x36是文件头本身的长度
//以上共占据14个字节
m_BMPInfoHeader.biSize=sizeof(BITMAPINFOHEADER); //指示 文件信息头大小
m_BMPInfoHeader.biWidth=width; //图片宽度
m_BMPInfoHeader.biHeight=height; //图片高度
m_BMPInfoHeader.biPlanes=1;
m_BMPInfoHeader.biBitCount=24; //图片位数,位24位图
//以上共占据14+16个字节
m_BMPInfoHeader.biCompression=0; //表示没有压缩
m_BMPInfoHeader.biSizeImage=0x30; //因为没有压缩,所以可以设置为0
m_BMPInfoHeader.biXPelsPerMeter=0x0;
m_BMPInfoHeader.biYPelsPerMeter=0x0;
m_BMPInfoHeader.biClrUsed=0; //表明使用所有索引色
m_BMPInfoHeader.biClrImportant=0; //说明对图象显示有重要影响的颜色索引的数目,0表示都重要。
//以上共占据14+16+24个字节
/*m_BMPRgbQuad.rgbBlue=0x0;
m_BMPRgbQuad.rgbGreen=0x0;
m_BMPRgbQuad.rgbRed=0x0;
m_BMPRgbQuad.rgbReserved=0x0;

*/
// m_BMPInfo.bmiColors[1]=NULL;
m_BMPInfo.bmiHeader=m_BMPInfoHeader;
//创建并打开空的位图文件
fp = fopen("/home/qjizi/Desktop/a.bmp", "wb");
if (fp != NULL) {
//写入文件头
fwrite(&(m_BMPHeader),sizeof(m_BMPHeader),1, fp);
fwrite(&m_BMPInfoHeader,sizeof(m_BMPInfo)-sizeof(m_BMPRgbQuad),1,fp);

}

BYTE red,green,blue;
wxColor color;
wxPaintDC pDC(this); //创建DC
//写入像素颜色信息
for (int j=height;j>=1;j--)
{
for (int i=1;i<=width;i++)
{

//扫描客户区每个点,注意从坐下角 开始最后到 右上角
pDC.GetPixel(i,j, &color);
//获得“蓝”、“绿”、“红”分值
blue = color.Blue();
green = color.Green();
red = color.Red();
c1.b=blue;
c1.g=green;
c1.r=red;
fwrite(&c1,sizeof(MyPixel),1,fp);
}
}
}
fclose(fp); //关闭位图文件
wxMessageBox(_T("成功创建文件!"));
}
这里可以很清楚的看到写入位图文件的几个步骤。编译运行:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: