How to Enable Main Items & Button in Application Button in MFC - visual-studio

I'm developing an MFC application dedicated to Windows 7 GUI on visual studio 2013. What I've done so far is I've added Main Item and Button into Application Button by getting some help from http://msdn.microsoft.com/en-us/library/ee354414.aspx as you can see in below image.Now, I want to know that how can I add Event Handler into it.

Sorry, I'm giving answer of my own question.I've done this by adding few line of code show below.In MainFrm.h I've declare two public method
afx_msg void OnMainItemKasim();
afx_msg void OnButtonKasim();
In MainFrm.cpp. Define empty stub of both method
void CMainFrame::OnMainItemKasim()
{
// TODO: Add your command handler code here
}
void CMainFrame::OnButtonKasim()
{
// TODO: Add your command handler code here
}
finally, write following lines in BEGIN_MESSAGE_MAP
ON_COMMAND(ID_FILE_KASIM, &CMainFrame::OnMainItemKasim)
ON_COMMAND(ID_APP_KASIM, &CMainFrame::OnButtonKasim)
where, ID_FILE_KASIM is Main Item below Save As & ID_APP_KASIM is button next to Exit button
finally it looks like this

Related

VisualStudio customize paste operation for my component at design-time

I created WinForm component and I want to replace some properties of my component when developer copy and then paste(not when copy but when paste) component from clipboard at design time.
VisualStudio creates new copy of component and assign properties so it became copy of the source component.
I need to replace some properties on paste operation depending on the selected component.
It is very similar to standard Copy/Paste operation with Control component. When designer change Parent of component if developer select other container (like Panel) before Paste Control.
I think that code to perform it should be somewhere in my ComponentDesigner class.
I explored ComponentDesigner methods but can't find any methods that controls clipboard operations.
You can override the OnParentChanged method of your component, which is executed when the component is pasted onto the form. Then test the DesignMode property to make sure you are in design mode:
public class MyComponent : Label
{
protected override void OnParentChanged(EventArgs e)
{
if (DesignMode) {
// Change properties as desired.
Text = "Design";
}
base.OnParentChanged(e);
}
}
When the component is dropped from the Toolbox, this code is not executed. (I can't explain why, but it happens to be exactly what we need.)
If you derived your component from System.ComponentModel.Component, you can override the property Site; however, this will require some more logic to check whether the component has been pasted.
public override ISite Site
{
get {
return base.Site;
}
set {
base.Site = value;
if (value?.Container is IDesignerHost dh &&
dh.TransactionDescription == "Paste components") {
MessageBox.Show("Pasted");
}
}
}
But probably the transaction description is localized, because it is the text that you see in the drop-down of the Undo button on the toolbar of Visual Studio after having pasted the component.

onPrepareOptionsMenu acts weird if API is called from inside

I have a need to enable and disable menu items based on the API call response and it has to be called everytime the menu shows up.
I need asynctask because I have to show progressbar
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
handleMenuItems(menu)
}
private void handleMenuItems(menu)
{
new AsyncTask<Void, Void, Void>(){
#Override
protected Void doInBackground(Void... params) {
//API call
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (progressDialog != null)
progressDialog.hide();
//necessary menu items are enabled and disabled
super.onPrepareOptionsMenu(menu);
}
#Override
protected void onPreExecute() {
if (progressDialog != null){
progressDialog.setMessage("Checking");
progressDialog.show();
}
super.onPreExecute();
}
}.execute();
}
Whenever I touch the options menu the onPrepareOptionsMenu gets called and the options menu doesn't appear then when I press it again the onPrepareOptionsMenu doesn't get called and the options menu appears.
I want the api to get called every time and menu to be shown whenever I touch for options menu.
You shouldn't show and hide progress dialog in OnPrepareOptionsMenu
Comment out the progress dialog codings and rest of the codes are ok and works as you expected
Note:
You have called super.OnPrepareOptionsMenu in OnPostExecute(UI
Thread)
As the progress bar is not needed don't remove the AsyncTask
otherwise app my crash
I don't think this code does what you expect it to do. When onPrepareOptionsMenu is called the first time, it only schedules the AsyncTask to run at a future time and not right away. Next it executes the super.onPrepareOptionsMenu line which should show whatever the option menu is set to be at that time. I think (have to check on that) the UI will then wait for the user input as it is showing some sort of a menu (maybe not what you had expected). When the menu is touched for the second time, I think it will dismiss the menu and that might be the time that the AsyncTask is executed which results in the first menu that you had expected in the first place. The second instance of the AsyncTask will not run until the first one finishes (unless executeOnExecutor is called instead of execute, but that is not the case here nor I believe it to help the expected outcome.)
You might want to have a "dummy" menu prepared (maybe something that shows "waiting...") and start a background thread where it builds up the right menu and then when it is ready update and display a new menu (programmatically).
I personally would advise in finding a way to build and show the menu on the UI thread (maybe prep the menu as the changes are made, ahead of time, and just show it when needed.
Kaamel

Pebble - Override ScrollLayer's select button callback function

I'm trying to implement a button action that displays text on my ScrollLayer when the Select button is pressed. I used:
scroll_layer_set_click_config_onto_window(scrollLayer, window);
to set the BUTTON_UP and BUTTON_DOWN callback functions automatically (to scroll). So then I want to set a BUTTON_SELECT callback function. I wrote these two functions:
//Performs an action whenver the 'select' button is pressed
void select_click_handler(ClickRecognizerRef recognizer, void *ctx) {
text_layer_set_text(textLayer, "You pressed select");
}
//Links the select button to a function, select_click_handler
void click_config_provider(void *ctx) {
window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler);
}
And in my initializer I have:
void init() {
window = window_create();
window_set_window_handlers(window, (WindowHandlers) {
.load = window_load,
.unload = window_unload,
});
window_set_click_config_provider(window, click_config_provider);
window_stack_push(window,true);
}
When I run this, it compiles and installs on the watch just fine, however, when I hit the select button, nothing happens. When I comment out scroll_layer_set_click_config_onto_window(scrollLayer, window);
it works like it is supposed to.
Is there a way to override the select button callback without having to comment out the above line?
Thanks in advance!
I found a simple workaround using scroll_layer_set_callbacks() function here:
http://forums.getpebble.com/discussion/11432/scroll-layer-set-callbacks
Now you can find it in Pebble official document(SDK 2.0).
ScrollLayer // Pebble Developers
http://developer.getpebble.com/docs/c/User_Interface/Layers/ScrollLayer/
The SELECT button can be configured by installing a click configuration provider using scroll_layer_set_callbacks().

How do I get notification from a `CEdit` box?

I have a CEdit box where a user can enter relevant information. As soon as he\she starts writing in the box, I need a notification so that I can call doSomething() to perform some other task. Does Windows provide a callback, and if so, how do I use it?
With MFC there's no callback as such, rather you do this by implementing a handler for the appropriate event. You need to handle one of two events: WM_CHAR or EN_CHANGE
Handle the dialog's EN_CHANGE for example duplicating in realtime the entered text elsewhere on the dialog. You need to firstly add an entry in the dialog's message map, and secondly override the appropriate handler:
BEGIN_MESSAGE_MAP(CstackmfcDlg, CDialog)
ON_EN_CHANGE(IDC_EDIT1, &CstackmfcDlg::OnEnChangeEdit1)
END_MESSAGE_MAP()
void CstackmfcDlg::OnEnChangeEdit1()
{
CString text;
m_edit.GetWindowText(text);
m_label.SetWindowText(text); // update a label control to match typed text
}
Or, handle the editbox class's WM_CHAR for example preventing input of certain characters, e.g. ignore anything other than a digit for numerical entry. Derive a class from CEdit, handle the WM_CHAR event of that class (not the dialog) and make your edit control an instance of that class.
BEGIN_MESSAGE_MAP(CCtrlEdit, CEdit)
ON_WM_CHAR()
END_MESSAGE_MAP()
void CCtrlEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// Do nothing if not numeric chars entered, otherwise pass to base CEdit class
if ((nChar >= '0' && nChar <= '9') || VK_BACK == nChar)
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
Note that you can use the VS IDE to put in stubs for the handler overrides by using the Properties bar with the mouse selection in the message map block.
EDIT: Added example code, and corrected explanation of WM_CHAR which I had wrong.
If you double click on the edit box in the resource editor it automatically creates the OnEnChanged event for you.
The following assumes that you have an MFC dialog application.
The class wizard can be started with a right-click:
Double-click the Control ID (has an icon with a small green plus) of the new edit control to add the corresponding member variable to the class.
The class and event wizards will update the class definition and add a CEdit member:
afx_msg void OnEnChangeEdit1(); // Added by event wizard
CEdit m_edit1; // member added by class wizard
The class wizard will update the function:
void CMFCApplication5Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT1, m_edit1); // new variable added with class wizard
}
Double-clicking the control or right-clicking and selecting the add event wizard will update the message map and create the function declaration and definition:
BEGIN_MESSAGE_MAP(CMFCApplication5Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_EN_CHANGE(IDC_EDIT1, &CMFCApplication5Dlg::OnEnChangeEdit1) // new event handler added with wizard
END_MESSAGE_MAP()
Finally the code may be updated to interact with the edit control:
void CMFCApplication5Dlg::OnEnChangeEdit1()
{
// TODO: Add your control notification handler code here
CString text;
m_edit1.GetWindowText(text);
//m_edit1.SetWindowText(text);
}

Creating Dialogs with Silverlight Prism

I'm new to creating Silverlight applications. I have inherited a code-base that I've been told is using Prism. Based upon what I've seen in the code-base, this looks to be true. My problem is, I am just trying to open a dialog window and close a dialog window.
To open the dialog window, I'm using the following:
UserControl myDialog = new MyDialog();
IRegion region = myRegionManager.Regions["DIALOG_AREA"];
IRegionManager popupRegionManager = region.Add(myDialog, null, true);
region.Activate(myDialog);
The dialog I have designed appears. It has two buttons: "OK" and "Cancel". When a user clicks on of these dialogs, I want to close the dialog. My problem is, I have no idea how to do this. Because the dialog is not a ChildWindow, I cannot call this.Close(). But, when I change MyDialog to a ChildWindow, it is wrapped in some custom window chrome that i can't figure out how to get rid of.
How do I close a dialog in Prism? Thank you!
Instead of putting in a different region, you should make your ViewModel call a service where you call your dialog window.
public MainPageViewModel(IMainPage view,
ILoginBox loginBox )
: base(view)
{
this.view = view;
this.loginBox = loginBox;
}
public interface ILoginBox
{
void Show();
}
remember to use the IOC container and declare on your ModuleClass:
protected override void RegierTypes()
{
base.Container.RegisterType<ILoginBox , LoginBox>();
}
I hope it helps.
If you are still looking for an example, check:
another Stackoverflow sample

Resources