Calling OnDraw in MFC SDI application - visual-c++

I am trying to make a MFC SDI application in VC++ to draw different shapes on click of the respective button. We have written our code inside the OnDraw() function. But Ondraw gets automatically called on running the application. We want it to be called only on the onclick of respective button. How do we proceed..?

When a shape button is clicked set a member variable that remembers what you want to draw.
Call Invalidate. This will cause OnDraw to be called by the framework.
In OnDraw check the member variable and draw the shape.
You must permit OnDraw to draw every time it is called by the framework. That is how the window image is restored after being uncovered, unminimized, resized, etc.

You cant prevent that OnDraw is only called when you press a button.
OnDraw in a view is called when WM_PAINT arrives and asks the window to repaint itself. Not executing OnDraw would cause nothing to be drawn at all.
OnDraw is also called when your application is minimized and maximized again, or when the size of the window changes.
Also I can not think about a scenario, where I want drawing only when a button is pressed. You need to be more precise. here.

OnDraw, as xMRi said, is called by the framework ... but if you want to draw something only when you click on a button, why don't you create a member variable, setup when you click the button, and get count when OnDraw is executed ?
void CYourAppView::OnDraw(CDC* pDC)
{
// MFC code
if(m_bButtonWasClicked)
{
// draw what ever you want
}
}

Related

Child window not getting invalidated when the parent window has CLIP_CHILDREN and CLIP_SIBLINGS style

In my Application, there is a modeless dialog box which has one static control and some customized controls - Edit box, buttons. Actually we are enhancing the functionality of this dialog and added 2 more static controls. The dialog box has CLIP_SIBLINGS and CLIP_CHILDREN styles.
The Problem I am facing is in WinXP, when some other window gets overlapped on this dialog, every other control is getting painted properly except the newly added static controls. But there is no particular difference between the existing static controls and the ones I added.
This is solved with explicitly calling Invalidate method of the static control in dialog's OnPaint() method, but still I didn't get why the existing controls are painted fine.
I tried removing CLIP_CHILDREN style, then it worked fine but there is a lot of flickering.
Please help me understand the same, I have gone through so many threads relating to the same but I didn't find the answer.

Keep taskbar icon, replace MFC dialog

I have a MFC dialog based application. User can change the language of the dialog, and I made this by closing existing dialog, and opening another with changed language. The problem is that the effect in the taskbar is that one icon is removed, and another identical is created in its place. If my application's icon is not the last icon in the task bar it will be perceived as it was moved to the end of taskbar icon set.
I want to retain icon's position in the taskbar, and rather to prevent icon flicker at all from happening. How do I do that?
The application must support OS'es from Windows XP to Windows 7.
EDIT: alternative question for which I would accept an answer is how to create an invisible window that is nevertheless shown in the taskbar, and how to forward relevant window messages from that window to my main window?
Make the dialog a child of another outer parent window. The parent can be a dialog or any other kind of window; all it will be providing is the title bar. If the user tries to resize it it will have to forward resizing commands to the dialog, but otherwise you shouldn't need to do much in the parent window.
Why not replace the dialog with a CFormView instead? That way there's a frame window that wraps around the dialog (which is embedded in a form view) and it's the frame window that owns the taskbar icon.
Create an SDI application that displays a CFormView. Display the dialog in the default language (or whatever langauge the user previously chose) on initialization. When the user chooses the 'change language' option, simply change the form view that's being displayed with a new one.
Bonus feature: with this design, the framework will take care of things like language-specific accelerators and menus for you with no effort on your part.
For more on how to do this, check out http://www.codeguru.com/cpp/w-d/doc_view/viewmanagement/article.php/c3341/Multiple-Views-Using-SDI.htm

drawRect not getting called when needed under OS X

I am using a window that uses a subclass of NSView. Part of it is drawn when the program starts, another part needs to be drawn when the user clicks on a button in another window. The code for the other window calls my subclass of NSView with no problems, creates the new graphics, and then returns. However, the window that needs to get updated with new graphics does not change.
When I put both the beginning graphics and the graphics that are to be drawn later in the initial drawRect call everything is drawn without a problem. However, drawRect does not get called when the user clicks the button on the second window. I've tried awakeFromNib, setNeedsDisplay, needsDisplay, and display, but nothing I've found so far gets the NSView window to call drawRect.
I am fairly new to OS X programming and appreciate any hints you can throw my way.
Thanks, Tom Jeffries
You should separate button click into a controller class and that controller should call setNeedsDisplay on view object. Don't say view itself setNeedsDisplay.

Is there an 'Onload' sort of a signal for QMainWindow?

PyQt noob here.
I need to call a dialog window as soon as the main window is loaded.
I also need the main window to be displayed in the background when this dialog is shown on top of it.
So, I'm looking for a 'onload' sort of a signal for the main window to call the function which in turn calls the dialog. Is there any other way around?
You can override the QWidget.showEvent for your QMainWindow to achieve the same effect. However, you need to keep track of whether it is the first time the window is shown or not since that method will be called every time the window is displayed after being hidden. Then use a modal QDialog so that the main window is shown in the background, but not enabled.

Visual C++ - Event for ComboBox

I'd like to know if there is an event for ComboBox in Visual C++ that I can utilyze for doing something when mouse passes over the items of the combobox. Thank you
You could try WM_DRAWITEM notification, but you might need an owner drawn style for that.
The draw item member contains an itemAction that indicates for what situation the item needs to draw itself.
Have a look at TrackMouseEvent().
You will probably need to create a class for your combobox (derived from CComboBox); to add the TrackMouseEvent() in it.
There are messages to handle the mouse hover (when the mouse is inside the client area of the CWnd) and mouse leave (when mouse leaves the client area of the CWnd).

Resources