Monitor window title

2 comments

This is some BORLAND C++ Builder code to get the title of the currently focussed window on your desktop. I use this with a timer to track what I'm doing during the day to a log file. If you want a copy of the full application, please email me.


  HWND topWindow = GetForegroundWindow();
  HWND holdWindow;
  for(;topWindow!=NULL;)
  {
    holdWindow = topWindow;
    topWindow = GetParent(topWindow);
  }
  topWindow = holdWindow;
  if(topWindow!=NULL)
  {
    int textLen = GetWindowTextLength(topWindow);  // handle of window or control with text);
    LPTSTR windowTitleTmp = (LPTSTR)malloc(sizeof(char)*(textLen+5));
    int numChars = GetWindowText(
      topWindow, // handle of window or control with text
      windowTitleTmp,   // address of buffer for text
      textLen+1     // maximum number of characters to copy
      );
    AnsiString windowTitle = AnsiString(windowTitleTmp,textLen);
    free(windowTitleTmp);
    AnsiString Result="";
    TDateTime now = (new TDateTime())->CurrentDateTime();
    DateTimeToString(Result, "dd mmm yyyy - hh:mmam/pm:", now);
    if(lastLine != windowTitle)
    {
      lastLine = windowTitle;
      windowTitle = Result + " " + windowTitle;
      printf("windowTitle[%s]", windowTitle);
      this->Memo1->Lines->Insert(0,windowTitle);
      WriteLine(windowTitle);
    }
  }else
  {
    DWORD lastError = GetLastError();
    printf("lastError[%d]", lastError);
  }

Comments


Leave a Comment