SOL9 Sample: SolProcessWatcher

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * SolProcessWatcher.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */



// SOL9
// 2009/01/23
// 2012/03/13 Modified to use an inner class ProcessListView.

#include <sol\ApplicationView.h>
#include <sol\StatusBar.h>
#include <sol\FileDialog.h>
#include <sol\SystemImageList.h>
#include <sol\ListView.h>
#include <sol\FileVersionDialog.h>
#include <sol\COMInitializer.h>

#include "SolProcessWatcherThread.h"


#include "resource.h"

namespace SOL {

/**
 * SolProcessWatcher class is a sample Appplication program to 
 * display a newly created process properties.
 */
class SolProcessWatcher :public ApplicationView {

private:
  //2012/03/13
  // <InnerClass>
  //----------------------------------------------------------------------
  class ProcessListView :public ListView {
  private:  
    static const int NAME_WIDTH         = 140;
    static const int CREATIONDATE_WIDTH = 150;
    static const int PID_WIDTH          = 100;
    static const int __COMMANDLINE_WIDTH= 400;
    SystemImageList  imagelist;

  public:
    Boolean create(View* parent, const TCHAR* name, Args& ar)
    {
      Boolean rc = ListView::create(parent, NULL, ar);  
      setImageList(&imagelist, LVSIL_SMALL);

      ListViewColumn items[] = {
        {_T("Name"),    LVCFMT_LEFT, NAME_WIDTH},
        {_T("CommandLine"), LVCFMT_LEFT, __COMMANDLINE_WIDTH},
        {_T("CreationDate"),LVCFMT_LEFT, CREATIONDATE_WIDTH},
        {_T("PID"),      LVCFMT_LEFT, PID_WIDTH},
      };
      setColumn(items, sizeof(items)/sizeof(items[0]));

      return rc;
    }

  public:
    void reshape(int x, int y, int w, int h)
    {
      ListView::reshape(x, y, w, h);

      int cw, ch;
      getClientSize(cw, ch);

      int COMMANDLINE_WIDTH = cw - NAME_WIDTH - CREATIONDATE_WIDTH - PID_WIDTH;
      if (COMMANDLINE_WIDTH < __COMMANDLINE_WIDTH) {
        COMMANDLINE_WIDTH = __COMMANDLINE_WIDTH;
      }

      setColumnWidth(0, NAME_WIDTH);
      setColumnWidth(1, COMMANDLINE_WIDTH);
      setColumnWidth(2, CREATIONDATE_WIDTH);
      setColumnWidth(3, PID_WIDTH);
    }
  };
  //</InnerClass>
  //----------------------------------------------------------------------

private:
  ProcessListView listv;

  SystemImageList  imagelist;

  StatusBar  statusbar;
  FileDialog fileDialog;

  DWORD watcherThreadId;

  FileVersionDialog fileVersion;

public:
  /**
   * Constructor
   */
  SolProcessWatcher(Application& applet, const TCHAR* name, Args& args)
  :ApplicationView(applet, name, 
      args.set(XmNbackground, (ulong)(COLOR_BTNFACE+1)))    
  {
    //Create a window of SOL::ListView.
    Args ar;
    ar.reset();
    ar.set(XmNexStyle, (ulong)WS_EX_CLIENTEDGE);
    ar.set(XmNstyle, (ulong)LVS_REPORT//|LVS_SORTASCENDING
          |LVS_NOLABELWRAP|LVS_SHOWSELALWAYS
          |LVS_SHAREIMAGELISTS);
    listv.create(this, NULL, ar);

    this->watcherThreadId = 0;

    ar.reset();
    statusbar.create(this, NULL, ar);
    statusbar.setText(_T("SolProcessWatcher is ready! Please select [Start] item from Watcher menu."));

    ar.reset();
    ar.set(XmNaccessMode, FileDialog::OPEN);
    fileDialog.create(this, NULL, ar);

    fileVersion.create(this);

    addCallback(XmNmenuCallback, IDM_CLEAR, this, 
      (Callback)&SolProcessWatcher::clear, NULL);
    addCallback(XmNmenuCallback, IDM_EXIT, this, 
      (Callback)&SolProcessWatcher::exit, NULL);

    addCallback(XmNmenuCallback, ID_WATCHER_START, this, 
      (Callback)&SolProcessWatcher::start, NULL);
    addCallback(XmNmenuCallback, ID_WATCHER_STOP, this, 
      (Callback)&SolProcessWatcher::stop, NULL);

    addCallback(XmNmenuCallback, IDM_VERSION, this, 
      (Callback)&SolProcessWatcher::version, NULL);

    addEventHandler(WM_SIZE, this, 
      (Handler)&SolProcessWatcher::size, NULL);

    addEventHandler(WM_CLOSE, this, 
      (Handler)&SolProcessWatcher::close, NULL);

    HICON hIcon = loadIcon(IDI_SOLPROCESSWATCHER);
    send(WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
    send(WM_SETICON, ICON_BIG, (LPARAM)hIcon);

    restorePlacement();
  }

public:
  ~SolProcessWatcher()
  {
  }


public:
  long size(Event& event)
  {
    LPARAM l = event.getLParam();
    
    statusbar.send(WM_SIZE, event.getWParam(), event.getLParam());
    int w, h;
    statusbar.getSize(w, h);
    
    listv.reshape(0, 0, w, HIWORD(l) - h);

    return 0;
  }

public:
  long close(Event& event)
  {
    if (watcherThreadId>0) {
      PostThreadMessage(watcherThreadId, WM_THREAD_CANCEL, 0, 0);
    }
    savePlacement();
    return defaultProc(event);
  }

public:
  /**
   * Clear Listview
   */
  void clear(Action& action)
  {
    listv.clear();
  }

public:
  /**
   *Create a thread of SolProcessCreationWatcher and run it.
   */
  void start(Action& action)
  {
    listv.clear();

    SolProcessWatcherThread* thread = new SolProcessWatcherThread(listv, imagelist);
    this->watcherThreadId = thread->getThreadId();
    thread->start();
    statusbar.setText(_T("Started a ProcessCreationWatcher Thread."));
  }

public:
  /**
   *Stop a thread of SolProcessCreationWatcher.
   */
  void stop(Action& action)
  {
    if (watcherThreadId !=0) {
      PostThreadMessage(watcherThreadId, WM_THREAD_CANCEL, 0, 0);
      watcherThreadId = 0;
      statusbar.setText(_T("Stopped"));

    }
  }


public:
  /**
   * File menu [Version] callback.
   */
  void version(Action& action)
  {
    fileVersion.popupAt(action);
  }

};

}

//  Program entry point.
void  Main(int argc, TCHAR** argv)
{
  COMInitializer comInitializer(COINIT_MULTITHREADED);
  
  ModuleFileName module(argv[0]);
  const TCHAR* name = module.getFileName();

  try {
    //
    comInitializer.setDefaultSecurityLevels();

    const String appClass = "SolProcessWatcher";
    Application applet(name, argc, argv);

    Args args;
  
    SolProcessWatcher watcher(applet, name, args);
    watcher.realize();

    applet.run();

  } catch (Exception& ex) {
    caught(ex);
  } catch (...) {
    caught(UnknownException());
  }
}


Last modified: 1 Feb 2017

Copyright (c) 2017 Antillia.com ALL RIGHTS RESERVED.