AfxMessageBox shows up in the middle of the desktop - visual-c++

I call AfxMessageBox() function expecting the pop-up to be centered to the MFC dialog that called it. However the box gets centered to the desktop rather than to the dialog. Its position doesn't depend on actual location of the parent dialog. What can cause such behavior? Is it possible to move AfxMessageBox to a desired position? Thanks!

You could use
int WINAPI MessageBox(
_In_opt_ HWND hWnd,
_In_opt_ LPCTSTR lpText,
_In_opt_ LPCTSTR lpCaption,
_In_ UINT uType
);
and pass the handle of the application window as first parameter (HWND). Then it should be centered.
I don't use MFC anymore, but I think the main application window must be set somewhere (InitInstance?), maybe if this is not done, AfxMessageBox doesn't use the application window.

A dialog box can be centered on its parent window. AfxMessageBox is created as a 'system wide' dialog box, meaning not a 'child' dialog box of your application window, and that is why it is not centered on your parent window, but on the desktop.
You wont be able to move AfxMessageBox after its creation, since it is a modal dialog box (and the hassle of creating a thread just in order to move the dialog, is probably not worth it).
The better solution would be just to create your own dialog function (as suggested above by alain). Then, when you call it, you can pass the window handle of your calling window class (hWnd) and the dialog created will be centered on your application window, as you wanted.

Related

Calling OnDraw in MFC SDI application

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

How to make an expandable window or dialog in FLTK?

I want an 'expandable' window/widget.When clicking a button on the current window, another widget will shows up, which is attached to the current window, and it can be 'fold' back if clicking the button again.
It is not a pop-up window which is free to move with respect to the main window. I want it to be attached to the main window. Anyone has any idea? Thanks a lot.
I tried making a large window consisting of two box, with one being tackled to show or hide, but the window size does not change. If you resize the window, then the position does not look right.
Have you had a look at http://seriss.com/people/erco/fltk/ and the test programs that come with the fltk distribution?
Basically you need to set one resizable widget for the window/dialog. Make this the group that you are showing/hiding when you press the button.
If you have an FLTK distribution, have a look at the program test/resize.cxx as an example of how to do resizing based on button clicks.
I'm not sure I understand what your exact problem is...
Perhaps you need a widget that's not always shown on your main Fl_Window.
Then pressing another button makes it visible and attached somewhere in
the window, according to the window's size...
And when you resize the window, it changes its position, so it is always located,
let's say, at the bottom right of it. If I have understood your problem correctly,
then make a class, myWindow or whatever, a subclass of Fl_Window.
And override the resize function, which is called when the window is being resized.
class myWindow:public Fl_Window{
Fl_Button *mySpecialWidget; //the movable widget (not allways a button ofc)
public:
myWindow(int x, int y, int w, int h, const char *L=NULL):
Fl_Window(x,y,w,h,L){
mySpecialWidget = new Fl_Button(.....); //initialize subwidget here
add(mySpecialWidget); //add it as a subwidget of myWindow
//may also include all the other needed widgets here...
}
void resize(int x, int y, int w, int h){
//override this Fl_Window function, with
//any extra functionality that you need
//example, check window size-> set relevant position for mySpecialWidget
}
}

Set MFC dialog inside a cview

I have a cdialog in mfc and I want to host it in a cview so when you click on another cview, it will come to the top, unlike a normal dialog.
I tried by setting the cview as parent of the dialog, but the dialog did not showup. Is there a way to do this?
Thnaks.
Create a CFormView. A CFormView is based on a dialog template, so it's basically a view that looks/acts/works like a dialog (and unless you've done something strange, your existing dialog template should work fine for the CFormView as well).

Handling OnSize function or resizable dialog in MFC

In one MFC application there is a paned window.
On that window,
I have added a menu-bar and a toolbar and lots of other controls.
The paned window is re-sizable.
Now for the re-sizable window, I have override the function OnSize(). There I have retrieved the top window size and then below it and so on...
Then for every control I have retrieved it's window and called MoveWindow().
I just wants to be assured is it the correct way for handling resizable window/ dialog in MFC or there is some other ways available.
Thanks
Instead of reinventing the wheel: try the ResizableLib.
It can handle resizing of dialogs in a very easy way and you don't have to worry about every detail yourself.
When moving a large number of controls at once, it is preferable to use DeferWindowPos to minimize the repainting. I have an example of how to use it for a resizable MFC dialog at https://stackoverflow.com/a/5739620/5987.

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

Resources