Phu, that is not an easy task, as my screenshot tool is only a small add to a program, which I can't easily post here.
I have a really ugly compressed version with the not working toolbar tray icon and its context menu with "screenshot".
Start looking at function:
Code: Select all
void MyFrame::OnMouse(wxMouseEvent& event)
This one fires, after you clicked on menu item "Screenshot" and then tried to click and pull on the screen; but it does only once and with both events (up and down) at the same time.
And here comes the whole mess of compressed, but compileable, code
(used wxWidgets 2.9.2)
MyTaskBarIcon.cpp
Code: Select all
#include "MyTaskBarIcon.h"
#include <wx/cmdline.h>
#include <wx/menu.h>
#include <wx/filename.h>
#include <wx/textfile.h>
#include <wx/process.h>
#include <wx/stdpaths.h>
IMPLEMENT_APP(MyApp)
// 'Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit()
{
if ( !wxApp::OnInit() )
return false;
mfn_apppath = (new wxStandardPaths)->GetExecutablePath();
// create the main application window
m_frame = new MyFrame("Minimal wxWidgets App");
// and show it (the frames, unlike simple controls, are not shown when
// created initially)
m_frame->Show(false);
return true;
}
void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
{
static const wxCmdLineEntryDesc cmdLineDesc[] =
{
{wxCMD_LINE_PARAM, NULL, NULL, "", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL},
{ wxCMD_LINE_NONE }
};
parser.SetDesc (cmdLineDesc);
}
DECLARE_APP(MyApp)
BEGIN_EVENT_TABLE(MyTaskBarIcon, wxTaskBarIcon)
EVT_MENU(ID_TASK_EXIT, MyTaskBarIcon::OnMenuExit)
EVT_MENU(ID_TASK_SCREENSHOT_RECT, MyTaskBarIcon::OnScreenshotRect)
END_EVENT_TABLE()
wxMenu *MyTaskBarIcon::CreatePopupMenu()
{
wxMenu *menu = new wxMenu;
menu->Append(ID_TASK_SCREENSHOT_RECT, wxT("Screenshot"));
menu->Append(ID_TASK_EXIT, wxT("exit"));
return menu;
}
void MyTaskBarIcon::OnMenuExit(wxCommandEvent&)
{
wxGetApp().m_frame->Close(true);
}
void MyTaskBarIcon::OnScreenshotRect(wxCommandEvent&)
{
wxGetApp().m_frame->Screenshot_ToAppDir_rect();
}
DECLARE_APP(MyApp)
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MOUSE_EVENTS(MyFrame::OnMouse)
EVT_TIMER(ID_TIMER, MyFrame::Go)
END_EVENT_TABLE()
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxPoint(10,10), wxSize(100,100), wxSTAY_ON_TOP|wxRESIZE_BORDER|wxFRAME_NO_TASKBAR)
{
// set the frame icon
SetIcon(wxICON(infoicon));
wxInitAllImageHandlers();
m_taskBarIcon = new MyTaskBarIcon();
// we should be able to show up to 128 characters on recent Windows versions
// (and 64 on Win9x)
if ( !m_taskBarIcon->SetIcon(wxICON(infoicon), "Patientenhinweis-Dienst") )
{
wxLogError(wxT("Icon konnte nicht gesetzt werden."));
}
#if defined(__WXOSX__) && wxOSX_USE_COCOA
m_dockIcon = new MyTaskBarIcon(wxTaskBarIcon::DOCK);
if ( !m_dockIcon->SetIcon(wxICON(infoicon)) )
{
wxLogError(wxT("Icon konnte nicht gesetzt werden."));
}
#endif
mb_mouseCaptured = false;
mb_LeftMouseDown = false;
mfn_DirPath = wxGetApp().mfn_apppath;
ml_timerLapse = 500;
mt_timer = new wxTimer(this, ID_TIMER);
//mt_timer->Connect(ID_TIMER, wxEVT_TIMER, wxTimerEventHandler(MyFrame::Go), NULL, this);
//mt_timer->Bind(wxEVT_TIMER, wxTimerEventHandler(MyFrame::Go), ID_TIMER, wxID_ANY, NULL);
mt_timer->Start(1, true);
}
MyFrame::~MyFrame()
{
delete m_taskBarIcon;
#if defined(__WXCOCOA__)
delete m_dockIcon;
#endif
}
void MyFrame::Screenshot_ToAppDir_rect()
{
mfn_DirPath = wxGetApp().mfn_apppath;
ScreenshotRect(mfn_DirPath.GetFullPath());
}
void MyFrame::ScreenshotRect(wxString sSaveTo)
{
m_screeneRect = wxRect(0,0,0,0);
m_screenePointStart = wxPoint(0,0);
m_screenePointEnd = wxPoint(0,0);
mb_LeftMouseDown = false;
if(!mb_mouseCaptured)
{
wxWindow::CaptureMouse();
mb_mouseCaptured = true;
}
}
void MyFrame::OnMouse(wxMouseEvent& event)
{
if(event.GetEventType() == wxEVT_LEFT_DOWN)
{
m_screenePointStart = wxGetMousePosition();
if(!mb_LeftMouseDown)
{
mb_LeftMouseDown = true;
m_screenePointStart = wxGetMousePosition();
wxWindow::ReleaseMouse();
wxWindow::CaptureMouse();
}
else
{
mb_LeftMouseDown = false;
m_screenePointEnd = wxGetMousePosition();
wxWindow::ReleaseMouse();
FinishRectSave();
}
wxString s = "";
s << "LeftDown: " << mb_LeftMouseDown;
wxLogMessage(s);
}
else if(event.GetEventType() == wxEVT_LEFT_UP)
{
wxLogMessage("LeftUp");
}
else if(event.GetEventType() == wxEVT_RIGHT_DOWN)
{
wxLogMessage("RightDown");
}
}
void MyFrame::FinishRectSave()
{
wxString sA, sE = "";
sA << m_screenePointStart.x << "\n" << m_screenePointStart.y;
sE << m_screenePointEnd.x << "\n" << m_screenePointEnd.y;
wxMessageBox(sA + "\n" + sE);
wxSize screeneSize(m_screenePointEnd.x - m_screenePointStart.x, m_screenePointEnd.y - m_screenePointStart.y);
if(screeneSize.x <= 0 || screeneSize.y <= 0)
return;
m_screeneRect = wxRect(m_screenePointStart, screeneSize);
//Screenshot(mfn_DirPath.GetFullPath(), m_p, m_screeneRect);
}
void MyFrame::Go(wxTimerEvent &event)
{
mt_timer->Stop();
//other stuff [...]
mt_timer->Start(ml_timerLapse);
}
MyTaskBarIcon.h
Code: Select all
#pragma once
#include <wx/taskbar.h>
#include "wx/wxprec.h"
#include <wx/filename.h>
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#if !defined(__WXMSW__) && !defined(__WXPM__)
#include "../sample.xpm"
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#ifndef NDEBUG
#define NDEBUG
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
class MyTaskBarIcon : public wxTaskBarIcon
{
public:
#if defined(__WXOSX__) && wxOSX_USE_COCOA
MyTaskBarIcon(wxTaskBarIconType iconType = DEFAULT_TYPE)
: wxTaskBarIcon(iconType)
#else
MyTaskBarIcon()
#endif
{}
public:
void OnMenuExit(wxCommandEvent&);
void OnScreenshot(wxCommandEvent&);
void OnScreenshotRect(wxCommandEvent&);
void OnCharHook(wxKeyEvent &event);
void OnMouseLeftUp(wxMouseEvent& event);
void OnMouseLeftDown(wxMouseEvent& event);
virtual wxMenu *CreatePopupMenu();
DECLARE_EVENT_TABLE()
};
enum
{
//ID_TASK_SHOW,
ID_TASK_EXIT,
//ID_TASK_RESTART,
//ID_TASK_TEAMVIEWER,
ID_TASK_SCREENSHOT,
ID_TASK_SCREENSHOT_RECT
};
#ifndef myframe_h
#define myframe_h
// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
public:
// ctor(s)
MyFrame(const wxString& title);
~MyFrame();
wxFileName mfn_DirPath;
//Screenshots - Vars
bool mb_mouseCaptured;
bool mb_LeftMouseDown ;
wxRect m_screeneRect;
wxPoint m_screenePointStart;
wxPoint m_screenePointEnd;
wxTimer *mt_timer;
long ml_timerLapse;
//Screenshots - Functions
void FinishRectSave();
void Screenshot_ToAppDir_rect();
void ScreenshotRect(wxString sSaveTo);
MyTaskBarIcon *m_taskBarIcon;
#if defined(__WXOSX__) && wxOSX_USE_COCOA
MyTaskBarIcon *m_dockIcon;
#endif
void OnMouse(wxMouseEvent& event);
void Go(wxTimerEvent &event);
private:
// any class wishing to process wxWidgets events must use this macro
DECLARE_EVENT_TABLE()
};
// Define a new application type, each program should derive a class from wxApp
class MyApp : public wxApp
{
public:
// override base class virtuals
// ----------------------------
MyFrame *m_frame;
wxFileName mfn_apppath;
wxFileName baseexe;
//wxFileName mfn_dbpath;
//wxFileName mfn_patConfigPath;
int processid;
// this one is called on application startup and is a good place for the app
// initialization (doing it here and not in the ctor allows to have an error
// return: if OnInit() returns false, the application terminates)
virtual void OnInitCmdLine(wxCmdLineParser& parser);
// virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
virtual bool OnInit();
};
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// IDs for the controls and the menu commands
enum
{
// menu items
//Minimal_Quit = wxID_EXIT,
// it is important for the id corresponding to the "About" command to have
// this standard value as otherwise it won't be handled properly under Mac
// (where it is special and put into the "Apple" menu)
//Minimal_About = wxID_ABOUT,
ID_TIMER
};
#endif // myframe_h
Functions of interest in correct order:
Code: Select all
void MyTaskBarIcon::OnScreenshotRect(wxCommandEvent&)
void MyFrame::Screenshot_ToAppDir_rect()
void MyFrame::ScreenshotRect(wxString sSaveTo)
void MyFrame::OnMouse(wxMouseEvent& event) //error
(void MyFrame::FinishRectSave())
Greets
Natu