Click to See Complete Forum and Search --> : Drawing labels on a window


chrishowarth
May 16th, 2007, 10:29 AM
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

golanshahar
May 16th, 2007, 01:38 PM
look at:

DrawText()
ExtTextOut()
TextOut()


Cheers

chrishowarth
May 16th, 2007, 03:16 PM
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?

golanshahar
May 16th, 2007, 04:09 PM
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:


HDC hDC =::GetDC( hWnd );
::TextOut(hDC,,,,,,);
::ReleaseDC(hWnd,hDC);


Cheers

chrishowarth
May 17th, 2007, 10:33 AM
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:


HDC hDC =::GetDC( hWnd );
::TextOut(hDC,,,,,,);
::ReleaseDC(hWnd,hDC);


Cheers

I've implemented the 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

golanshahar
May 17th, 2007, 02:05 PM
Try this:


{
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

chrishowarth
May 18th, 2007, 10:22 AM
It compiles... but it still doesn't draw any text! here is the complete WM_CREATE:

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*/}

golanshahar
May 18th, 2007, 10:28 AM
You should be drawing the text in the WM_PAINT event.

Cheers

chrishowarth
May 19th, 2007, 05:44 AM
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?

Martin O
May 19th, 2007, 08:46 AM
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).

golanshahar
May 19th, 2007, 09:18 AM
Does your WM_PAINT looks like this:


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

?

Cheers

chrishowarth
May 19th, 2007, 11:54 AM
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.

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

What value do I put for &ps?

golanshahar
May 19th, 2007, 01:10 PM
What value do I put for &ps?

PAINTSTRUCT (http://msdn2.microsoft.com/en-us/library/ms534910.aspx).

Cheers

chrishowarth
May 19th, 2007, 02:44 PM
Hmm... is this all neccessary to draw a label? What did Martin O say about a static control?

Martin O
May 20th, 2007, 06:57 AM
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:


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:




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:



// 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 :)

chrishowarth
May 20th, 2007, 09:58 AM
Thankyou! :) It works; thankyou!