Solved

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

Posted on 2007-11-27
31
1,719 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
  • 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
 

Author Comment

by:Troudeloup
ID: 20360380
0
 

Author Comment

by:Troudeloup
ID: 20360391
0
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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
 

Author Comment

by:Troudeloup
ID: 20362031
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now