//---------------------------------------------------------------------------
#include <vcl.h>
#include <Clipbrd.hpp>
#pragma hdrstop
#include "Unit1.h"
#include "jpeg.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  RECT rec;
  HWND hwnd;
  hwnd=::FindWindow("Shell_TrayWnd",NULL);
  ::GetWindowRect(hwnd,&rec);
  Top=rec.top-Height;
  Left=rec.right-Width;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N3Click(TObject *Sender)
{
  Close();  
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
  if(N5->Checked) ::UnregisterHotKey(Handle,MYWM_ID);
  DelIcon();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormActivate(TObject *Sender)
{
  AddIcon();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N1Click(TObject *Sender)
{
  Graphics::TBitmap *ImageBmp=new Graphics::TBitmap;
  DWORD bt;
  bt=GetTickCount();
  do {
    Application->ProcessMessages();
  } while (GetTickCount()-bt<1000);
  HDC ScrDC;
  TRect R;
  RECT r;
  HWND hwnd;
  hwnd=::FindWindow("Shell_TrayWnd",NULL);
  ::GetWindowRect(hwnd,&r);
  HWND Wnd=GetDesktopWindow();
  ScrDC=GetWindowDC(Wnd);
  GetWindowRect(Wnd, &R);
  ImageBmp->Width = R.right;
  ImageBmp->Height = R.bottom;
  BitBlt(ImageBmp->Canvas->Handle, 0, 0, R.right, R.bottom, ScrDC, 0, 0, SRCCOPY);
  ReleaseDC(Wnd, ScrDC);
  if(N4->Checked){
    if (SavePictureDialog1->Execute()){
      TJPEGImage *TheJPEG;
      TheJPEG = new TJPEGImage;
      TheJPEG->Assign(ImageBmp);
      TheJPEG->SaveToFile(SavePictureDialog1->FileName);
      delete TheJPEG;
    }
  } else Clipboard()->Assign(ImageBmp);
  delete ImageBmp;
}
//---------------------------------------------------------------------------
void TForm1::AddIcon()
{
  ezScrCap.cbSize=sizeof(NOTIFYICONDATA);
  ezScrCap.hWnd=Handle;
  ezScrCap.uID=(UINT)MYNOTIFYICONID;
  ezScrCap.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
  ezScrCap.uCallbackMessage=MYWM_NOTIFYICON;
  ezScrCap.hIcon=LoadIcon(HInstance,"MAINICON");
  strcpy(ezScrCap.szTip,this->Caption.c_str());
  Shell_NotifyIcon(NIM_ADD,&ezScrCap);
  ShowWindow(Application->Handle,SW_HIDE);
  ShowWindow(Handle,SW_HIDE);
  SetWindowText(Application->Handle,this->Caption.c_str());
}
//---------------------------------------------------------------------------
void TForm1:

elIcon()
{
  ezScrCap.cbSize=sizeof(NOTIFYICONDATA);
  ezScrCap.hWnd=Handle;
  ezScrCap.uID=(UINT)MYNOTIFYICONID;
  ezScrCap.uFlags=0;
  ezScrCap.uCallbackMessage=MYWM_NOTIFYICON;
  ezScrCap.hIcon=0;
  strcpy(ezScrCap.szTip,this->Caption.c_str());
  Shell_NotifyIcon(NIM_DELETE,&ezScrCap);
  ShowWindow(Application->Handle,SW_NORMAL);
  ShowWindow(Handle,SW_NORMAL);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TRAYCALLBACK(TMessage& Message)
{
  POINT MousePos;
  switch(Message.LParam)
  {
    case WM_LBUTTONDBLCLK:
      break;
    case WM_LBUTTONUP:
    case WM_RBUTTONUP:
      if (GetCursorPos(&MousePos))
      {
        PopupMenu1->PopupComponent = Form1;
        SetForegroundWindow(Handle);
        PopupMenu1->Popup(MousePos.x, MousePos.y);
      }
      break;
    default:
      break;
  }
  TForm:

ispatch(&Message);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N10Click(TObject *Sender)
{
/*******************************************************************************************************************+
| CF_TEXT		Text with a CR-LF combination at the end of each line. A null character identifies the end of the text. |
| CF_BITMAP	A Windows bitmap graphic.                                                                               |
| CF_METAFILEPICT	A Windows metafile graphic.                                                                       |
| CF_PICTURE	An object of type TPicture.                                                                           |
| CF_COMPONENT	Any persistent object.                                                                              |
+*******************************************************************************************************************/
  if(Clipboard()->HasFormat(CF_BITMAP)) {
    if (SavePictureDialog1->Execute()){
      TJPEGImage *TheJPEG;
      TheJPEG = new TJPEGImage;
      TheJPEG->Assign(Clipboard());
      TheJPEG->SaveToFile(SavePictureDialog1->FileName);
      delete TheJPEG;
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N11Click(TObject *Sender)
{
  TJPEGImage *TheJPEG;
  if (OpenPictureDialog1->Execute()){
    TheJPEG= new TJPEGImage;
    TheJPEG->LoadFromFile(OpenPictureDialog1->FileName);
    Clipboard()->Assign(TheJPEG);
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N5Click(TObject *Sender)
{
  if(N5->Checked)
    ::RegisterHotKey(Handle,MYWM_ID,NULL,VK_F7);  //NULL, MOD_ALT, MOD_CONTROL, MOD_NOREPEAT, MOD_SHIFT, MOD_WIN
  else
    ::UnregisterHotKey(Handle,MYWM_ID);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WMHotKey(TMessage& Message)
{
  Graphics::TBitmap *ImageBmp=new Graphics::TBitmap;
//    DWORD bt;
//    bt=GetTickCount();
//    do {
//      Application->ProcessMessages();
//    } while (GetTickCount()-bt<3000);
  HDC ScrDC;
  TRect R;
  HWND Wnd=GetDesktopWindow();
  ScrDC=GetWindowDC(Wnd);
  GetWindowRect(Wnd, &R);
//  MapWindowPoints(HWND_DESKTOP, Wnd, (PPOINT)&R, 2);
//  OffsetRect(&R, -R.left, -R.top);
  ImageBmp->Width = R.right;
  if(N14->Checked){
    ImageBmp->Height = R.bottom;
  } else {
    RECT r;
    HWND hwnd;
    hwnd=::FindWindow("Shell_TrayWnd",NULL);
    ::GetWindowRect(hwnd,&r);
    ImageBmp->Height = r.top;
  }
  BitBlt(ImageBmp->Canvas->Handle, 0, 0, R.right, R.bottom, ScrDC, 0, 0, SRCCOPY);
  if(N12->Checked) {
    CURSORINFO ci;
    ci.cbSize=sizeof(CURSORINFO);
    GetCursorInfo(&ci);
    POINT pt;
    GetCursorPos(&pt);
    DrawIcon(ImageBmp->Canvas->Handle, pt.x, pt.y, ci.hCursor);
  }
  ReleaseDC(Wnd, ScrDC);
  if(N13->Checked){
    if (SavePictureDialog1->Execute()){
      TJPEGImage *TheJPEG;
      TheJPEG = new TJPEGImage;
      TheJPEG->Assign(ImageBmp);
      TheJPEG->SaveToFile(SavePictureDialog1->FileName);
      delete TheJPEG;
      ::ShellExecute(::GetDesktopWindow(), "Open", SavePictureDialog1->FileName.c_str(), NULL, NULL, SW_SHOWNORMAL);
    }
  } else Clipboard()->Assign(ImageBmp);
  delete ImageBmp;
  TForm:

ispatch(&Message);
}
//---------------------------------------------------------------------------