Link to home
Start Free TrialLog in
Avatar of Troudeloup
Troudeloup

asked on

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

when run with both of these, it crashed the window of hwnd.


given hwnd, how do I activate a window?
Avatar of jkr
jkr
Flag of Germany image

Hm, that should not happen. How are you using that?
Avatar of Troudeloup
Troudeloup

ASKER

#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;
}
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...
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.
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.
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);
      }


yep i have exactly this

SetForegroundWindow(*i);


let me run it a few times so I can describe what happens with it directly.
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.
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.
Well, 'ShowWindow()' only affects the show state. Have you tried e.g.

ShowWindow(hwnd1, SW_MINIMIZE);
Sleep ( 5000 );
ShowWindow(hwnd1, SW_RESTORE);
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?
oh i should try hide
ok

SW_HIDE

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

cool, but useless.
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);

what does it do?    O_o
It places the 1st window in the back, waits 5s and then brings it to the front again.
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.

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);

?
           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);
Um, sorry, - use

SetActiveWindow(*i);

to make it 'pale'.
uh, i don't get it.

I want to make it blue.
Well, whatever color - give it a try ;o)
I think it works for now :D
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Sure, just one moment!
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);
https://www.experts-exchange.com/questions/22983831/how-do-I-save-a-screen-shot.html


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



nooooo!  one liner!