Help with displaying the programs name in the window?

I am making a program where it shows the programs name in the middle of the window as static text, and I got it to work in a messagebox, but I cant get it in the window as static text. Here is the switch(msg):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
HWND txt;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
    switch(msg){
            case WM_CREATE:
            {
                HINSTANCE hInstance = GetModuleHandle(NULL);
                char sz_FileName[MAX_PATH];
                GetModuleFileName(hInstance, sz_FileName, MAX_PATH);
                txt = CreateWindow("STATIC",
                             sz_FileName,
                             WS_VISIBLE | WS_CHILD,
                             100, 100, 300, 50,
                             hwnd, NULL, NULL, NULL);
            }
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(NULL);
            break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}


So, whenever I compile it, no errors come up, but the window doesnt show up. When I exclude the WM_CREATE: case it works, but with it it doesn't. Any help?
I wouldn't expect it to work. There is no break statement to close the WM_CREATE handler, so it immediately falls through to DestroyWindow().

After you fix that it might work. Why not try using TextOut() to draw the program name in sz_FileName to your window?

Also, typically, if one doesn't need the HWND of the label, it is customary to just use (HMENU)-1 for the menu handle/child id parameter.
Last edited on
OH! Oh wow. Wow. Silly mistake. Thanks for pointing that out haha. Thanks about the other tips too. Wow I am like in awe right now hahahahahaha
Done the same myself already. If you take up my suggestion about TextOut(), that needs to go in a WM_PAINT handler, as there you'll easily be able to get an HDC, and put the TextOut() between BeginPaint() and EndPaint().
Okay; I can try that. I will do it when the book I am reading gets to WM_PAINT. Thanks.
closed account (3pj6b7Xj)
Don't forget that when you create a window, you must also "SHOW" the window what ever it is that you created.

I assume your windows code already created a window as you need to associate a winproc with it, the right way to do this is to catch WM_PAINT create, get the window handle and draw to the window......here is an example of mine...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        case WM_PAINT: // What to paint on the window.
        {
            PAINTSTRUCT ps; // Our paint structure.
            HDC hDC=BeginPaint(hWnd,&ps); // Start paint scene.

            // Display the file in the client area of window.
            SetBkColor(hDC,pView->Background);
            string Space, Text; // String objects.
            for(UINT i=0,iRow=10;i<pView->Height;i++,iRow+=18)
            {
                // If the string is less than the View.FileWidth, add a space.
                if(pView->Text[pView->Offset_Y+i].length()<pView->FileWidth)
                    Space.assign(pView->FileWidth-pView->Text[pView->Offset_Y+i].length(),' ');
                // Note: The only reason I add a space is to prevent left overs from the
                // previous lines since I am not redrawing the window to prevent flicker.

                // The following assign operation takes the string at at the Y offset
                // location starting from Offset_X and up to View.Width. This is what
                // allows scrolling left and right using Offset_X (^_^)
                Text.assign(pView->Text[pView->Offset_Y+i]+Space,pView->Offset_X,pView->Width);
                // Note: Text is not the same as View.Text, these two are different!

                // Display the data in the client window.
                TxtOut(hDC,5,iRow,Text.c_str(),pView->Foreground);

                // This helps identify strings that are bigger in width than the
                // window view width. A "»" is drawn to tell the user there is more.
                if(pView->Text[pView->Offset_Y+i].length()-pView->Offset_X==pView->Width||
                   pView->Text[pView->Offset_Y+i].length()-pView->Offset_X<=pView->Width||
                   pView->Text[pView->Offset_Y+i].length()<pView->Width)
                // I normally don't use bracket enclosures for a single line of code
                // after an "if" statement but since this example already looks intense,
                // the brackets help to read the code a bit.
                {
                    TxtOut(hDC,pView->Width+690,iRow," ",RGB(0,0,200));
                }
                else // Draw the little arrows.
                {
                    TxtOut(hDC,pView->Width+690,iRow,"»",RGB(255,255,0));
                }

                // Clear the strings.
                Space.clear();Text.clear();
            }

            EndPaint(hWnd,&ps); // End paint scene.
            break;
        }


I know this is crazy and bloated but I was too lazy to edit it...the TxtOut() function is just a wrapper around the TextOut() function that adds more of my own features for use......this is from a program that opens a file and lets you scroll it up and down on the screen.
Topic archived. No new replies allowed.