CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2005
    Location
    India
    Posts
    238

    Angry Window creation in C++

    can we create a window without using-->
    Code:
    int APIENTRY WinMain(HINSTANCE hinstance,
    HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdshow)
    right now i m creating a window using CreateWindow(,,,,) but in this we hav to pass a register class which takes the HINSTANSE of the WinMian function.
    I want to know if we can create a window without using this as i have to work on C++ not on Win32 ie
    Code:
    void main (void)
    {
    //here i want to create a window
    }
    Virender

  2. #2
    Join Date
    Nov 2004
    Location
    Pakistan
    Posts
    466

    Arrow Re: Window creation in C++

    Actually, as far I understand there must be a main loop where you will check the messages comming from windows, and those are tracked in WinMain, secondly each window has its own windows procedure which controls messages of specific window individually.

    You may create a window in main like this

    Code:
    #include <windows.h>
    
    HWND myWin;
    
    int main(int argc, char *argv[])
    {
    
    	myWin = CreateWindow(
    		WC_DIALOG,"My Window",
    		WS_OVERLAPPEDWINDOW,
         		100,100,400,300,
    		HWND_DESKTOP,
    		NULL,
    		NULL,
    		NULL
    	);
    
    	ShowWindow(myWin, SW_SHOW);
    
    	//do whatsoever is nedded
    
    	DestroyWindow(myWin);
    	return 0;
    }
    But problem is where are you going to track the messages. there are hundreds of messages which are sent/processed by windows.

    If any guru gets to this thread please implement something for this fellow by replacing '//do whatsoever is nedded' with some message map.

    I am also interested in knowing the implementation of custom message map rather than using the default method.


    I hope it was of a little 0.01% help.

    regards
    » Please 'Rate This Post' if it helped (encourage us to help you more)
    » Build GUI in minute using rad c++
    » Free IDE + GUI code generator - screenshot
    » Free WINAPI sourcecode and tutorials

  3. #3
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: Window creation in C++

    Quote Originally Posted by Ali Imran
    If any guru gets to this thread please implement something for this fellow by replacing '//do whatsoever is nedded' with some message map.
    Code:
    MSG msg =  {0};
    
    while ( GetMessage(&msg, myWin, 0, 0) )
    {
       TranslateMessage(&msg);
       DispatchMessage(&msg);
    }
    Last edited by NoHero; February 28th, 2006 at 07:12 AM. Reason: typo in the code
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Window creation in C++

    Quote Originally Posted by Ali Imran
    But problem is where are you going to track the messages. there are hundreds of messages which are sent/processed by windows.
    Yes, indeed there are many hundreds of messages, but luckily we have not to process all of them and just send "not interesting" ones to default window procedure (by a call of DefWindowProc).
    Quote Originally Posted by Ali Imran
    If any guru gets to this thread please implement something for this fellow by replacing '//do whatsoever is nedded' with some message map.
    Not a place for "message map" there. Maybe a call of SetWindowLong to assign a window procedure followed by a message loop.
    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
    {
       switch(nMsg)
       {
       // ...
       // ...
       case WM_DESTROY:
          PostQuitMessage(0);
          break;
       default:
          return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
       }
       return TRUE;
       
    }
    
    int main(int argc, char* argv[])
    {
       HWND hWnd = CreateWindow(WC_DIALOG,
                                "My Window",
                                WS_OVERLAPPEDWINDOW|WS_VISIBLE,
                                100,100,400,300,
                                HWND_DESKTOP,
                                NULL,
                                NULL,
                                NULL);
       
       ::SetWindowLong(hWnd, GWL_WNDPROC, (LONG)WndProc);
       ::UpdateWindow(hWnd);
       
       MSG msg;
       
       while(GetMessage(&msg, NULL, 0, 0)) 
       {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
       }
       return 0;
    }
    However, I cannot guarantee you'll have not headaches developing the code above and I do not recommend it.
    Once you suggested a dialog, much more easier and correct is to call DialogBox or CreateDialog for making a modal or modeless dialog, respectively.
    Quote Originally Posted by Ali Imran
    I am also interested in knowing the implementation of custom message map rather than using the default method.
    PS. Please, explain what do you mean with "custom message map" and "default method".
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: Window creation in C++

    You can also handle specific messages that you need to handle(sent in the message loop) with a WindowProcedure.

    Code:
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        PAINTSTRUCT ps;
        HDC hdc;
    
        switch(msg)
        {
        case WM_PAINT:
            {
                hdc = BeginPaint(hWnd, &ps);
                EndPaint(hWnd, &ps);
                return(0);
            }break;
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return(0);
            }break;
        default:break;
        }
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    This returns DefWindowProc that handles the rest of the messages with their defaults. LRESULT is a typedef for long and CALLBACK is a typedef for __stdcall.
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

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