Sign in to follow this  

Help with c++ windows

This topic is 2954 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <windows.h>
#include "resource.h"


const char g_szClassName[] = "myWindowClass";


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
             DestroyWindow(hwnd);
             break;
        case WM_DESTROY:
             PostQuitMessage(0);
             break;
        case WM_COMMAND:
             switch(LOWORD(wParam))
             {
                case ID_FILE_EXIT:
                     PostMessage(hwnd, WM_CLOSE, 0, 0);
                     break;
                case ID_CAP_WINDOW:
                     break;    
                     }
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    HWND wChild;
    MSG Msg;
    MSG Msg2;


    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = NULL;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = NULL;


    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }


    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Key",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 500, 450,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
       

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
      
    
    return Msg.wParam;
}
[/source



With the resource file, this creates a window w/ menu...Pull down file menu. I want "ID_CAP_WINDOW" to create and show a child window when the user clicks on that menu option. I've tried so many things but it keeps giving me errors that "wChild" is undeclared whenever I try to call CreateWindow() inside the switch. I tried putting it under WM_CREATE too...doesn't work. I don't understand how it recognizes "hwnd" in PostMessage() but not wChild. What's the deal? *PS* how do I add formatting to my code in these forums? I don't see a menu anywhere. [Edited by - OneThreeThreeSeven on November 11, 2009 10:06:20 PM]

Share this post


Link to post
Share on other sites
I assume what Bitshifter was trying to say, is that te variable "wChild" is out of scope for the "WndProc" function. Since it's only declared in "WinMain".

Meaning "WndProc" does not know about "wChild" since it's not defined in "WndProc" nor is it a global variable, nor did it "wChild" get passed to "WndProc".

Also:
Quote:
Original post by OneThreeThreeSeven
I don't understand how it recognizes "hwnd" in PostMessage() but not wChild. What's the deal?


That is because "WndProc" is the windows procedure of "hWnd", and hWnd basically passes it's own handle to that function, hence the "WndProc" knows about "hWnd".

Share this post


Link to post
Share on other sites
Quote:
Original post by OneThreeThreeSeven

I don't understand how it recognizes "hwnd" in PostMessage() but not wChild. What's the deal?


Your are using the hwnd passed into WndProc() and not the hwnd that you declared in WinMain().

Maybe you could just make the HWND global or in a namespace?

namespace MyWindows
{
HWND hWnd = NULL;
HWND wChild = NULL;
};

Share this post


Link to post
Share on other sites
Thx alot. I knew it was a scope problem. ;o

But hey, I was looking at some sites on making buttons


case WM_CREATE:
{
CreateWindow(TEXT("button"), TEXT("Show Title"),
WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
20, 20, 185, 35,
hwnd, (HMENU) 1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
CheckDlgButton(hwnd, 1, BST_CHECKED);
break;
}



I noticed that they used


((LPCREATESTRUCT)lParam)->hInstance



To get the instance. But exactly what does that mean?

Share this post


Link to post
Share on other sites
Quote:
Original post by OneThreeThreeSeven
I noticed that they used

((LPCREATESTRUCT)lParam)->hInstance

To get the instance. But exactly what does that mean?

This is basically a shorter version for something like this:
LPCREATESTRUCT pTemp = (LPCREATESTRUCT)lParam;
pTemp->hInstance;


So, it's just a cast and accessing the member variable of a pointer combined.

Also, it's quite obvious that you don't really know a lot about C++. I really recommend you to make sure you know your language inside out (or at LEAST know the most important syntax), before you try to move on and program something bigger, or you will hit a lot more obstacles on your way.

Share this post


Link to post
Share on other sites
I finally got a window to popup with it's own menu and it sends and displays typed strings back to the "parent window"..isn't that what you call it? O_o Anyways...it took like a week simply to do something like that. So Screw c++ for now. ;)

I decided to learn Visual Basic. I've been trying to do c++ on and off for like 2 years now lol...it's a joke. The problem is that there's SO much stuff you have to learn the in's and out's of. MSDN is not clear with their desriptions of things either.

time is money and ohhh...how I have wasted so much time. :p

Share this post


Link to post
Share on other sites

This topic is 2954 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this