Win32 Bitmap on menuItem and checkmark - winapi

Is it possible to checkmark a menuitem showing a bitmap for an app running on Windows 7 (visual styles active)?. I only get a blue rectangle surrounding the place where the checkmark should be:
.
The code i'm using to configure the menuitem is:
MENUITEMINFO mii = { 0 }; //all members null
mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_TYPE | MIIM_STATE;
mii.fType = MFT_BITMAP;
mii.fState = MFS_CHECKED;
mii.dwTypeData = (LPSTR)bmp; //the bitmap shown in the menuItem
SetMenuItemInfo(hmenu, itemID, FALSE, &mii);

Related

Xamarin Forms vertical align image at bottom programmatically

In the (relatively) new Xamarin Forms I'm trying to vertically align an image to the bottom of a scrollview.
This is my code, this does exactly what I want (for larger images it scrolls). But when I have an image which is smaller than the height of the device, it should align to the bottom of the screen, instead of the center (probably default) of the screen. Since the documentation is (still) lacking, how can I achieve this in code?
return new ContentPage {
Content = new ScrollView {
Orientation = ScrollOrientation.Vertical,
BackgroundColor = Color.Black,
Content = new Image {
Source = ImageSource.FromFile (image)
}
}
};
I've tried it with this, but it gives an error that the call is ambiguous between the following methods or properties...
RelativeLayout rl = new RelativeLayout ();
rl.Children.Add (new ScrollView {
Orientation = ScrollOrientation.Vertical,
BackgroundColor = Color.Black,
Content = new Image {
Source = ImageSource.FromFile (image)
}
});
return new ContentPage {
Content = rl
};
From the Assembly Browser in Xamarin Studio, your options for Children.Add are:
void Add(T view, Expression<Func<Rectangle>> bounds);
void Add(T view, Expression<Func<double>> x = null, Expression<Func<double>> y = null, Expression<Func<double>> width = null, Expression<Func<double>> height = null);
void Add(T view, Constraint xConstraint = null, Constraint yConstraint = null, Constraint widthConstraint = null, Constraint heightConstraint = null);
Where Expression is the type from System.Linq.Expresssions.
You received an ambiguous call error because all these overloads have default values for all parameters besides view.
In order to use the object initializer for Children, you'll need to pass in your expressions or constraints like:
rl.Children.Add (
{
new ScrollView {
Orientation = ScrollOrientation.Vertical,
BackgroundColor = Color.Black,
Content = new Image {
Source = ImageSource.FromFile (image)
}
},
Constraint.Constant(0),
Constraint.Constant(0)
}
)
Constraint.RelativeToParent and Constraint.RelativeToView are useful in simple cases and Expression trees will solve arbitrary layout problems.

How do I use an image as the background of a gtk toolbar in GTK+2.0

Is there any api like gtk_widget_modify_bg() to modify the background of a gtk toolbar by an image.
gtk_widget_modify_bg() only can change the color of the background.
After one day's try, now I has known how to set an image backgroud for a gtk toolbar.
The keypoint is that you could not directly modify background for GtkToolbar or even GtkVBox (generally you would put GtkToolbar in a GtkVBox). Because they are all none window GtkWidget, so they couldn't catch expose-event, then can't draw their own background. Their background are same as their parent.
But the GtkEventBox can. So you can put the GtkToolbar into a GtkEventBox, and put GtkEventBox into a GtkVBox. Then you modify GtkEventBox's backgroud by an image, It seems GtkToolBar's background changed.
Following is my test codes:
int main(int argc, char* argv[])
{
GtkWidget* window;
GtkWidget* vbox;
GtkWidget* event_box;
GtkWidget* toolbar;
GtkToolItem* item;
gtk_init(&argc, &argv);
gtk_rc_parse("./gtk.rc");
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 250, 200);
gtk_window_set_title(GTK_WINDOW(window), "toolbar");
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
event_box = gtk_event_box_new();
//gtk_widget_set_name(vbox, "toolbar_event_box");
//set_event_box_background(event_box);
gtk_box_pack_start(GTK_BOX(vbox), event_box, FALSE, FALSE, 5);
toolbar = gtk_toolbar_new();
gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
gtk_container_set_border_width(GTK_CONTAINER(toolbar), 2);
item = gtk_tool_button_new_from_stock(GTK_STOCK_NEW);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
item = gtk_tool_button_new_from_stock(GTK_STOCK_OPEN);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
item = gtk_tool_button_new_from_stock(GTK_STOCK_SAVE);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
item = gtk_separator_tool_item_new();
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
item = gtk_tool_button_new_from_stock(GTK_STOCK_QUIT);
g_signal_connect(G_OBJECT(item), "clicked", G_CALLBACK(gtk_main_quit), NULL);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
gtk_container_add(GTK_CONTAINER(event_box), toolbar);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
and I use gtkrc file to set the GtkEventBox backgroud. Following is the gtk.rc:
pixmap_path "/usr/share/pixmaps:/usr/share/myself"
style "window"
{
bg_pixmap[NORMAL] = "firefox.png"
}
style "toolbar"
{
bg_pixmap[NORMAL] = "bk.bmp"
}
widget_class "GtkWindow" style "window"
widget_class "GtkEventBox" style "toolbar"
The result of the program run:
If you don't use gtkrc file, you can use gtk_widget_set_style() to change the GtkEventBox background. like this:
void set_event_box_background(GtkWidget* event_box)
{
GError* error = NULL;
GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file("/usr/share/myself/bk.bmp", &error);
GdkPixmap *pixmap = NULL;
GdkPixmap *mask = NULL;
gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &mask, 255);
GtkStyle* orig_style = gtk_widget_get_style(event_box);
GtkStyle* style = gtk_style_copy(orig_style);
style->bg_pixmap[GTK_STATE_NORMAL] = pixmap;
gtk_widget_set_style(event_box, style);
}
I get this method from Yuren's Info Area.
When you use this method, you should comment gtk_rc_parse("./gtk.rc"); and uncomment set_event_box_background(event_box); line at above main() function.

One button selected at a time in listbox (background color)

So I want to set the currently selected button to green background in a listbox while all buttons that are not selected should stay black. How can I do this. View examplecode below.. cant get it working though.
foreach(Button btn in ListBox.Items)
btn.Background = new SolidColorBrush(Colors.Black);
Button clickedButton = sender as Button;
clickedButton.Background = new SolidColorBrush(Colors.Green);
If you want it that way (without Binding and converters) here you go:
(I'm also assuming that only a button is in the listbox item)
for (int i = 0; i < ListBox.Items.Count; i++)
{
Button currentButton = ListBox.Items[i] as Button;
if(currentButton != null)
{
if (i == ListBox.SelectedIndex)
currentButton.Background = new SolidColorBrush(Colors.Green);
else
currentButton.Background = new SolidColorBrush(Colors.Black);
}
}

WP7 Popup not showing

In my app I want to display a simple string within a popup when the user clicks on an image. For this I added a Tap gesture listener to the image and within the handler I have the following code:
private void GestureListener_Tap( object sender, GestureEventArgs e )
{
var img = sender as Image;
if( img == null ) {
return;
}
Point pos = e.GetPosition( img );
string text = "I'm a popup!";
var popup = new Popup() {
Child = new Border() {
BorderBrush = new SolidColorBrush( Colors.LightGray ),
Child = new TextBlock() {
Text = text,
TextWrapping = TextWrapping.Wrap,
},
},
HorizontalAlignment = HorizontalAlignment.Stretch,
HorizontalOffset = pos.X,
VerticalOffset = pos.Y,
Visibility = Visibility.Visible,
};
popup.IsOpen = true;
Debug.WriteLine( "GestureListener_Tap: " + text );
}
The call to WriteLine prints in the debugger output window but the popup doesn't get displayed. What am I doing wrong here?
Thanks for your help!
I tried your code and the Popup is displayed. I think the problem for you is the Position for the Image relative to the Mouse. Try to set another Background for the Parent Container and I think you'll see the Popup. You can also try to play around with
Point pos = e.GetPosition(null);
until you get the Position you require

Adding WP7 ContextMenu programmatically

I'm loading elements on a page dynamically (reading the contents of an XML file). The dynamic content is loaded into a StackPanel. Each element of the content consists of a TextBlock and one other UI element, so for each pair I create a new StackPanel which is then added to the parent StackPanel. The code looks like this:
TextBlock header = new TextBlock() {
Text = "Heading 1",
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Top,
Foreground = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"],
};
TextBox item = new TextBox() {
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Top,
};
StackPanel sp = new StackPanel();
sp.Children.Add( header );
sp.Children.Add( item );
parentSP.Children.Add( sp );
I want to add a ContextMenu to this StackPanel (sp, not parentSP); depending on some parameters read from the file it could be one of 2 different context menus. I tried the following but it is not working:
ContextMenu cm = new ContextMenu();
RoutedEventHandler clickHandler = new RoutedEventHandler( OnContextMenuClicked );
// Add "edit" entry
MenuItem menuItem = new MenuItem() {
Header = "edit",
Tag = "edit",
};
menuItem.Click += clickHandler;
cm.Items.Add( menuItem );
// Add "delete" entry
menuItem = new MenuItem() {
Header = "delete",
Tag = "delete",
};
menuItem.Click += clickHandler;
cm.Items.Add( menuItem );
parentSP.Children.Add( cm );
How do I add a context menu to the StackPanel programmatically?
Also, is there a better way to solve this problem? Maybe by storing the 2 different types of context menus in a XAML resources section and adding them as needed? I tried doing this by adding the context menus to the parent's StackPanel.Resource section but got an error saying "A property element cannot be the direct child of another property element"
Thanks in advance for your help
ContextMenuService.SetContextMenu(sp, cm);

Resources