Thread: Basic Loop Question

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    7

    Basic Loop Question

    Alright well i must say.. i havent really touched any cpp for a year or two, untill now. This is a snippet from a larger peice which gets the name of the active window.

    I only want it to write the name of the active window, if the active window has changed since the last write. I cant seem to find where to put the loop (rusty i know)

    I started with this
    Code:
    void saveCaption(HWND hwnd, TCHAR *filename) {
      int length = GetWindowTextLength( hwnd ) + 1;
      TCHAR *caption = new TCHAR[length];
    
      GetWindowText( hwnd, caption, length );
    
      ofstream myfile;
      myfile.open (filename, ios::app | ios::out);
      myfile << caption << "\n"  ;
      myfile.close();
    
    }
    
    
    int main() {
    
    
    
      saveCaption( GetForegroundWindow(), TEXT("caption.txt") );
    
        }
    Then added in a while loop, so its constantly checking the window instead of checking it once and exiting

    Code:
    void saveCaption(HWND hwnd, TCHAR *filename) {
      int length = GetWindowTextLength( hwnd ) + 1;
      TCHAR *caption = new TCHAR[length];
    
      GetWindowText( hwnd, caption, length );
    
      ofstream myfile;
      myfile.open (filename, ios::app | ios::out);
      myfile << caption << "\n"  ;
      myfile.close();
    
    }
    
    
    int main() {
        while (1) {
    
    
      saveCaption( GetForegroundWindow(), TEXT("caption.txt") );
      Sleep(50);
        }}
    This works to some degree but its constantly writing the same thing over and over again which seems redundant. eg ( it'd write "main.cpp - Notepad" 50 times over untill the window focus is changed and writes the name of the next window 50 times over).



    I cant figure out how to compare the name of the window in the previous loop and the current variable content.

    Anyway, im really rusty so any help would be appreciated.

    Cheers, kp.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I am probably reading your question wrong, but it seems you only want it to save the caption once per focus change. In this case use a holder to check against the currently active window.

    example
    Code:
    void saveCaption(HWND hwnd, TCHAR *filename) {
      static HWND LastWindow = 0;
      if ( hwnd != LastWindow )
      {
        LastWindow = hwnd;
    
    
      int length = GetWindowTextLength( hwnd ) + 1;
      TCHAR *caption = new TCHAR[length];
    
      GetWindowText( hwnd, caption, length );
    
      ofstream myfile;
      myfile.open (filename, ios::app | ios::out);
      myfile << caption << "\n"  ;
      myfile.close();
    }
    
    }
    Like I said I may be misunderstanding you completely.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  3. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM