/***************************************************************
* Name: IndexXApp.h
* Purpose: Defines Application Class
* Author: xiangyang ()
* Created: 2015-04-25
* Copyright: xiangyang ()
* License:
**************************************************************/
#ifndef INDEXXAPP_H
#define INDEXXAPP_H
#include <wx/app.h>
class IndexXApp : public wxApp
{
public:
virtual bool OnInit();
};
#endif // INDEXXAPP_H
写了3个wxGrid放置在一个窗口里,希望点击一个wxgrid时响应当前表格的消息
头文件部分
#ifndef INDEXXMAIN_H
#define INDEXXMAIN_H
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include "IndexXApp.h"
#include <wx/button.h>
#include <wx/statline.h>
#include <wx/grid.h>
#include <wx/clipbrd.h>
class IndexXDialog: public wxDialog
{
public:
IndexXDialog(wxDialog *dlg, const wxString& title);
~IndexXDialog();
protected:
enum{
id_grid_zhu=1000,
id_grid_ci,
id_grid_index,
};
wxGrid *g_zhu,*g_ci,*g_index;
private:
void OnClose(wxCloseEvent& event);
void ongridzhuselect(wxGridEvent &ev);
void ongridciselect(wxGridEvent &ev);
const int g_column=3, g_rows=100;
DECLARE_EVENT_TABLE()
};
#endif // INDEXXMAIN_H
cpp部分:
/***************************************************************
* Name: IndexXApp.cpp
* Purpose: Code for Application Class
* Author: xiangyang ()
* Created: 2015-04-25
* Copyright: xiangyang ()
* License:
**************************************************************/
#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__
#include "IndexXApp.h"
#include "IndexXMain.h"
//(*AppHeaders
#include <wx/image.h>
//*)
IMPLEMENT_APP(IndexXApp);
bool IndexXApp::OnInit()
{
IndexXDialog* dlg = new IndexXDialog(0L, _("wxWidgets Application Template"));
dlg->SetIcon(wxICON(aaaa)); // To Set App Icon
dlg->Show();
return true;
}
CPP部分:
/***************************************************************
* Name: IndexXMain.cpp
* Purpose: Code for Application Frame
* Author: xiangyang ()
* Created: 2015-04-25
* Copyright: xiangyang ()
* License:
**************************************************************/
#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__
#include "IndexXMain.h"
#include <wx/datetime.h>
#include <wx/url.h>
BEGIN_EVENT_TABLE(IndexXDialog, wxDialog)
EVT_CLOSE(IndexXDialog::OnClose)
EVT_GRID_CELL_LEFT_CLICK(IndexXDialog::ongridzhuselect)
EVT_GRID_CELL_LEFT_CLICK(IndexXDialog::ongridciselect)
END_EVENT_TABLE()
IndexXDialog::IndexXDialog(wxDialog *dlg, const wxString &title)
: wxDialog(dlg, -1, title,wxDefaultPosition,wxDefaultSize,wxCLOSE_BOX|wxCAPTION|wxMINIMIZE_BOX|wxMAXIMIZE_BOX|wxRESIZE_BORDER)
{
this->SetSizeHints(500, 400);
g_zhu=new wxGrid(this,id_grid_zhu,wxDefaultPosition,wxDefaultSize);
this->Layout();
g_zhu->CreateGrid(g_rows,g_column);
g_ci->CreateGrid(g_rows,g_column);
g_index->CreateGrid(g_rows,g_column);
for(int i=0; i<g_column; i++) {
g_zhu->SetCellRenderer(0,i,new wxGridCellBoolRenderer);
g_zhu->SetCellEditor(0,i,new wxGridCellBoolEditor);
g_zhu->SetCellBackgroundColour(0,i,wxColour(255, 127, 127));
g_ci->SetCellRenderer(0,i,new wxGridCellBoolRenderer);
g_ci->SetCellEditor(0,i,new wxGridCellBoolEditor);
g_ci->SetCellBackgroundColour(0,i,wxColour(255, 127, 127));
}
}
IndexXDialog::~IndexXDialog()
{
}
void IndexXDialog::OnClose(wxCloseEvent &event)
{
Destroy();
}
void IndexXDialog::onpaint(wxPaintEvent &ev)
{
wxPaintDC dc(this);
PrepareDC(dc);
}
void IndexXDialog::onerasebackground(wxEraseEvent &ev)
{
wxImage image;
wxBitmap m_background;
wxImage::AddHandler(new wxPNGHandler);
if(image.LoadFile("1.png",wxBITMAP_TYPE_PNG)) {
m_background=wxBitmap(image);
}
if(m_background.Ok()) {
wxSize sz=GetClientSize();
wxRect rect(0,0,sz.x,sz.y);
if(ev.GetDC()) {
tilebitmap(rect,*(ev.GetDC()),m_background);
} else {
wxClientDC dc(this);
tilebitmap(rect,dc,m_background);
}
} else
ev.Skip();
}
bool IndexXDialog::tilebitmap(const wxRect &rect,wxDC &dc,wxBitmap &bitmap)
{
int w=bitmap.GetWidth();
int h=bitmap.GetHeight();
for(int i=rect.x; i<rect.x+rect.width; i+=w) {
for(int j=rect.y; j<rect.y+rect.height; j+=h) {
dc.DrawBitmap(bitmap,i,j);
}
}
return true;
}
void IndexXDialog::oncal(wxCommandEvent &ev)
{
}
void IndexXDialog::onindexcopy(wxCommandEvent &ev)
{
wxString s="";
for(int i=1; i<g_index->GetRows(); i++) { ///行数
for(int i1=0; i1<g_index->GetCols()-1; i1++) {
s=s+g_index->GetCellValue(i,i1);
s=s+"\t";
}
s=s+g_index->GetCellValue(i,g_index->GetCols()-1);
s=s+"\n";
}
if(wxTheClipboard->Open()) {
if(wxTheClipboard->IsSupported(wxDF_UNICODETEXT)) {
wxTheClipboard->SetData(new wxTextDataObject(s));
}
wxTheClipboard->Close();
}
}
void IndexXDialog::ongridzhuselect(wxGridEvent &ev)
{
if(ev.GetId()!=id_grid_zhu) { ///使用的控件ID来识别是否处理该消息
ev.Skip();
} else {
int col=ev.GetCol();
int cur_col=g_zhu->GetCols(),cur_rows=g_zhu->GetRows();
for(int i=0; i<cur_col; i++) {
i==col ? g_zhu->SetCellValue(0,i,"1"):g_zhu->SetCellValue(0,i,"");
}
ev.Skip();
}
}
void IndexXDialog::ongridciselect(wxGridEvent &ev)
{
if(ev.GetId()!=id_grid_ci) {<pre name="code" class="cpp">///使用的控件ID来识别是否处理该消息
ev.Skip(); } else { if(ev.GetId()!=id_grid_ci) ev.Skip(); int col=ev.GetCol(); int cur_col=g_ci->GetCols(),cur_rows=g_ci->GetRows(); for(int i=0; i<cur_col; i++) { i==col ? g_ci->SetCellValue(0,i,"1"):g_ci->SetCellValue(0,i,""); } ev.Skip(); }}