|
I write apps in C++ (using VC++) and often I'm not sure where to palce the classes. For example, if I have a class that interact with a database (CDaoMyClass) I put a reference to this class in the CMyDocument class. Is it right?
I have some trouble in design the class hirerchy for my applycation.
Is there a book that teach how to design a GOOD application with an Object Oriented approach? I mean the Style of programming is important!!!
Thanks!
|
|
|
|
|
For example, if I have a class that interact with a database (CDaoMyClass) I put a reference to this class in the CMyDocument class. Is it right?
What do you mean by a 'reference'? Something like this?
// member of CMyDocument
CDaoMyClass &m_rDaoMyClass;
Is there a book that teach how to design a GOOD application with an Object Oriented approach?
'Design Patterns' by Gamma et al.
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
The 'Orthodox' way is to store all persistent data in the applications document. There are problems with doing this:
Eg.
You have an SDI application, with a control bar, nested in control bar is a custom list ctrl. Now from the list ctrl you need to display data stored in the document, this is where MFC becomes very messy.
You do the following,
1/ Get the MainFrame (AfxGetMainWnd and cast to CMainFrame*)
2/ Get the ActiveDocment (GetActiveDocument and cast to CMyDoc)
This completely sucks, however I have lived with this for 7 years.
The other solution is a MVC (Model/View/Controller) pattern, Stingray have developed a class framework implementing MVC (unfortunately I haven't used this). MVC is a much OOD way of the DOC/View metaphor.
Regards
Norm
|
|
|
|
|
I am working with a drawing program. I want to draw rotated ellipse(I dont't want to use rotated DC),but I fond the pen caps can only be round in win98. So I write
"LineTo" myself. But I found its speed was too slow. So please help me, tell where can find fastest code.
thanks to all the programmers who have helped me.
|
|
|
|
|
You can draw rotated ellipse using approximation with Bezier segments. The code below may be just what you want (I've extracted it from some old app - found the code on the net); the degrees parameter controls the rotation
void EllipseEx(CDC &dc, int cx, int cy, int rx, int ry, int degrees)
{
const int Segments = 12;
const int PointCount = 1 + 3 * Segments;
const double Alpha = 3.14159265359 / Segments;
const double Beta = 4 * (1 - cos(Alpha)) / (3 * sin(Alpha));
double px[PointCount], py[PointCount];
double Angle, CosA, SinA;
for (int i = 0; i < Segments; i ++)
{
Angle = 2 * i * Alpha;
CosA = cos(Angle);
SinA = sin(Angle);
int k = 3 * i;
int j = (i == 0) ? 3 * Segments - 1 : k - 1;
px[k] = rx * CosA;
py[k] = ry * SinA;
SinA *= Beta * rx;
CosA *= Beta * ry;
px[j] = px[k] + SinA;
py[j] = py[k] - CosA;
px[k + 1] = px[k] - SinA;
py[k + 1] = py[k] + CosA;
}
px[PointCount - 1] = px[0];
py[PointCount - 1] = py[0];
Angle = 2 * 3.1415927 * degrees / 360;
CosA = cos(Angle);
SinA = sin(Angle);
POINT pt[PointCount];
for (i = 0; i < PointCount; i ++)
{
pt[i].x = cx + (LONG)(0.5 + px[i] * CosA - py[i] * SinA);
pt[i].y = cy + (LONG)(0.5 + px[i] * SinA + py[i] * CosA);
}
PolyBezier(dc, pt, PointCount);
}
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
thank you! Do you have the code of LineTo? Mine is much slower than the CDC::LineTo. And how to create a pen with square cap in win98?
thanks to all the programmers who have helped me.
|
|
|
|
|
Do you have the code of LineTo? Mine is much slower than the CDC::LineTo.
Why do you need your own LineTo?
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
Hi, I have programm in VB, where is transparent window. And I rewrite code to MFC. But i have problemm ((. I dont know why ?!, but i see only black color on screen .
Can you help me ?? Here is my code.
_________________________________________
void CMyDialog::DrawScreen(CDC* dc){
CDC *window=GetDesktopWindow()->GetDC();
CDC *drawing = new CDC;
CBitmap *bitmap = new CBitmap;
CBitmap* oldbit;
drawing->CreateCompatibleDC(window);
bitmapa->CreateCompatibleBitmap(window,1050,1004);
oldbit = drawing->SelectObject(bitmap);
dc->BitBlt(0, 0, 100, 100, drawing, 1000, 730, SRCCOPY);
drawing->SelectObject(oldbit);
}
________________________________________
LB
|
|
|
|
|
You're trying to blt from uninitialized bitmap selected into the 'drawing' variable. Can't you just use the 'window' DC as a source?
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
Can you write my the code ?
Please .
lb
|
|
|
|
|
Ohhh, I have it. But : when I use BilBlt--> programm display the picture. And this picture is dialogwindow of programm . But i need picture which is unter the window ?? How can i do it ??
|
|
|
|
|
But i need picture which is unter the window ?? How can i do it ??
What worked OK in your VB program?
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
This is code from program in VB :
Function hDCToPicture(ByVal hDCSrc As Long, ByVal LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc As Long) As Picture
Dim hDCMemory As Long, hBmp As Long, hBmpPrev As Long, R As Long
Dim hPal As Long, hPalPrev As Long, RasterCapsScrn As Long, HasPaletteScrn As Long
Dim PaletteSizeScrn As Long, LogPal As LOGPALETTE
'Create a compatible device context
hDCMemory = CreateCompatibleDC(hDCSrc)
'Create a compatible bitmap
hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
'Select the compatible bitmap into our compatible device context
hBmpPrev = SelectObject(hDCMemory, hBmp)
R = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy)
hBmp = SelectObject(hDCMemory, hBmpPrev)
R = DeleteDC(hDCMemory)
Set hDCToPicture = CreateBitmapPicture(hBmp, hPal)
End Function
Private Sub Form_Load()
dsk = GetDesktopWindow()
'Create a picture object from the screen
Screen.TwipsPerPixelX, Screen.Height / Screen.TwipsPerPixelY)
srcdc = GetDC(dsk)
Set Me.Picture = hDCToPicture(srcdc, Left / Screen.TwipsPerPixelX, Top / Screen.TwipsPerPixelY, Width / Screen.TwipsPerPixelX, Height / Screen.TwipsPerPixelY)
End sub
Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture
Dim R As Long, Pic As PicBmp, IPic As IPicture, IID_IDispatch As GUID
'Fill GUID info
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
'Fill picture info
With Pic
.Size = Len(Pic) ' Length of structure
.Type = vbPicTypeBitmap ' Type of Picture (bitmap)
.hBmp = hBmp ' Handle to bitmap
.hPal = hPal ' Handle to palette (may be null)
End With
'Create the picture
R = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
'Return the new picture
Set CreateBitmapPicture = IPic
End Function
|
|
|
|
|
It seems that you're trying to copy the desktop window to a bitmap first. Next, you want to display a dialog and paint its background using saved image. There's a class encapsulating a memory DC and a bitmap on code project; search for CMemDC.
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
I would to monitor the memory usage as given in the taskmanager , i would be more helpful if some one provide the solution .
GG
|
|
|
|
|
|
Hi all,
I'm writing program about database,
In my program i have three dialogs, and i have to use of CDaoDatabase object in all dialogs ...
I want to use of only one object for do that,
How can i do that ???
Does i must create global variable ?
I mean, writing CDaoDatabase g_db; in first of my source code, and using of extern CDaoDatabase g_db; in other cpp files ???
Do you have any idea ?
My program is in SDI mode ...
My month article: Game programming by DirectX by Lan Mader.
Please visit in: www.geocities.com/hadi_rezaie/index.html
Hadi Rezaie
|
|
|
|
|
Make your CDaoDatabase an application data member and provide an accessor function in your app class...
class CYourApp : public CWinApp
{
public:
CDaoDatabase& GetDatabase() { return m_db; }
private:
CDaoDatabase m_db;
};
... and use AfxGetApp in your dialogs to get access to db:
CDaoDatabase &db = static_cast<CYourApp *>(AfxGetApp())->GetDatabase();
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
Hi Tomasz, and thanks for reply ...
Can you explain more about your source codes ...
For example, first, you make private member variable CDaoDatabase m_db; and then you make public member function CDaoDatabase &GetDatabase(){return m_db;} !!!
WHY ???
Why you make GetDatabase() function ? why i shouldn't use of only m_db ?
And please explain to me how can i use of GetDatabase() function ...
Because i'm beginner ...
Thanks again ...
My month article: Game programming by DirectX by Lan Mader.
Please visit in: www.geocities.com/hadi_rezaie/index.html
Hadi Rezaie
|
|
|
|
|
For example, first, you make private member variable CDaoDatabase m_db; and then you make public member function CDaoDatabase &GetDatabase(){return m_db;} !!! WHY ???
This is called encapsulation. The main benefit is that you can rename m_db and you'll have to change the name only in two places
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
Hi again,
AfxGetApp() is object of CWinApp class, so can't use of m_db or GetDatabase() in AfxGetApp() !!!
My month article: Game programming by DirectX by Lan Mader.
Please visit in: www.geocities.com/hadi_rezaie/index.html
Hadi Rezaie
|
|
|
|
|
|
Actually, Tomasz, I would not mind having an explanation of the advantage of static_cast over the old fashioined ((CMyApp*)AfxGetApp())->WhatEver() in this case.
|
|
|
|
|
The only syntactic difference is that static_cast won't compile if CYourApp isn't derived from CWinApp, which is highly unlikely. It's just my personal preference to cast the C++ way.
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
Hi Tomasz,
Thank you for your explain ...
Now, can you write example about using of m_db from AfxGetApp() ?
Because in your example you use of GetDatabase() in AfxGetApp(), now can you write example about using m_db in AfxGetApp() ?
My month article: Game programming by DirectX by Lan Mader.
Please visit in: www.geocities.com/hadi_rezaie/index.html
Hadi Rezaie
|
|
|
|