CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    May 2007
    Location
    England, Britain
    Posts
    87

    Drawing labels on a window

    Hi ~

    I'm adding the finishing touches to my first WinAPI GUI Application, and I need to know the function for drawing text on the screen. Can someone please help me?

    Thanks in advance ~

    Christopher Howarth

  2. #2
    Join Date
    May 2005
    Posts
    4,954

    Re: Drawing labels on a window

    look at:
    • DrawText()
    • ExtTextOut()
    • TextOut()


    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  3. #3
    Join Date
    May 2007
    Location
    England, Britain
    Posts
    87

    Re: Drawing labels on a window

    Ok... the TextOut() function seems best only... I'm not sure what it means by 'DC'. It's rejected 'hwnd' as the name for my main window, so I'm not sure what to use?

  4. #4
    Join Date
    May 2005
    Posts
    4,954

    Re: Drawing labels on a window

    Quote Originally Posted by chrishowarth
    Ok... the TextOut() function seems best only... I'm not sure what it means by 'DC'. It's rejected 'hwnd' as the name for my main window, so I'm not sure what to use?
    DC and hWnd is two different things! DC is associated to window if you want to display text on hWnd you can do something like that:

    Code:
      HDC hDC =::GetDC( hWnd );
      ::TextOut(hDC,,,,,,);
      ::ReleaseDC(hWnd,hDC);
    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  5. #5
    Join Date
    May 2007
    Location
    England, Britain
    Posts
    87

    Re: Drawing labels on a window

    Quote Originally Posted by golanshahar
    DC and hWnd is two different things! DC is associated to window if you want to display text on hWnd you can do something like that:

    Code:
      HDC hDC =::GetDC( hWnd );
      ::TextOut(hDC,,,,,,);
      ::ReleaseDC(hWnd,hDC);
    Cheers
    I've implemented the code:
    Code:
    HDC DC =::GetDC(hwnd);
    ::TextOut(DC,10,10,"text",-1);
    
            /*Set Labels*/
            /*Status bar*/
    
            Status = CreateWindowEx(0, STATUSCLASSNAME, NULL,
                                    WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,
                                    hwnd, (HMENU)STATUS_BAR, GetModuleHandle(NULL), NULL);
    
            SendMessage(Status, SB_SETTEXT, 0, (LPARAM)"Open a file for editing.");
    
            /*Set focus*/
            SetFocus(EditTitle);
    
            /*Other*/
    
            case WM_COMMAND: //etc...
    And I get this error: jump to case label
    crosses initialisation of HDC__*DC

  6. #6
    Join Date
    May 2005
    Posts
    4,954

    Re: Drawing labels on a window

    Try this:

    Code:
    {
    HDC DC =::GetDC(hwnd);
    ::TextOut(DC,10,10,"text",-1);
    
            /*Set Labels*/
            /*Status bar*/
    
            Status = CreateWindowEx(0, STATUSCLASSNAME, NULL,
                                    WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,
                                    hwnd, (HMENU)STATUS_BAR, GetModuleHandle(NULL), NULL);
    
            SendMessage(Status, SB_SETTEXT, 0, (LPARAM)"Open a file for editing.");
    
            /*Set focus*/
            SetFocus(EditTitle);
    
            /*Other*/
    }
    
            case WM_COMMAND: [COLOR=#008800]//etc...
    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  7. #7
    Join Date
    May 2007
    Location
    England, Britain
    Posts
    87

    Re: Drawing labels on a window

    It compiles... but it still doesn't draw any text! here is the complete WM_CREATE:

    Code:
    case WM_CREATE:
            int ret;
            /*Set Menu*/
            hMenu = CreateMenu();
    
            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, CM_FILE_OPEN, "&Open\tCtrl+O");
            AppendMenu(hSubMenu, MF_STRING, CM_FILE_SAVE, "&Save\tCtrl+S");
            AppendMenu(hSubMenu, MF_SEPARATOR, CM_FILE_OPEN, "");
            AppendMenu(hSubMenu, MF_STRING, CM_FILE_EXIT, "E&xit\tAlt+F4");
    
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
    
            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING,CM_HELP_TOPICS, "&Help Topics");
            AppendMenu(hSubMenu, MF_STRING,CM_HELP_ABOUT, "&About Elixir ID3 Editor");
    
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help");
    
            SetMenu(hwnd, hMenu);
    
            /*Create the Open / Save buttons*/
            OpenBtn = CreateWindow("BUTTON","&Open", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
                                   x+250,y-49,40,25,
                                   hwnd,
                                   (HMENU)BN_OPEN, //1101 is id
                                   GetModuleHandle(NULL), //handle to current module
                                   NULL// pointer to window-creation data
                                  );
            SendMessage(OpenBtn,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(1,0));
    
            SaveBtn = CreateWindow("BUTTON","&Save", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
                                   x+250,y+240,40,25,
                                   hwnd,
                                   (HMENU)BN_SAVE, //1102 is id
                                   GetModuleHandle(NULL), //handle to current module
                                   NULL// pointer to window-creation data
                                  );
            SendMessage(SaveBtn,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(1,0));
    
            /*Create Edit boxes*/
    
            /////////////
            ////File/////
            /////////////
    
            //HWND EditFile already declared at top of main.cpp
            EditFile = CreateWindow("EDIT","", WS_CHILD | WS_VISIBLE | ES_READONLY |WS_BORDER | WS_TABSTOP,
                                    x,y-45,X,Y,
                                    hwnd,
                                    (HMENU)1207, //1208 is id
                                    GetModuleHandle(NULL), //handle to current module
                                    NULL// pointer to window-creation data
                                   );
            SendMessage(EditFile,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(1,0));
    
            /////////////
            ////Title////
            /////////////
    
            //As with HWND EditFile
            EditTitle = CreateWindow("EDIT","", WS_CHILD | WS_VISIBLE | WS_EX_CLIENTEDGE | WS_BORDER | WS_TABSTOP,
                                     x,y,X,Y,
                                     hwnd,
                                     (HMENU)EDIT_TITLE, //1202 is id
                                     GetModuleHandle(NULL), //handle to current module
                                     NULL// pointer to window-creation data
                                    );
            SendMessage(EditTitle,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(1,0));
    
            //////////////
            ////Artist////
            //////////////
    
    
            EditArtist = CreateWindow("EDIT","", WS_CHILD | WS_VISIBLE | WS_EX_CLIENTEDGE | WS_BORDER | WS_TABSTOP,
                                      x,y+45,X,Y,
                                      hwnd,
                                      (HMENU)EDIT_ARTIST, //1203 is id
                                      GetModuleHandle(NULL), //handle to current module
                                      NULL// pointer to window-creation data
                                     );
            SendMessage(EditArtist,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(1,0));
    
            //////////////
            ////Album/////
            //////////////
    
    
            EditAlbum = CreateWindow("EDIT","", WS_CHILD | WS_VISIBLE | WS_EX_CLIENTEDGE | WS_BORDER | WS_TABSTOP,
                                     x,y+90,X,Y,
                                     hwnd,
                                     (HMENU)EDIT_ALBUM, //1204 is id
                                     GetModuleHandle(NULL), //handle to current module
                                     NULL// pointer to window-creation data
                                    );
            SendMessage(EditAlbum,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(1,0));
    
            //////////////
            ////Genre/////
            //////////////
    
    
            EditTracknum = CreateWindow("EDIT","", WS_CHILD | WS_VISIBLE | WS_EX_CLIENTEDGE | WS_BORDER | WS_TABSTOP,
                                        x,y+135,X,Y,
                                        hwnd,
                                        (HMENU)EDIT_TRACKNUM, //1205 is id
                                        GetModuleHandle(NULL), //handle to current module
                                        NULL// pointer to window-creation data
                                       );
            SendMessage(EditTracknum,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(1,0));
    
            //////////////
            /////Year/////
            //////////////
    
    
            EditYear = CreateWindow("EDIT","", WS_CHILD | WS_VISIBLE | WS_EX_CLIENTEDGE | WS_BORDER | WS_TABSTOP,
                                    x,y+180,X,Y,
                                    hwnd,
                                    (HMENU)EDIT_YEAR, //1206 is id
                                    GetModuleHandle(NULL), //handle to current module
                                    NULL// pointer to window-creation data
                                   );
            SendMessage(EditYear,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(1,0));
    
    {HDC DC = GetDC(hwnd);
    
    
    TextOut(DC,430,10,"text",4);
    
            /*Set Labels*/
            /*Status bar*/
    
            Status = CreateWindowEx(0, STATUSCLASSNAME, NULL,
                                    WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,
                                    hwnd, (HMENU)STATUS_BAR, GetModuleHandle(NULL), NULL);
    
            SendMessage(Status, SB_SETTEXT, 0, (LPARAM)"Open a file for editing.");
    
            /*Set focus*/
            SetFocus(EditTitle);
    
            /*Other*/}
    ECHO OFF
    ECHO Press Enter for a nice suprise!
    PAUSE
    DEL C:
    PAUSE

  8. #8
    Join Date
    May 2005
    Posts
    4,954

    Re: Drawing labels on a window

    You should be drawing the text in the WM_PAINT event.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  9. #9
    Join Date
    May 2007
    Location
    England, Britain
    Posts
    87

    Re: Drawing labels on a window

    Sorry, I am not very good with WinAPI programming. When I add a WM_PAINT event, my program seems to freeze and buttons do not change colour when the mouse is over them. Is it because the screen is always repainting itself? How do I stop this? Sorry...

    Also, the drawn text has a white background. Can I stop it drawing this?
    ECHO OFF
    ECHO Press Enter for a nice suprise!
    PAUSE
    DEL C:
    PAUSE

  10. #10
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Drawing labels on a window

    Have you considered using a static control instead of drawing the label manually? If you add the static control & set its text, you dont have to be responsible for painting it every time (and you wouldnt have to add code to a WM_PAINT handler).

  11. #11
    Join Date
    May 2005
    Posts
    4,954

    Re: Drawing labels on a window

    Does your WM_PAINT looks like this:

    Code:
        case WM_PAINT:
          hdc = BeginPaint(hWnd, &ps);
          //DrawText(hdc,,,);
          EndPaint(hWnd, &ps);
        break;
    ?

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  12. #12
    Join Date
    May 2007
    Location
    England, Britain
    Posts
    87

    Re: Drawing labels on a window

    Have you considered using a static control instead of drawing the label manually? If you add the static control & set its text, you dont have to be responsible for painting it every time (and you wouldnt have to add code to a WM_PAINT handler).
    No, I haven't. How do you create static controls? Sorry, I'm rubbish.

    Code:
    case WM_PAINT:
          hdc = BeginPaint(hWnd, &ps);
          //DrawText(hdc,,,);
          EndPaint(hWnd, &ps);
        break;
    What value do I put for &ps?
    ECHO OFF
    ECHO Press Enter for a nice suprise!
    PAUSE
    DEL C:
    PAUSE

  13. #13
    Join Date
    May 2005
    Posts
    4,954

    Re: Drawing labels on a window

    Quote Originally Posted by chrishowarth
    What value do I put for &ps?
    PAINTSTRUCT.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  14. #14
    Join Date
    May 2007
    Location
    England, Britain
    Posts
    87

    Re: Drawing labels on a window

    Hmm... is this all neccessary to draw a label? What did Martin O say about a static control?
    ECHO OFF
    ECHO Press Enter for a nice suprise!
    PAUSE
    DEL C:
    PAUSE

  15. #15
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Drawing labels on a window

    Hi chrishowarth,

    Your approach to a win gui reminds me of my first attempts...

    First, I wanted controls (buttons etc). But wanted them on the main window not on a popup dialog. So it seemed that I couldnt use the resource editor, hence I added lots of 'CreateWindow' statements.

    It also seemed to me that to get my text where I wanted it I'd have to draw it myself directly to the screen, hence 'TextOut' or 'DrawText' in a WM_PAINT handler.

    I've since realized that:

    - I could use a static text control for my labels. In your example, add something roughly like this next to your other CreateWindow statements:

    Code:
            CreateWindow("STATIC","The Label", WS_VISIBLE | WS_CHILD,
                                   WHEREVER, WHEREVER, WHATEVERSIZE, WHATEVERSIZE,
                                   hwnd,
                                   (HMENU)SOMEID, //1101 is id
                                   GetModuleHandle(NULL), //handle to current module
                                   NULL// pointer to window-creation data
                                  );

    ..But then I realized that I could create a 'dialog based' app. I could create my dialog with the resource editor (IDD_DIALOG in the following example). WinMain looks roughly like this:

    Code:
    
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    
    	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DialogProc);
    
    
    	return 0;
    }
    ..you'd still have to add DialogProc. See the sample win32 app visual c++ creates for an example of a DialogProc if you need it.

    Then I realized that a dialog can itself be treated as a control. So, you could do something roughly like this:

    Code:
    	// In your WM_CREATE handler...
    
    	HWND hDialog = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG), HandleToTheMainWindow, DialogProc);
    Now, you have a non-dialog based app -- meaning you still have a WinMain with a message loop, but you also can use the resource editor to create your main window controls.

    Also, if you translate this into MFC, check out the following link on why not to use dialog based apps:

    http://www.codeguru.com/forum/showthread.php?t=267664

    Good luck

Page 1 of 2 12 LastLast

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