MFC Dialog controls goes invisible while running - visual-c++

I created a MFC application.Sometimes the controls(Button,Label etc) in the Dialog goes invisible at run time.The Dialog form remains.This is a random issue.The screenshots of the dialog in normal time and when goes blank are attached.Can anyone help me to find the solution for this ?
http://i.stack.imgur.com/KTDGV.png
http://i.stack.imgur.com/3PqUb.png
for displaying/hiding the Dialog i used the following code
void CVideoConverter::PopUpDlg(BOOL bValue)
{
try
{
if(bValue) // show
{
CVideoConverterApp::m_pCVideoConverterDlg->ShowWindow(SW_SHOWNORMAL);
CVideoConverterApp::m_pCVideoConverterDlg->UpdateWindow();
}
else
{// hide
CVideoConverterApp::m_pCVideoConverterDlg->ShowWindow(SW_MINIMIZE);
}
}
catch(...)
{}
}
The following code was used to position the dialog to bottom right corner of the window.This is called before the PopupDlg()
void CVideoConverter::SetWindowToBottomRightCorner()
{
try
{
CRect rcScreen;
SystemParametersInfo(SPI_GETWORKAREA, 0, (void *) &rcScreen, 0);
CRect rcWindow;
GetWindowRect(&rcWindow);
MoveWindow(rcScreen.right - rcWindow.Width(), rcScreen.bottom - rcWindow.Height(), rcWindow.Width(), rcWindow.Height(), TRUE);
}
catch(...)
{}
}

Related

Drawing Tool with Processing

I am trying to create a little drawing tool with processing. The final drawing should be exportable as a .svg file – so i thought this to be pretty easy… but actually it isnt…
I put the background function into setup – to be able to draw – the safed svg file unfortunately only contains a single frame – and not the whole drawing. :-(
What am I missing – how could I achieve that! I would be thankful for any kind of help!
This is my code so far:
import processing.svg.*;
boolean record;
void setup () {
size(1080, 1080);
background(255);
}
void draw() {
if (record) {
beginRecord(SVG, "frame-####.svg");
}
fill(255);
strokeWeight(1);
ellipse(mouseX, mouseY, 100, 100);
if (record) {
endRecord();
record = false;
}
}
void mousePressed() {
record = true;
}
Tried different things in organizing the code lines in different orders – but could not manage it…
Thank you!
That's because you're creating an image every time you beginRecord and endRecord. If you want to save the image as you see it, you can use save(fileName.png) instead. Here's a code snippet to demonstrate:
void setup() {
size(800, 600);
background(255);
fill(255);
strokeWeight(1);
}
void draw() {
ellipse(mouseX, mouseY, 100, 100);
}
void mousePressed() {
save("myImage.png");
}
If, on the other hand, you really want to use beginRecord, know that it'll save everything you draw between beginRecord and endRecord. You could programatically create an image file this way, as an example, but you cannot just add snapshots to an existing image (which is why you only see "one frame" with your current code). Every time you begin recording, you create a new image. I'm not especially familiar with this method, but one obvious way to do things would be to "save" whatever the user is doing and reproduce those instructions to save them. Here's an example which does this (it saves when you right-click, and I also took the liberty of drawing only when the left mouse button is down):
import processing.svg.*;
boolean record;
ArrayList<PVector> positionsList;
void setup() {
size(800, 600);
positionsList = new ArrayList<PVector>();
}
void draw() {
background(255);
fill(255);
strokeWeight(1);
for (PVector p : positionsList) {
ellipse(p.x, p.y, 100, 100);
}
ellipse(mouseX, mouseY, 100, 100);
if (record) {
positionsList.add(new PVector(mouseX, mouseY));
}
}
void mousePressed() {
record = mouseButton == LEFT;
if (mouseButton == RIGHT) {
beginRecord(SVG, "frame.svg");
fill(255);
strokeWeight(1);
for (PVector p : positionsList) {
ellipse(p.x, p.y, 100, 100);
}
endRecord();
}
}
void mouseReleased() {
record = false;
}
While drawing:
The file (as a png here but it was saved as a svg on my computer):
Hope it helps. Have fun!
import processing.pdf.*;
PShape shape;
void setup () {
size(1080, 1080);
beginRecord(PDF, "drawing.pdf");
shape = loadShape("shape.svg");
shapeMode(CENTER);
background(0,255,0);
}
void draw() {
shape.disableStyle();
fill(255);
strokeWeight(10);
shape(shape, mouseX, mouseY, 200, 200);
}
void keyPressed() {
if (key == 's') {
endRecord();
exit();
}
}

Popup menu in MFC from button click handler not working

// CMFCApplication1Dlg dialog
CMFCApplication1Dlg::CMFCApplication1Dlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CMFCApplication1Dlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CMFCApplication1Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &CMFCApplication1Dlg::OnBnClickedButton1)
END_MESSAGE_MAP()
// CMFCApplication1Dlg message handlers
BOOL CMFCApplication1Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CMFCApplication1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMFCApplication1Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMFCApplication1Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CMFCApplication1Dlg::OnBnClickedButton1()
{
//Simulating the similar code as product
CFrameWnd *pFrame = GetParentFrame();
CMenu popup;
popup.CreatePopupMenu();
LPCWSTR pszMenuItem2 = L"Korean with wchar_t: 기존 운";
AppendMenuW(popup.m_hMenu, MF_STRING, 1, pszMenuItem2);
TCHAR* pszMenuItem3 = "|| Korean without wchar_t: 또는 차량 삽입";
AppendMenu(popup.m_hMenu, MF_STRING, 2, pszMenuItem3);
UINT nCmd = popup.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD,
14, 20, pFrame, 0);
}
I have written above piece of code to generate popup menu in the screen on button click but it doesn't show any menu at all? Am I missing something?
As you asked here is the complete code. Actually I have placed one static button through GUI and on button click trying to generate the popup menu.
These button is not created dynamically. Also, the entire code here is generated by wizard except the last portion of the code about the button click.
The problem was in the handle, if I use the right API to get the handle, it shows the popup menu.
Here is the updated code.
void CMFCApplication1Dlg::OnBnClickedButton1()
{
//Simulating the similar code as product
HWND wnd= GetSafeHwnd();//<---- this had to change
CMenu popup;
popup.CreatePopupMenu();
LPCWSTR pszMenuItem2 = L"Korean with wchar_t: 기존 운";
AppendMenuW(popup.m_hMenu, MF_STRING, 1, pszMenuItem2);
TCHAR* pszMenuItem3 = "|| Korean without wchar_t: 또는 차량 삽입";
AppendMenu(popup.m_hMenu, MF_STRING, 2, pszMenuItem3);
UINT nCmd = popup.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD,
14, 20, CWnd::FromHandle(wnd)/*need this api to get the CWnd from HWND*/, 0);

MFC menu is not drawing but white blank

Im working on some Window Customizing stuff.
I removed title bar and border of CFrameWnd and added my own title bar.
It seems work nice.
But if I add menu to this window, menu will be on top of this window. (above my title bar)
So I add new CFrameWnd and set my menu to this new CFrameWnd.
this is the code.
CMenu * pMenu = this->GetMenu();
if( pMenu != NULL )
{
this->SetMenu(NULL);
m_pMenuWnd = new CMenuWindow();
m_pMenuWnd->Create(NULL, NULL, 0, m_WindowRects.MenuRect, this, 0, NULL);
m_pMenuWnd->SetMenu(pMenu);
m_pMenuWnd->ShowWindow(SW_NORMAL);
}
And I added this menu window's move handler on my custom CFrameWnd's OnSize, OnMove Message Handler for corrent positionning.
It looks pretty good.
But when I minimize and restore its position, menu windows position is just white blank.
But if I move cursor on there, menus r there.
What am I doing wrong?
I checked OnCtlColor but its not even get called.
Here's CMenuWindow code
BEGIN_MESSAGE_MAP(CMenuWindow, CFrameWnd)
ON_WM_CREATE()
ON_WM_ACTIVATE()
ON_WM_KEYUP()
ON_WM_SYSKEYUP()
END_MESSAGE_MAP()
void CMenuWindow::OnDraw(CDC* pDC)
{
CRect clRect;
GetClientRect(&clRect);
}
// CMenuView message handlers
int CMenuWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
ModifyStyle(WS_CAPTION, WS_CLIPCHILDREN);
ModifyStyleEx(WS_EX_CLIENTEDGE,0);
return 0;
}

MFC: Content menu of a second modal dialog not showing

I have a modal dialog with a list control. Right clicking one of the items in the list control, shows a content menu. Clicking this popup menu, opens another modal dialog with another list control.
My problem is that I can't display a right click content menu for the list control in the second dialog.
I tried:
void CMyListCtrl::OnContextMenu(CWnd* , CPoint point)
{
if (GetSelectedCount() == 0)
return ;
if (point.x == -1 && point.y == -1)
{
//keystroke invocation
CRect rect;
GetClientRect(rect);
ClientToScreen(rect);
point = rect.TopLeft();
point.Offset(5, 5);
}
CMenu menu;
VERIFY(menu.LoadMenu(IDR_HISTORY_MENU));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = this->GetParent();
SetForegroundWindow();
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
point.x,
point.y,
pWndPopupOwner);
}
and I also added to the parent dialog ON_WM_INITMENUPOPUP
Also tried just to create a popup menu on the fly
void CHistoryListCtrl::OnContextMenu(CWnd* , CPoint point)
{
if (GetSelectedCount() == 0)
return ;
if (point.x == -1 && point.y == -1)
{
//keystroke invocation
CRect rect;
GetClientRect(rect);
ClientToScreen(rect);
point = rect.TopLeft();
point.Offset(5, 5);
}
CMenu addMenu;
addMenu.CreatePopupMenu();
// add all possible interface items
CString mstr; mstr="Compare";
addMenu.AppendMenu(MF_ENABLED | MF_STRING,ID_HISTORY_COMPARE , (LPCTSTR)mstr);
addMenu.TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON,point.x,point.y,this,NULL);
addMenu.DestroyMenu();
}
It didn't work as well. Tried to add SetForgroundWindow before the call to TrackPopupMenu, but it also failed.
Any idea what to do ?

UISearchBar disable auto disable of cancel button

I have implemented a UISearchBar into a table view and almost everything is working except one small thing: When I enter text and then press the search button on the keyboard, the keyboard goes away, the search results are the only items shown in the table, the text stays in the UISearchBar, but the cancel button gets disabled.
I have been trying to get my list as close to the functionality of the Apple contacts app and when you press search in that app, it doesn't disable the cancel button.
When I looked in the UISearchBar header file, I noticed a flag for autoDisableCancelButton under the _searchBarFlags struct but it is private.
Is there something that I am missing when I setup the UISearchBar?
I found a solution. You can use this for-loop to loop over the subviews of the search bar and enable it when the search button is pressed on the keyboard.
for (UIView *possibleButton in searchBar.subviews)
{
if ([possibleButton isKindOfClass:[UIButton class]])
{
UIButton *cancelButton = (UIButton*)possibleButton;
cancelButton.enabled = YES;
break;
}
}
I had to tweak this a bit to get it work for me in iOS7
- (void)enableCancelButton:(UISearchBar *)searchBar
{
for (UIView *view in searchBar.subviews)
{
for (id subview in view.subviews)
{
if ( [subview isKindOfClass:[UIButton class]] )
{
[subview setEnabled:YES];
NSLog(#"enableCancelButton");
return;
}
}
}
}
There is two way to achieve this easily
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
// The small and dirty
[(UIButton*)[searchBar valueForKey:#"_cancelButton"] setEnabled:YES];
// The long and safe
UIButton *cancelButton = [searchBar valueForKey:#"_cancelButton"];
if ([cancelButton respondsToSelector:#selector(setEnabled:)]) {
cancelButton.enabled = YES;
}
}
You should go with the second one, it will not crash your application if Apple will change it in the background.
BTW i tested it from iOS 4.0 to 8.2 and no changes, also i use it in my Store approved application without any issues.
This is what made it to work on iOS 6 for me:
searchBar.showsCancelButton = YES;
searchBar.showsScopeBar = YES;
[searchBar sizeToFit];
[searchBar setShowsCancelButton:YES animated:YES];
Here's my solution, which works for all situations in all versions of iOS.
IE, other solutions don't handle when the keyboard gets dismissed because the user dragged a scroll view.
- (void)enableCancelButton:(UIView *)view {
if ([view isKindOfClass:[UIButton class]]) {
[(UIButton *)view setEnabled:YES];
} else {
for (UIView *subview in view.subviews) {
[self enableCancelButton:subview];
}
}
}
// This will handle whenever the text field is resigned non-programatically
// (IE, because it's set to resign when the scroll view is dragged in your storyboard.)
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
[self performSelector:#selector(enableCancelButton:) withObject:searchBar afterDelay:0.001];
}
// Also follow up every [searchBar resignFirstResponder];
// with [self enableCancelButton:searchBar];
As per my answer here, place this in your searchBar delegate:
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
dispatch_async(dispatch_get_main_queue(), ^{
__block __weak void (^weakEnsureCancelButtonRemainsEnabled)(UIView *);
void (^ensureCancelButtonRemainsEnabled)(UIView *);
weakEnsureCancelButtonRemainsEnabled = ensureCancelButtonRemainsEnabled = ^(UIView *view) {
for (UIView *subview in view.subviews) {
if ([subview isKindOfClass:[UIControl class]]) {
[(UIControl *)subview setEnabled:YES];
}
weakEnsureCancelButtonRemainsEnabled(subview);
}
};
ensureCancelButtonRemainsEnabled(searchBar);
});
}
None of the answers worked for me at all. I'm targeting iOS 7. But I found an answer.
What I'm trying is something like the Twitter iOS app. If you click on the magnifying glass in the Timelines tab, the UISearchBar appears with the Cancel button activated, the keyboard showing, and the recent searches screen. Scroll the recent searches screen and it hides the keyboard but it keeps the Cancel button activated.
This is my working code:
UIView *searchBarSubview = self.searchBar.subviews[0];
NSArray *subviewCache = [searchBarSubview valueForKeyPath:#"subviewCache"];
if ([subviewCache[2] respondsToSelector:#selector(setEnabled:)]) {
[subviewCache[2] setValue:#YES forKeyPath:#"enabled"];
}
I arrived at this solution by setting a breakpoint at my table view's scrollViewWillBeginDragging:. I looked into my UISearchBar and bared its subviews. It always has just one, which is of type UIView (my variable searchBarSubview).
Then, that UIView holds an NSArray called subviewCache and I noticed that the last element, which is the third, is of type UINavigationButton, not in the public API. So I set out to use key-value coding instead. I checked if the UINavigationButton responds to setEnabled:, and luckily, it does. So I set the property to #YES. Turns out that that UINavigationButton is the Cancel button.
This is bound to break if Apple decides to change the implementation of a UISearchBar's innards, but what the hell. It works for now.
Here's a Swift 3 solution that makes use of extensions to get the cancel button easily:
extension UISearchBar {
var cancelButton: UIButton? {
for subView1 in subviews {
for subView2 in subView1.subviews {
if let cancelButton = subView2 as? UIButton {
return cancelButton
}
}
}
return nil
}
}
Now for the usage:
class MyTableViewController : UITableViewController, UISearchBarDelegate {
var searchBar = UISearchBar()
func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
tableView.tableHeaderView = searchBar
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
DispatchQueue.main.async {
if let cancelButton = searchBar.cancelButton {
cancelButton.isEnabled = true
cancelButton.isUserInteractionEnabled = true
}
}
}
}
For Monotouch or Xamarin iOS I have the following C# solution working for iOS 7 and iOS 8:
foreach(UIView view in searchBar.Subviews)
{
foreach(var subview in view.Subviews)
{
//Console.WriteLine(subview.GetType());
if(subview.GetType() == typeof(UIButton))
{
if(subview.RespondsToSelector(new Selector("setEnabled:")))
{
UIButton cancelButton = (UIButton)subview;
cancelButton.Enabled = true;
Console.WriteLine("enabledCancelButton");
return;
}
}
}
}
This answer is based on David Douglas solution.
A more complete answer:
since iOS 7, there is an additional level of subviews under the searchBar
a good place to enable the cancel button is in searchBarTextDidEndEditing
.
extension MyController: UISearchBarDelegate {
public func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
DispatchQueue.main.async {
// you need that since the disabling will
// happen after searchBarTextDidEndEditing is called
searchBar.subviews.forEach({ view in
view.subviews.forEach({ subview in
// ios 7+
if let cancelButton = subview as? UIButton {
cancelButton.isEnabled = true
cancelButton.isUserInteractionEnabled = true
return
}
})
// ios 7-
if let cancelButton = subview as? UIButton {
cancelButton.isEnabled = true
cancelButton.isUserInteractionEnabled = true
return
}
})
}
}
}
Time passes, but the problem is still there...
Elegant Swift 5/iOS 13 solution:
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
for case let cancelButton as UIButton in searchBar.subviews {
cancelButton.isEnabled = true
}
}

Resources