CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Little problem

  1. #1
    Join Date
    Nov 2007
    Posts
    51

    Little problem

    Hi!

    I am trying to make a Hello World program with little more features than usual.
    I wanted it to allow running of only one instance of the program. This part was
    OK. The problem came when I wanted it to hide/show itself with one key
    press. The program hides itself correctly and runs in background but I can't get it back active. Also, I don't want this program to occupy 50% of CPU
    Usage. What am I doing wrong?

    Here's the code:

    Code:
    #include <windows.h>
    #include <ctime>
    #include "Resource.h"
    
    HWND MainWindow = 0;
    
    
    bool InitializeApp(HINSTANCE hInstance, HWND hwnd, int nShowCmd);
    
    int Run();
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
    
    
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    	HANDLE hMutex = CreateMutex(NULL, FALSE, "APPLICATION_ID");
    	if (GetLastError() == ERROR_ALREADY_EXISTS)
    	{
    		::MessageBox(0, "Program is already running!", "Error", MB_OK);
    		return 0;
    	}
    
    	if(!InitializeApp(hInstance, MainWindow, nShowCmd))
    	{
    		::MessageBox(0, "Initialization Failed!", "Error", MB_OK);
    		return 0;
    	}
    
    	return Run();
    }
    
    
    
    
    bool InitializeApp(HINSTANCE hInstance, HWND hWnd, int nShowCmd)
    {
    	WNDCLASSEX wc;
    
    	wc.style		 = CS_HREDRAW | CS_VREDRAW;
    	wc.lpfnWndProc	 = WndProc;
    	wc.cbSize		 = sizeof(WNDCLASSEX);
    	wc.cbClsExtra	 = 0;
    	wc.cbWndExtra	 = 0;
    	wc.hInstance	 = hInstance;
    	wc.hIcon		 = ::LoadIcon(hInstance, MAKEINTRESOURCE(WINDOWICON));
    	wc.hIconSm		 = ::LoadIcon(hInstance, MAKEINTRESOURCE(WINDOWICON));
    	wc.hCursor		 = ::LoadCursor(0, IDC_ARROW);
    	wc.hbrBackground = static_cast<HBRUSH>(::GetStockObject(WHITE_BRUSH));
    	wc.lpszMenuName  = 0;
    	wc.lpszClassName = "HWC"; // Hello World Class
    
    	if(!::RegisterClassEx(&wc))
    	{
    		::MessageBox(0, "WNDCLASS registration failed!", "Error", 0);
    		return false;
    	}
    
    	UINT XPOS = (::GetSystemMetrics(SM_CXSCREEN) - WIDTH)  / 2;
    	UINT YPOS = (::GetSystemMetrics(SM_CYSCREEN) - HEIGHT) / 2;
    
    
    
        MainWindow = ::CreateWindow(
    				   "HWC",
    				   "Hello, World!",
    				   WS_OVERLAPPEDWINDOW,
    				   XPOS,						// Place window in the middle of the screen
    				   YPOS,
    				   WIDTH,
    				   HEIGHT,
    				   0,
    				   0,
    				   hInstance,
    				   0);
    
    	if(MainWindow == 0)
    	{
    		::MessageBox(0, "Window creation failed!", 0, 0);
    		return false;
    	}
    
    	::ShowWindow(MainWindow, nShowCmd);
    	::UpdateWindow(MainWindow);
    
    	return true;
    }
    
    
    int Run()
    {
    	MSG msg;
    	::ZeroMemory(&msg, sizeof(MSG));
    
    	while(TRUE)
    	{
    		long int startpoint = ::GetTickCount();
    
    		if(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    		{
    			if(msg.message == WM_QUIT)
    				break;
    			if(msg.message == WM_KEYDOWN)
    			{
    				if(msg.wParam == VK_DELETE)
    				{
    					::ShowWindow(MainWindow, SW_HIDE);
    					::UpdateWindow(MainWindow);
    				}
    				if(msg.wParam == VK_SPACE)
    				{
    					::ShowWindow(MainWindow, SW_SHOW);
    					::UpdateWindow(MainWindow);
    				}
    				if(msg.wParam == VK_ESCAPE)
    				{
    					::DestroyWindow(MainWindow);
    				}
    			}
    		
    			::TranslateMessage(&msg);
    			::DispatchMessage(&msg);
    		}
    
    		while((::GetTickCount() - startpoint) < 25);
    	}
    
    	return msg.wParam;
    }
    
    
    
    LRESULT CALLBACK WndProc(HWND	hWnd, UINT	msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    	case WM_LBUTTONDOWN:
    		::MessageBox(0, "Hello, World!", "Hello!", MB_OK);
    		return 0;
    
    	case WM_DESTROY:
    		::PostQuitMessage(0);
    		return 0;
    	}
    	
    	return ::DefWindowProc(hWnd, msg, wParam, lParam);
    }
    PS. Don't mind the Resource header, it's for the icons that work fine.
    Last edited by Jpsarapuu; July 15th, 2008 at 12:23 PM.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Little problem

    Regarding the 50% CPU usage, your Run consume 100% thread time due to that while(TRUE) statement (50% since you have a dual core).

    Regarding the "restore to active", you don't get a any WM_KEYDOWN messages while minimized.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Nov 2007
    Posts
    51

    Re: Little problem

    Thank you. That explains a lot.

    How would my program be able to receive input while minimized?

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Little problem

    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured