Toggle button in MFC - c++

How to make a toggle button in MFC dialog,?
Like the one you usually used in switching on the wifi in smart phone, push like switch button and radion buttons are not my needs,
Uptil now i reached to changing the switch button to push like effect but i need the real toggle button effect as describe about those in smartphones.
Remember that it supports dragging to on from off and vice versa.... :(

You can keep 2 images that are visible when button is pressed and one when button is not pressed. Now you can use CBitmapButton::LoadBitmaps or CButton::SetBitmap to change the image everytime the button is clicked. Make sure to invalidate the button so that new image can take effect.
//load your bitmaps (in constructor if dialog)
m_wifionBitmap.LoadBitmap(IDB_WIFION);
m_wifioffBitmap.LoadBitmap(IDB_WIFIOFF);
// In turn_on_wifi()
CButton* pButton = (CButton*)GetDlgItem(IDC_WIFI_TOGGLE_BUTTON);
pButton->SetBitmap(HBITMAP)m_wifionBitmap);
// In turn_off_wifi()
CButton* pButton = (CButton*)GetDlgItem(IDC_WIFI_TOGGLE_BUTTON);
pButton->SetBitmap(HBITMAP)m_wifioffBitmap);

Related

Qt transfer mouse move events to new window

I'm handling mouse events (through an event filter) on a QTabBar to detect when the user clicks and drags a tab to tear it off. When this happens, I remove the current widget from the QTabWidget and create a new top level widget that I add it to so that it's detached and floating, just like when you tear off a tab in Chrome. This new floating window is a custom frameless widget I made that has a custom titlebar that I handle mouse events on to allow the user to drag the window around the desktop.
The problem I'm having is that when you click and drag the tab to pull it off and the new top level window is created, I can't seem to get the application to continue dragging the new window without the user clicking and dragging on my titlebar. I'd like for the original drag motion to just transfer to the new widget so that the use can keep dragging it until he releases the mouse button.
I've tried creating a "fake" QMouseEvent to pass to my title bar (by calling QCoreApplication::sendEvent(object, event) to make it think it's been clicked on, but it doesn't receive any mouse move events unless you actually click on it. I'm open to other ideas.
Update: I added some debugging statements and it looks like once I detach the tab and create the new floating window, the QMainWindow continues to receive the mouse move events until I release the mouse button. I'll try adding some code to forward these mouse events on to the new floating window, but that feels kinda hacky.
Correction: The QMainWindow is not receiving the mouse move events, an object named "MainWindowWindow" is, which is a QWidgetWindow that I guess is a private type used to manage top level windows?
Ok, I got it working, but I don't like it. As I said in my correction, once the tab is detached, the QWidgetWindow starts receiving the mouse move events. This is a private type that's not exposed via the API.
What I ended up doing was installing an event filter on the application and looking for mouse events on an object that inherits from QWidgetWindow. I then directly call new drag() and endDrag() methods that I added to my floating window class. In my eventFilter method, it looks like this.
if (watched->inherits("QWidgetWindow"))
{
if (floater) // <- the floating window that was recently detached
{
if (event->type() == QEvent::MouseMove)
{
floater->drag(QCursor::pos()); // <-- Pass global cursor pos
}
else if (event->type() == QEvent::MouseRelease)
{
floater->endDrag();
floater = nullptr;
}
}
}
Like I said, if feels dirty because I A) am checking for events on a private type that I'm not really supposed to know about, and B) telling another window to drag around when it has code to do this itself. But it works, and I've spent enough time working on this. If someone has a more elegant solution, I'm happy to hear it.

Drag and drop a button using CCControlbutton

I am new to cocos2d-x and I am developing a game in x-code using cocos2d-x2.0.4. In my game I created a button using CCcontrolbutton. Now I want to drag my button to one place to another. I tried with all the CCControlEvents but it doesn't work. Now I want to know, is it possible to drag and drop a button using CCControlbutton. I pasted my code which I have used to create the button.
button1 = CCControlButton::create(CCScale9Sprite::create("7.png"));
button1->setAdjustBackgroundImage(false);
button1->setPosition( ccp(winwsize/6, winhsize/7) );
button1->addTargetWithActionForControlEvents(this, cccontrol_selector(plus::add),CCControlEventTouchDragOutside);
button1->addTargetWithActionForControlEvents(this, cccontrol_selector(plus::add),CCControlEventTouchDragInside);
this->addChild(button1, 4);
In add() I have given the code to enter next scene. But now it is entering while clicking the button. But i want while drag it to one position to another. If it is possible to drag a button using CCControlbutton then please give me some sample codes. Thanks.
The events CCControlEventTouchDragInside and CCControlEventTouchDragOutside are happening when the user's touch (his fingertip) is entering or leaving your button while already or still touching the touchscreen (or while the mouse button is still pressed while the mouse pointer moves).
You would need to observe the dragging process yourself. Starting with a click on your button, change the button's position while the user is dragging (to visualize the dragging process), and then call plus::add() when the dragging ends in your target area.

C++ Win32 Trying to create an owner drawn toggle button

I have been trying for a very long time (well a few days) to create a toggle button. A button having a up or down state.
Took over a day to realize it is not possible to create an owner drawn toggle button, a checkbox and pushlike does not work. When using owner drawn there is no difference between a checkbox or regular button (MSDN also notes you cant use owner drawn with any of those styles.)
From reading I found out you have to do it yourself, normally not a problem at all, but I cannot get any real "responsiveness." That is, if I click fast, nothing happens, sometimes I will click and it changes states, other times not and only updates when I click a different button.
I created a global variable for if the button should be shown in up or down state. In the commands I have it set that when the button, IDC_BTN_TOGGLE, it will set the opposite value on the bool.
Then the draw item part:
// button down
if ((pDIS->itemState & ODS_SELECTED) || showButtonDown) {
oldBrush = (HBRUSH)SelectObject(pDIS->hDC, theme.hBrush[BRUSH_BUTTON2]);
}
// button up
else {
oldBrush = (HBRUSH)SelectObject(pDIS->hDC, theme.hBrush[BRUSH_BUTTON]);
}
All my buttons are owner drawn and run through this. showButtonDown is only true when it is drawing IDC_BTN_TOGGLE and the top bool is true as well.
The normal buttons function normally, when I click them, it instantly shows the down state, release, back to normal, the toggle button is barely responsive.

Qt: Disable a Push Button without turning it gray

I need to make a checkable Push Button in Qt 4.8 that when is checked it becomes disabled.
The problem that I have is that the button turns gray and I need to keep it with the same color always. I have two questions for two possibles paths to follow:
1) Is there a way to disable the gray out effect when I use button.setEnabled(false)?
2) Is there a way to hook the click event so I can "simulate" the disabled property?
Thanks in advance!
[Edit] To give a little context, I have two push buttons that should toggle each other and that's why I need to prevent clicking on a pressed button.
Try button.blockSignals(true). You could also overwrite the look of the button when its disabled with a Qt style sheet.
To give a little context, I have two push buttons that should toggle
each other and that's why I need to prevent clicking on a pressed
button.
You should use QButtonGroup instead.
The only way that the button should be unchecked is when the user
checks the other button...
From doc:
In an exclusive group, the user cannot uncheck the currently checked
button by clicking on it; instead, another button in the group must be
clicked to set the new checked button for that group.
For disabling toolbutton and put icon of your choice instead of turning into grey
you have to load icon creating QPixmap object say qpm,
create QIcon object say icon
icon.addPixmap(qpm,QIcon::Disabled,QIcon::On) this line will show icon
setIcon on toolbutton
setStyleSheet
When enabling Toolbutton just change
3. icon.addPixmap(qpm,QIcon::Normal,QIcon::On)

How to SetFocus to a CButton so that the border and focus dotted line are visible?

I created a simple dialog-based application, and in the default CDialog added three buttons (by drag-and-dropping them) using the Visual Studio editor.
The default OK and Cancel buttons are there too.
I want to set the focus to button 1 when I click button 3.
I set the property Flat to true in the properties for muy buttons.
I coded this:
void CbuttonfocusDlg::OnBnClickedButton3()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_BUTTON1)->SetFocus();
Invalidate();
}
But the boder in button1 is never drawn. The caret (the dotted line indicating focus) is only drawn if I pressed TAB any time before clicking button 3.
I want the button to look exactly as it looks after I click it. Showing the dotted line inside the button programatically, would be a plus.
What I want:
What I get:
Use WM_NEXTDLGCTL.
See Reymond Chen's "How to set focus in a dialog box":
void SetDialogFocus(HWND hdlg, HWND hwndControl)
{
SendMessage(hdlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, TRUE);
}
By calling UpdateWindow, the button is being redrawn before the focus change can take effect. The Invalidate should be sufficient by itself, the window will get repainted when everything settles down.
This draws the thick border around the button:
static_cast<CButton*>(GetDlgItem(IDC_BUTTON1))->SetButtonStyle(BS_DEFPUSHBUTTON);
A more elegant way to do this would be to define a CButton member variable in CbuttonfocusDlg and associate it to the IDC_BUTTON1 control, and then calling
this->m_myButton.SetButtonStyle(BS_DEFPUSHBUTTON);
This makes the button to which I'm setting the focus the default button, but note that when the focus goes to a control (inside the dialog) that is not a button, the default button is once more the original default button set in the dialog resource, in this case the "Ok" button.
I am following Joel's suggestion. But slightly different with the API used in that link, my one is :
PostMessage(WM_NEXTDLGCTL, (WPARAM)(pwnd->GetSafeHwnd()), TRUE);