Solved

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

Posted on 2007-11-27
31
1,750 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

770 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