ubuntu下codeblocks起步(九)-下

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("成功創建文件!"));

}

這裏可以很清楚的看到寫入位圖文件的幾個步驟。編譯運行:

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章