Solved

ShowWindow(hwnd , SW_SHOWNORMAL)  +        SetActiveWindow(hwnd);

Posted on 2007-11-27
31
1,764 Views
Last Modified: 2008-02-01
when run with both of these, it crashed the window of hwnd.


given hwnd, how do I activate a window?
0
Comment
Question by:Troudeloup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 18
  • 13
31 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 20360328
Hm, that should not happen. How are you using that?
0
 

Author Comment

by:Troudeloup
ID: 20360375
#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;

vector<HWND> vw; // array for HWND

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
      char buf [1024];
      GetWindowText(hwnd,buf,1024);

      if(!strstr(buf,(char*)lParam)) return true;

       vw.push_back(hwnd);

      return true;
}

int main()
{
      EnumWindows(EnumWindowsProc,(LPARAM)"notepad");

            
      
      for (vector<HWND>::iterator i = vw.begin(); i != vw.end(); ++i)
      {
            cout << "HWND: " << *i << endl;
            ShowWindow(*i , SW_SHOWNORMAL);
            SetActiveWindow(*i);
      }

   return 0;
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 20360408
Well, using

      for (vector<HWND>::iterator i = vw.begin(); i != vw.end(); ++i)
      {
            cout << "HWND: " << *i << endl;
            ShowWindow(*i , SW_SHOWNORMAL);
            SetForegroundWindow(*i);
      }

that works fine here...
0
 

Author Comment

by:Troudeloup
ID: 20360574
i get what's going on now.

it doesn't crash them, in fact, what it does is to hide one of them, and, in fact, it even hides it from the taskbar.


if I run it again, the one that's showing from the run before would disppear and then the previsouly hidden one will pop up.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20360591
That's very odd. I ran that here, and it works as expected - it brings the windows to the front and does not affect the task bar or anything. Note that I used 'SetForegroundWindow(*i);', though.
0
 

Author Comment

by:Troudeloup
ID: 20360596
I think i got a few terms confused here:

focus
gain focus
show
activate.


i think they all mean different things,

yet this is what i want to do, in english:

I am working only with top level windows, and I have multiple of them.
I need one thing that brings one of them to the front, so I can work with it,
and when I use this one thing again, given a different handle, it will bring another one to the front.


does it make sense?

this is doing funny things, i am not sure what it's doing, actually.



      for (vector<HWND>::iterator i = vw.begin(); i != vw.end(); ++i)
      {
            cout << "HWND: " << *i << endl;
            ShowWindow(*i , SW_SHOWNORMAL);
            SetForegroundWindow(*i);
      }


0
 

Author Comment

by:Troudeloup
ID: 20360606
yep i have exactly this

SetForegroundWindow(*i);


let me run it a few times so I can describe what happens with it directly.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20360620
Try

      for (vector<HWND>::iterator i = vw.begin(); i != vw.end(); ++i)
      {
            cout << "HWND: " << *i << endl;
            Sleep(2000);
            ShowWindow(*i , SW_SHOWNORMAL);
            SetForegroundWindow(*i);
      }

to better recognize the effect.
0
 

Author Comment

by:Troudeloup
ID: 20360673
ok i also tried a combination of things





#include <iostream>
#include <windows.h>
using namespace std;
int main ()
{

      
      HWND hwnd1 = (HWND)0x001B027E;
      HWND hwnd2 = (HWND)0x000403D6;

      SetForegroundWindow( hwnd1 );
      SetWindowPos(  hwnd1, HWND_TOPMOST, 250, 0, 0, 0, SWP_NOSIZE );
      //MoveWindow( hwnd1, 250, 0, 1030, 800, false );
            
      
      //SetActiveWindow( hwnd1 );
      
      Sleep ( 5000 );
      
      SetWindowPos(  hwnd1, HWND_NOTOPMOST, 250, 0, 0, 0, SWP_NOSIZE );
      
      //MoveWindow( hwnd2, 250, 0, 1030, 800, false );
      SetForegroundWindow( hwnd2 );
      SetWindowPos(  hwnd2, HWND_TOPMOST, 250, 0, 0, 0, SWP_NOSIZE );
      
      
      
      
      //SetActiveWindow( hwnd2 );
      
      
      
      
      SetWindowPos(  hwnd2, HWND_NOTOPMOST, 250, 0, 0, 0, SWP_NOSIZE );
      
      //Sleep ( 5000 );
      
      
      //MoveWindow( hwnd1, 250, 0, 1030, 800, false );
      //SetForegroundWindow( hwnd1 );      
      
      
      //SetActiveWindow( hwnd2 );
      
      
      
      return 0;
}

















is it possible to do the same thing, but without using showwindow?

I think that's what messing things up.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20360721
Well, 'ShowWindow()' only affects the show state. Have you tried e.g.

ShowWindow(hwnd1, SW_MINIMIZE);
Sleep ( 5000 );
ShowWindow(hwnd1, SW_RESTORE);
0
 

Author Comment

by:Troudeloup
ID: 20360770
oh you are good at this :D

both window are not back in the taskbar and they work normally.


however, minimizing is slow and it not practical for actual use,  but it's good to fix the strange thing.



I don't understand what's going on, and I am not sure if showwindow should be used at all, but this is what i want to do,


I am working only with top level windows, and I have multiple of them.
I need one thing that brings one of them to the front, so I can work with it,
and when I use this one thing again, given a different handle, it will bring another one to the front.




does it still make sense?
0
 

Author Comment

by:Troudeloup
ID: 20360790
oh i should try hide
0
 

Author Comment

by:Troudeloup
ID: 20360805
ok

SW_HIDE

would take it off the taskbar and restore put it back.

cool, but useless.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20360828
SW_HIDE would be another option, yes.

You could also try

SetWindowPos(hwnd1,HWND_BOTTOM,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
Sleep(5000);
SetWindowPos(hwnd1,HWND_TOPMOSZ,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);

0
 

Author Comment

by:Troudeloup
ID: 20360840
what does it do?    O_o
0
 
LVL 86

Expert Comment

by:jkr
ID: 20360862
It places the 1st window in the back, waits 5s and then brings it to the front again.
0
 

Author Comment

by:Troudeloup
ID: 20360998
SetWindowPos(hwnd1,HWND_BOTTOM,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
Sleep(5000);
SetWindowPos(hwnd1,HWND_TOPMOSZ,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);



ok, this would do that, but then it leaves them both covering over other windows, refusing go behind the c++ editor.




also, when it's brought to the front, it's still pale blue and not deep blue so i can use it.

0
 
LVL 86

Expert Comment

by:jkr
ID: 20361087
OK, then what about

SetWindowPos(hwnd1,HWND_BOTTOM,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_HIDE);
Sleep(5000);
SetWindowPos(hwnd1,HWND_TOPMOSZ,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
SetActiveWindow(hwnd1);

?
0
 

Author Comment

by:Troudeloup
ID: 20361158
           SetWindowPos(*i,HWND_TOP,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
              SetWindowPos(*i,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
              Sleep ( 2000 );
              SetWindowPos(*i, HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
            SetWindowPos(*i,HWND_BOTTOM,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);



this almost works, i just need the topmost window to turn blue from pale blue so I can edit it.


I already tried this one

            SetWindowPos(*i,HWND_TOP,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
              SetWindowPos(*i,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
                SetActiveWindow(*i);
              Sleep ( 2000 );
              SetWindowPos(*i, HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
            SetWindowPos(*i,HWND_BOTTOM,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
0
 
LVL 86

Expert Comment

by:jkr
ID: 20361181
Um, sorry, - use

SetActiveWindow(*i);

to make it 'pale'.
0
 

Author Comment

by:Troudeloup
ID: 20361196
uh, i don't get it.

I want to make it blue.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20361204
Well, whatever color - give it a try ;o)
0
 

Author Comment

by:Troudeloup
ID: 20361262
I think it works for now :D
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 20361283
Great ;o)
0
 

Author Comment

by:Troudeloup
ID: 20361368
will you also take a look of my thread about screenshot?

I had to ask that Q again because I forgot to mention I need to save it.

also (yes) you gave me that nice one liner, but then didn't give me the line to save it to .bmp.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20361569
Sure, just one moment!
0
 
LVL 86

Expert Comment

by:jkr
ID: 20362929
Sorry, dunno where your Q about saving file is located, but the code would be

        RECT rc;
        HDC hDC = GetDC(hwnd);
        BITMAPINFOHEADER BMIH;

        GetWindowRect(hwnd,&rc);

        BMIH.biSize = sizeof(BITMAPINFOHEADER);
        BMIH.biBitCount = 24;
        BMIH.biPlanes = 1;
        BMIH.biCompression = BI_RGB;
        BMIH.biWidth = rc.width;
        BMIH.biHeight = rc.height;
        BMIH.biSizeImage = ((((BMIH.biWidth * BMIH.biBitCount)
                           + 31) & ~31) >> 3) * BMIH.biHeight;
        HBITMAP hDrawingSurface = CreateDIBSection(hdc,
                 (CONST BITMAPINFO*)&BMIH, DIB_RGB_COLORS,
                 (void**)&pDrawingSurfaceBits, NULL, 0);
        ReleaseDC(hDC);
       
        //Create a new file for writing
        FILE *pFile = fopen("c;\\tmp.bmp", "wb");
        if(pFile == NULL)
        {
            AfxMessageBox(IDS_FILE_CREATE_ERROR_MESSAGE);
            return;
        }
        BITMAPFILEHEADER bmfh;
        int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize;
        LONG lImageSize = BMIH.biSizeImage;
        LONG lFileSize = nBitsOffset + lImageSize;
        bmfh.bfType = 'B'+('M'<<8);
        bmfh.bfOffBits = nBitsOffset;
        bmfh.bfSize = lFileSize;
        bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
        //Write the bitmap file header
        UINT nWrittenFileHeaderSize = fwrite(&bmfh, 1,
                     sizeof(BITMAPFILEHEADER), pFile);
        //And then the bitmap info header
        UINT nWrittenInfoHeaderSize = fwrite(&BMIH,
               1, sizeof(BITMAPINFOHEADER), pFile);
        //Finally, write the image data itself
        //-- the data represents our drawing
        UINT nWrittenDIBDataSize =
             fwrite(pDrawingSurfaceBits, 1, lImageSize, pFile);

        fclose(pFile);
0
 

Author Comment

by:Troudeloup
ID: 20363013
http://www.experts-exchange.com/Programming/Languages/CPP/Q_22983831.html


HDC hWindowDC = ::GetDC(hWnd);
GetPixel(   HDC hWindowDC, int x, int y )



nooooo!  one liner!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question