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
Printable View
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
look at:
- DrawText()
- ExtTextOut()
- TextOut()
Cheers
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:Quote:
Originally Posted by chrishowarth
CheersCode:HDC hDC =::GetDC( hWnd );
::TextOut(hDC,,,,,,);
::ReleaseDC(hWnd,hDC);
I've implemented the code:Quote:
Originally Posted by golanshahar
And I get this error: jump to case labelCode: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...
crosses initialisation of HDC__*DC
Try this:
CheersCode:{
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...
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*/}
You should be drawing the text in the WM_PAINT event.
Cheers
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?
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).
Does your WM_PAINT looks like this:
?Code:case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
//DrawText(hdc,,,);
EndPaint(hWnd, &ps);
break;
Cheers
No, I haven't. How do you create static controls? Sorry, I'm rubbish.Quote:
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).
What value do I put for &ps?Code:case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
//DrawText(hdc,,,);
EndPaint(hWnd, &ps);
break;
PAINTSTRUCT.Quote:
Originally Posted by chrishowarth
Cheers
Hmm... is this all neccessary to draw a label? What did Martin O say about a static control?
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:
..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.Code:
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DialogProc);
return 0;
}
Then I realized that a dialog can itself be treated as a control. So, you could do something roughly like this:
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.Code:
// In your WM_CREATE handler...
HWND hDialog = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG), HandleToTheMainWindow, DialogProc);
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 :)
Thankyou! :) It works; thankyou!