View Full Version : Some basic GUI help
Bender
11-08-2002, 06:11 AM
I'm going to be finding myself doing some basic GUI coding with Visual C++ 6.0 this weekend (using the MFC only). I've never done it before, and I don't really need to know all of the intricacies or complexities involved, simply how to make it do what I want it to do. I'm hoping I can ask a few questions of this board over the weekend...
Anyway, my first question should be a simple one. I have a button that, when clicked, should pop up a dialog box. I've given this dialog box the identifier "IDD_DIALOG_BAGGAGE" and have the code execute the method
void CProjectDlg::OnButtonMakeBaggageTag(){...}
What I need to know is how to get the dialog to appear when the button is clicked.
Please remember that I'm brand spanking new at this (and yearning and crying for the sweet embrace of Java again) so don't think you'll offend me if you explain it like you would to a child. :)
Thanks for any help that you can give me.
stuka
11-08-2002, 11:19 AM
You'll want to create a new class, derived from CDialog, and I don't remember exactly how you associate your class with the dialog resource, but I think it's fairly easy. Anywho, after you do that, then in your handler, just do:BaggageDialog dlg;
int result = dlg.DoModal();
if (result == IDOK){
//They clicked OK
}
else{
//What to do if they didn't
}
Oh, and just in case you don't know, the UpdateData() method will swap data between your dialog's edit boxes, etc. and any member variables defined to handle them - for example, if you associate a CString with an Edit control, then doing:dlg.UpdateData();
will put the contents of the edit box into the CString - calling dlg.UpdateData(FALSE)
will put the contents of the CString variable into the control.
Bender
11-08-2002, 02:45 PM
Thanks Stuka very much. I'm beginning to appreciate how easy VC++ makes this stuff.
I'll keep this thread bumped with new questions. Thanks again.
Bender
11-08-2002, 03:23 PM
Okay, another question that's probably a bit more complicated.
When I click on a button, I pop up a CPrintDialog dialog where I can select my printer. Here's the code I have for that:
CPrintDialog dlg(FALSE);
if (dlg.DoModal() == IDOK)
{
//Make it print here
}
GlobalFree(dlg.m_pd.hDevMode);
GlobalFree(dlg.m_pd.hDevNames);
DeleteDC(dlg.GetPrinterDC());But now I need to know how to actually make it print. I can get the device name with dlg.getDeviceName(), but how can I send it an image to print, or some text, or anything like that?
Finding any information on this online is almost impossible.
bwkaz
11-08-2002, 03:36 PM
I believe you grab the printer DC and do what you normally would with a screen DC to it (SelectFont(), then WriteText() or whatever they call it, or LineTo() or whatever it's called, or LoadBitmap(), or pretty much anything else). At least, that's the Windows API way. Probably not the MFC way, but it should still work.
stuka
11-08-2002, 04:41 PM
bwkaz - you're pretty much right. I think I have some code that does this in MFC. I'll post a bit of it if I find it.
Bender
11-08-2002, 04:47 PM
Originally posted by Stuka
bwkaz - you're pretty much right. I think I have some code that does this in MFC. I'll post a bit of it if I find it. Please do. I found some example on Microsoft's site, but it only shows printing text, and the letters are terribly tiny.
It says you can fix the scaling, but doesn't say how to.
stuka
11-08-2002, 05:10 PM
Here's a bit that works:
CDC prnDC; //DC for printing
TEXTMETRIC tm; //Text metrics for printing lines of text
int txtHeight, curLine = 8; //Storing print info
CPrintDialog prnDlg(FALSE); //Print setup dialog
CString temp, oem = config->GetOEM().c_str();
if (prnDlg.DoModal() != IDOK) return;//Make sure they don't cancel
prnDC.Attach(prnDlg.CreatePrinterDC());
prnDC.GetTextMetrics(&tm);
txtHeight = tm.tmHeight; //Get the text height
prnDC.StartDoc("config.txt");
prnDC.StartPage();
PrintHeader(prnDC); //Print logo and other header info
temp.LoadString(IDS_TITLE);
prnDC.TextOut(0, txtHeight * 4, temp);
PrintSysInfo(prnDC); //Print customer system info
temp.LoadString(IDS_OTHER_PTSHDR);
prnDC.TabbedTextOut(0, txtHeight * 7, temp, 0, NULL, 0);
for (int i = 0; i < m_ctrlDisplay.GetCount(); ++i)
{
m_ctrlDisplay.GetText(i, temp);
temp.Replace("\t\t", "\t");
prnDC.TabbedTextOut(0, curLine * txtHeight, temp, 0, NULL, 0);
curLine++;
}
prnDC.EndPage();
prnDC.EndDoc();
The font is just about right IIRC. Some of the code I left in there is for formatting, so yours will vary.
Bender
11-08-2002, 06:54 PM
Yeah Stuka, that works. Now how can I change it to print out an image? Is it possible? The DrawIcon() method maybe?
stuka
11-08-2002, 07:12 PM
Never did that, but I THINK the functions you need would be from CBitmap (loading your .bmp into an MFC object), specifically LoadBitmap(), and CDC::BitBlt() to copy the CBitmap into the device context.
Bender
11-09-2002, 02:39 PM
Is there anyone who can give me some example code of what was explained above? None of this makes sense to me.
stuka
11-11-2002, 01:20 AM
Hold the phone - I think I found a more correct answer! The DrawState() function of the CDC class appears to do what you need:BOOL DrawState( CPoint pt, CSize size, CBitmap* pBitmap, UINT nFlags, CBrush* pBrush = NULL ); You'll need to figger out the details of CSize and CPoint, but I imagine they're easy...
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.