How to create domodal dialog box in mfc dynamically? - mfc

i have created two dailog box statically.
CParentDailog
CMyDailog
and then
CParentDailog
{
CMyDailog *l_pdailog;
}
in oninitdailog of CParentDailog ia mdoing
l_pdailog = new CMyDailog();
l_pdailog ->create(ID_DAILG1); // this is id of CMyDailog
l_pdailog ->Domodal(); // crashing at this point
why it is crashing?

Hi you need to set parent window:
l_pdailog = new CMyDailog(pWndparent);
l_pdailog ->Domodal();

Related

How to get screen bounds for WinUI 3 Desktop application?

I need to achieve below requirements in WinUI 3 Desktop applications.
How to get screen bounds?
How to change windows cursor type in runtime?
I already did this in WinUI UWP applications.
For Screen bounds,
var visibleBounds = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds;
var scaleFactor = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
Size screenSize = new Size((visibleBounds.Width * scaleFactor), (visibleBounds.Height * scaleFactor));
For Cursor:
Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.SizeNorthwestSoutheast, 0);
Anyone please suggest how to achieve same requirement in WinUI Desktop applications?
I've added the following to App.xaml.cs:
private static MainWindow m_window;
public static MainWindow MainWindow { get { return m_window; } }
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs e)
{
m_window = new MainWindow();
m_window.Activate();
}
And when I need bounds anywhere, I can use MainWindow.Bounds.
Regarding cursor, you need ProtectedCursor
this.ProtectedCursor = InputSystemCursor.Create(InputSystemCursorShape.Arrow);
Only thing it is protected, so you need to use it from right class.

How to fix the size of MDI Subwindow in Qt

I'm creating a Desktop Application in Qt and I'm showing subwindow inside the MDI Area. I have the problem that in the size of subwindow adjusts every time I open my code in the other monitor.
My goal is to fix the size of subwindow and make it parent size. Hope you understand my problem.
I'm using this code to change the size and showing the subwindow in mdi area
Subwindow1 = new QMdiSubWindow(mdiArea);
QQuickWidget* widget2 = new QQuickWidget(Map);
widget2->setResizeMode(QQuickWidget::SizeRootObjectToView);
widget2->setSource(QUrl("qrc:/subwindow1.qml"));
widget2->show();
Subwindow1->setWidget(widget2);
Subwindow1->resize(700,640);
Subwindow1->setWindowTitle("Subwindow 1");
Subwindow1->setAttribute(Qt::WA_DeleteOnClose,false);
Subwindow1->setWindowFlag(Qt::FramelessWindowHint);
Subwindow1->addSubWindow(Subwindow1);
mdiArea->setActiveSubWindow(Subwindow1);
Subwindow1->show();
Subwindow2 = new QMdiSubWindow(mdiArea);
QQuickWidget* widget3 = new QQuickWidget(Camera1);
widget3->setResizeMode(QQuickWidget::SizeRootObjectToView);
widget3->setSource(QUrl("qrc:/subwindow2.qml"));
widget3->show();
Subwindow2->setWidget(widget3);
Subwindow2->resize(304,270);
Subwindow2->setWindowTitle("Subwindow 2");
Subwindow2->setAttribute(Qt::WA_DeleteOnClose,false);
Subwindow2->setWindowFlag(Qt::FramelessWindowHint);
mdiArea->addSubWindow(Subwindow2);
mdiArea->setActiveSubWindow(Subwindow2);
Subwindow2->show();
Subwindow3 = new QMdiSubWindow(mdiArea);
QQuickWidget* widget4 = new QQuickWidget(Camera2);
widget4->setResizeMode(QQuickWidget::SizeRootObjectToView);
widget4->setSource(QUrl("qrc:/subwindow3.qml"));
widget4->show();
Subwindow3->setWidget(widget4);
Subwindow3->resize(304,270);
Subwindow3->setWindowTitle("Subwindow 3");
Subwindow3->setAttribute(Qt::WA_DeleteOnClose,false);
Subwindow3->setWindowFlag(Qt::FramelessWindowHint);
mdiArea->addSubWindow(Subwindow3);
mdiArea->setActiveSubWindow(Subwindow3);
Subwindow3->show();
For example :
You have Subwindow1 with (700,640)
The code is : Subwindow1->setFixedSize(new QSize(700,640))
Hope it help !
https://doc.qt.io/qt-5/qwidget.html#setFixedSize

Create a function that selects a Main Menu bar based on the selected tab

I am fairly new to c++ and am wondering if there is a way to create a function that will select a menu bar created in the MFC Menu Editor and display it at the top of the window.
The idea is to have a different menu bar for each of the tabs because each tab will have different options.
For example a menu bar called ID_REGMENUBAR for Doom Reg and ID_SCRIPTMENUBAR for the Script
If more info is needed please say so. Thanks!
Use CMenu::LoadMenu and CWnd::SetMenu. For example, declare member data:
CMenu m_menu1, m_menu2;
Initialize the menu once:
m_menu1.LoadMenu(ID_REGMENUBAR);
m_menu2.LoadMenu(ID_SCRIPTMENUBAR);
Use SetMenu(&m_menu1) to assign the menu on run time. You can respond to tab selection changes by looking TCN_SELCHANGE
BEGIN_MESSAGE_MAP(...)
ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, OnTabSelection)
END_MESSAGE_MAP()
void CMyWnd::OnTabSelection(NMHDR*, LRESULT*)
{
int tab = m_Tab.GetCurSel();
CMenu *pMenu = NULL;
if (tab == 0) pMenu = &m_menu1;
if (tab == 1) pMenu = &m_menu2;
CFrameWnd* frame = (CFrameWnd*)AfxGetMainWnd();
frame->SetMenu(pMenu);
}

MFC - Printing - Changing page orientation from a custom pagesetup dialog

I am developing a custom print dialog and page setup using MFC and VS2008 for my Win32 program. Since the code is legacy, I can't take much advantage from MFC view/doc architecture. As a result, I wrote a printing code completely from scratch.
I setup CPrintInfo, instantiate my custom print dialog box and hook this dialog box to the CPrintInfo I just created. When my custom print dialog is up, I have a radio button to let a user toggles the page orientation. For some reasons, I couldn't modify the current DEVMODE at the run-time. As a result, every page I print will end up as a portrait.
Even if I manually set pDevMode->dmOrientation to DMORIENT_LANDSCAPE from the event handler of the custom print dialog, the printing result is still ended up as portrait. I am really not sure why this is happening and how to modify the DevMode after the print dialog is up.
Thank you in advance for any help.
Here is the code I have:
void PrintSomething(CWnd* currentWnd) {
// Create CPrintInfo
CPrintInfo* pPrintInfo = new CPrintInfo;
SetupPrintInfo(pPrintInfo); // simply setup some member variables of CPrintInfo
// Create a custom print dialog
CustomPrintDlg* pCustomPrtDlg = new CustomPrintDlg(FALSE, PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS
| PD_HIDEPRINTTOFILE | PD_NOSELECTION, pPrintInfo, currentWnd);
SetupPrintDialog(pPrintInfo,pCustomPrtDlg);
if ( AfxGetApp()->DoPrintDialog(pCustomPrtDlg) == IDOK ) {
... // proceed a print loop
}
}
Code for setting up the custom print dialog:
void SetupPrintDialog(CPrintInfo* pPrintInfo,CustomPrintDlg* pCustomPrtDlg) {
delete pInfo->m_pPD;
pInfo->m_pPD = pCustomPrtDlg;
pInfo->m_pPD->m_pd.hInstance = AfxGetInstanceHandle();
pInfo->m_pPD->m_pd.lpPrintTemplateName = MAKEINTRESOURCE(IDD_CUSTOM_PRTDLG);
// Set the Flags of the PRINTDLG structure as shown, else the
// changes will have no effect.
pInfo>m_pPD->m_pd.Flags |= PD_ENABLEPRINTTEMPLATE;
// Set the page range.
pInfo>m_pPD->m_pd.nMinPage = 1; // one based page numbers.
pInfo>m_pPD->m_pd.nMaxPage = 0xffff; // how many pages is unknown.
}
When a user toggles the radio button to Landscape, this function will be invoked:
void CustomPrintDlg::OnLandscapeChecked() {
// set the current Devmode to landscape
LPDEVMODE pDevMode = GetDevMode();
GlobalUnlock(pDevMode);
pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
}
A pseucode for the custom print dialog class:
class CustomPrintDlg: public CPrintDialog {
... // just override some methods from CPrintDialog
};
Thanks again,
Unagi
I figured out the solution:
All I need is to call GlobalLock to obtain a pointer to the Devmode before changing the current DevMode.
void CustomPrintDlg::OnLandscapeChecked()
{
// set the current Devmode to landscape
LPDEVMODE pDevMode = GetDevMode();
GlobalLock(pDevMode);
pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
GlobalUnlock(pDevMode)
}
Thanks again for helping me.
Nowhere in your example code do you show how you're creating the DC for printing. When you call CreateDC, you must pass a pointer to a DEVMODE structure; this defines whether the printing will be portrait or landscape.

How to get the role of a QPushButton created by a QDialogButtonBox?

I'm trying get all the button child widgets of a currently active window. The buttons were created through QDialogButtonBox. I'm trying to get the roles of each button so I can identify which button is the OK, CANCEL, or SAVE button. However I'm getting an error with the following code:
QWidget *pWin = QApplication::activeWindow();
QList<QPushButton *> allPButtons = pWin->findChildren<QPushButton *>();
QListIterator<QPushButton*> i(allPButtons);
while( i.hasNext() )
{
QDialogButtonBox *pButtonRole = new QDialogButtonBox();
QDialogButtonBox::ButtonRole role = pButtonRole->buttonRole(i.next());
qDebug() << "buttonRole: " << role << endl ;
//the value of role here is -1, which means it's an invalid role...
}
I'm getting a negative value when getting the button's role :(
Can somebody tell me what's wrong with the code?
You can't call a non-static method like that. You need to have the QDialogButtonBox variable and call that particular instance for buttonRole() to work.
QDialogButtonBox::ButtonRole role = myButtonBoxPtr->buttonRole(i.next());
You are creating a new empty QDialogButtonBox which has no idea about buttons in allPButtons list. Calling buttonRole() on them returns -1 (InvalidRole) because buttons are not in that button-box.
You must do as jkerian wrote and myButtonBoxPtr must point to the QDialogButtonBox which is already in your window.
You can try something like this (if you have one ButtonBox):
QDialogButtonBox *box = pWin->findChild<QDialogButtonBox *>();
foreach(QAbstractButton* button, box->buttons())
{ qDebug() << box->buttonRole(button); }