[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1791
  • Last Modified:

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?
0
Troudeloup
Asked:
Troudeloup
  • 18
  • 13
1 Solution
 
jkrCommented:
Hm, that should not happen. How are you using that?
0
 
TroudeloupAuthor Commented:
#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
 
TroudeloupAuthor Commented:
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
jkrCommented:
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
 
TroudeloupAuthor Commented:
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
 
jkrCommented:
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
 
TroudeloupAuthor Commented:
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
 
TroudeloupAuthor Commented:
yep i have exactly this

SetForegroundWindow(*i);


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

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

SW_HIDE

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

cool, but useless.
0
 
jkrCommented:
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
 
TroudeloupAuthor Commented:
what does it do?    O_o
0
 
jkrCommented:
It places the 1st window in the back, waits 5s and then brings it to the front again.
0
 
TroudeloupAuthor Commented:
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
 
jkrCommented:
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
 
TroudeloupAuthor Commented:
           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
 
jkrCommented:
Um, sorry, - use

SetActiveWindow(*i);

to make it 'pale'.
0
 
TroudeloupAuthor Commented:
uh, i don't get it.

I want to make it blue.
0
 
jkrCommented:
Well, whatever color - give it a try ;o)
0
 
TroudeloupAuthor Commented:
I think it works for now :D
0
 
jkrCommented:
Great ;o)
0
 
TroudeloupAuthor Commented:
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
 
jkrCommented:
Sure, just one moment!
0
 
jkrCommented:
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
 
TroudeloupAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 18
  • 13
Tackle projects and never again get stuck behind a technical roadblock.
Join Now