/***************************************************************
* Name: MyGodMain.cpp
* Purpose: Code for example to access the PostgreSQL system.
* Author: 徐杨飞
* Created: 2010-02-06
* Copyright: 徐杨飞
* License: LGPL
**************************************************************/
#include "MyGodMain.h"
#include <wx/msgdlg.h>
#include <libpq-fe.h>
//(*InternalHeaders(MyGodDialog)
#include <wx/intl.h>
#include <wx/string.h>
//*)
//helper functions
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(MyGodDialog)
const long MyGodDialog::ID_STATICTEXT1 = wxNewId();
const long MyGodDialog::ID_TEXTCTRL1 = wxNewId();
const long MyGodDialog::ID_BUTTON1 = wxNewId();
const long MyGodDialog::ID_BUTTON3 = wxNewId();
const long MyGodDialog::ID_BUTTON2 = wxNewId();
const long MyGodDialog::ID_GRID1 = wxNewId();
//*)
//==================================================================
// 这个是http://hi.baidu.com/nivrrex/blog/item/a82febc4614955ae8226ac2e.html中的代码
wchar_t * ANSIToUnicode( const char* str )
{
int textlen ;
wchar_t * result;
textlen = MultiByteToWideChar( CP_ACP, 0, str,-1, NULL,0 );
result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t));
memset(result,0,(textlen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0,str,-1,(LPWSTR)result,textlen );
return result;
}
char * UnicodeToANSI( const wchar_t* str )
{
char* result;
int textlen;
textlen = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
result =(char *)malloc((textlen+1)*sizeof(char));
memset( result, 0, sizeof(char) * ( textlen + 1 ) );
WideCharToMultiByte( CP_ACP, 0, str, -1, result, textlen, NULL, NULL );
return result;
}
wchar_t * UTF8ToUnicode( const char* str )
{
int textlen ;
wchar_t * result;
textlen = MultiByteToWideChar( CP_UTF8, 0, str,-1, NULL,0 );
result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t));
memset(result,0,(textlen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0,str,-1,(LPWSTR)result,textlen );
return result;
}
char * UnicodeToUTF8( const wchar_t* str )
{
char* result;
int textlen;
textlen = WideCharToMultiByte( CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL );
result =(char *)malloc((textlen+1)*sizeof(char));
memset(result, 0, sizeof(char) * ( textlen + 1 ) );
WideCharToMultiByte( CP_UTF8, 0, str, -1, result, textlen, NULL, NULL );
return result;
}
/*宽字符转换为多字符Unicode - ANSI*/
char* w2m(const wchar_t* wcs)
{
int len;
char* buf;
len =wcstombs(NULL,wcs,0);
if (len == 0)
return NULL;
buf = (char *)malloc(sizeof(char)*(len+1));
memset(buf, 0, sizeof(char) *(len+1));
len =wcstombs(buf,wcs,len+1);
return buf;
}
/*多字符转换为宽字符ANSI - Unicode*/
wchar_t* m2w(const char* mbs)
{
int len;
wchar_t* buf;
len =mbstowcs(NULL,mbs,0);
if (len == 0)
return NULL;
buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
memset(buf, 0, sizeof(wchar_t) *(len+1));
len =mbstowcs(buf,mbs,len+1);
return buf;
}
char* ANSIToUTF8(const char* str)
{
return UnicodeToUTF8(ANSIToUnicode(str));
}
char* UTF8ToANSI(const char* str)
{
return UnicodeToANSI(UTF8ToUnicode(str));
}
//由nivrrex先生编写。在此予以感谢!
//==================================================================
BEGIN_EVENT_TABLE(MyGodDialog,wxDialog)
//(*EventTable(MyGodDialog)
//*)
END_EVENT_TABLE()
MyGodDialog::MyGodDialog(wxWindow* parent,wxWindowID id)
{
//(*Initialize(MyGodDialog)
wxFlexGridSizer* FlexGridSizer4;
wxFlexGridSizer* FlexGridSizer5;
wxFlexGridSizer* FlexGridSizer6;
Create(parent, wxID_ANY, _("wxWidgets app"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("wxID_ANY"));
BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
BoxSizer2 = new wxBoxSizer(wxVERTICAL);
FlexGridSizer4 = new wxFlexGridSizer(0, 3, 0, 0);
StaticText1 = new wxStaticText(this, ID_STATICTEXT1, _("姓名:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
FlexGridSizer4->Add(StaticText1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
TextCtrl1 = new wxTextCtrl(this, ID_TEXTCTRL1, _("Text"), wxDefaultPosition, wxSize(242,22), 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
FlexGridSizer4->Add(TextCtrl1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer2->Add(FlexGridSizer4, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
FlexGridSizer6 = new wxFlexGridSizer(0, 3, 0, 0);
Button1 = new wxButton(this, ID_BUTTON1, _("关于"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
FlexGridSizer6->Add(Button1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4);
Button3 = new wxButton(this, ID_BUTTON3, _("查询数据库"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON3"));
FlexGridSizer6->Add(Button3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
Button2 = new wxButton(this, ID_BUTTON2, _("退出"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
FlexGridSizer6->Add(Button2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4);
BoxSizer2->Add(FlexGridSizer6, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
FlexGridSizer5 = new wxFlexGridSizer(0, 3, 0, 0);
Grid1 = new wxGrid(this, ID_GRID1, wxDefaultPosition, wxSize(450,200), 0, _T("ID_GRID1"));
Grid1->CreateGrid(20,6);
Grid1->EnableEditing(true);
Grid1->EnableGridLines(true);
Grid1->SetRowLabelSize(30);
Grid1->SetDefaultCellFont( Grid1->GetFont() );
Grid1->SetDefaultCellTextColour( Grid1->GetForegroundColour() );
FlexGridSizer5->Add(Grid1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer2->Add(FlexGridSizer5, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer1->Add(BoxSizer2, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4);
SetSizer(BoxSizer1);
BoxSizer1->Fit(this);
BoxSizer1->SetSizeHints(this);
Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&MyGodDialog::OnAbout);
Connect(ID_BUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&MyGodDialog::OnButton3Click);
Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&MyGodDialog::OnQuit);
//*)
}
MyGodDialog::~MyGodDialog()
{
//(*Destroy(MyGodDialog)
//*)
}
void MyGodDialog::OnQuit(wxCommandEvent& event)
{
Close();
}
void MyGodDialog::OnAbout(wxCommandEvent& event)
{
wxString msg = wxbuildinfo(long_f);
msg.Printf(_T("这是一个参数%d!"),150);
wxMessageBox(msg, _("Welcome to..."));
}
void MyGodDialog::OnButton3Click(wxCommandEvent& event)
{
PGconn *conn;
PGresult *res;
int nFields;
int i, j;
wxString str;
conn = PQconnectdb("host=localhost dbname=test user=admin password=921090 port=5432 connect_timeout=30");
if(PQstatus(conn) != CONNECTION_OK)
{
wxMessageBox(_T("与PostgreSQL数据库系统连接失败."), _T("警告"));
PQfinish(conn);
}
PQsetClientEncoding(conn, "ASCII");
res = PQexec(conn, "BEGIN");
PQclear(res);
res = PQexec(conn,
评论0
最新资源