C++ /WinRT - TextBlock, how to dynamically attach and set its position (x, y) to XAML form? - c++

Below was Blank App (C++/WinRT) project.
I am trying to create a TextBlock and set its Text (x,y) property "Left" and "Top" dynamically from MainPage.cpp and display it on runtime XAML form.
However, for testing, code below able to compile successfully and at the runtime result, no TextBlock component "Hellow World!" is shown.
Is there anything wrong or missing ?
namespace winrt::...::implementation
{
MainPage::MainPage()
{
InitializeComponent();
Process();
}
void MainPage::Process()
{
winrt::hstring hs = L"Hello World!";
TextBlock tbx;
tbx.FontFamily( Windows::UI::Xaml::Media::FontFamily(
L"Segoe UI Semibold" ) );
tbx.FontSize(72.0);
tbx.Foreground( SolidColorBrush( Colors::Orange() ) );
tbx.VerticalAlignment( VerticalAlignment::Center );
tbx.TextAlignment( TextAlignment::Center );
tbx.Text( hs );
Window window = Window::Current();
window.Content( tbx );
window.Activate();
}
}
Please advise.

The underlying problem is a XAML layout issue. Here's a little demo that should put you on the right track. I modified your MainPage above to look like this:
MainPage::MainPage()
{
InitializeComponent();
winrt::hstring hs = L"Hello World!";
TextBlock tbx;
tbx.FontFamily(Windows::UI::Xaml::Media::FontFamily(
L"Segoe UI Semibold"));
tbx.FontSize(72.0);
tbx.Foreground(SolidColorBrush(Colors::Orange()));
tbx.VerticalAlignment(VerticalAlignment::Center);
tbx.TextAlignment(TextAlignment::Center);
tbx.Text(hs);
this->Content().as<Panel>().Children().Append(tbx);
}
The XAML page looks like this:
<Page ... >
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="myButton" >Click Me</Button>
</StackPanel>
</Page>
The basic idea is that you want to add the control to a panel that is managing layout on the main page, rather than adding it to the top level Window object. It may be instructive to play around in the designer a bit to get the desired look, then restructure it into equivalent code using C++ /WinRT.
Ben

Just discovered this works:
TranslateTransform pos;
pos.X( 500 );
pos.Y( 500 );
tbx.RenderTransform( pos )
Alternatively, found a latest video from Microsoft Build 2019, it shown a sample of UWP C++ with XAML with related info within.
Here is the link:
Meet C++/WinRT 2.0: Faster and smarter in the open - BRK4009
[https://www.youtube.com/watch?v=X41j_gzSwOY][1]

Related

Unit test on fixture in SVG generated with Batik

I'm trying to unit test generated svg in batik.
My development machine is windows,
my CI environment is Linux,
I use a a fixture to compare the svg result with my input, thus comparing the generated svg with what I've got in a file in my test/resource folder
I want my environments to be identical, so what I see in my development environment is what I get.
Initially I used the standard monospaced font.
To avoid the problem I tried the following: The documentation on Batik seems to suggest its possible to include the font in the generated svg to make it "self contained". I found out there are 2 options:
include the font definition
render the text themselves as strokes
That does not work either. Rendering takes place on the font definition before inclusion in the svg. I found there's a difference: java uses the system (windows/linux) font rendering to determine the font definition. They work differently. There are some articles on that topic.
But why render if you use monospaced fonts? The result should be the same, right? In the case of svg, I really would like to delegate rendering to the platform (browser or other tool) interpreting the svg.
My code:
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document.
Document document = domImpl.createDocument( "http://www.w3.org/2000/svg", "svg", null );
// embed fonts in svg document
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault( document );
ctx.setEmbeddedFontsOn( true );
GraphicsEnvironment ge;
try {
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont( Font.createFont( Font.TRUETYPE_FONT, this.getClass().getResourceAsStream( "/font/VeraMono.ttf" ) ) );
}
catch ( FontFormatException | IOException e ) {
LOG.error( "Cannot load font!", e );
}
// Create an instance of the SVG Generator: true converts text to paths.
SVGGraphics2D g2 = new SVGGraphics2D( ctx, true );
g2.setFont( new Font( "VeraMono", Font.PLAIN, 20 ) );
g2.drawString( "example text", 5, 30 );

MFC, How do I change the capiton of TabCtrl

I know the InsertItem is for add new tab in tabctrl, but I need change the caption of tab after create, I use keyword tabctrl and caption to search the old article ,but no relation problem to solve it, hot do I change the caption of tab page...
OK,
I got the solution, as follow code
TC_ITEM TabCtrlItem;
switch(m_tabCtrl.GetCurSel())
{
case TAB1:
TabCtrlItem.mask = TCIF_TEXT;
TabCtrlItem.pszText = _T("Some Text");
m_tabCtrl.SetItem(TAB1, &TabCtrlItem );

How to highlight the selected item of long list selector in windows phone 8

I've developed Windows Phone 8 application. In that I'm using the Long list selector to display items in the list. Everything is fine up till now. When the user clicks on any item of long list selector, I want to highlight that item's background color, so that the user clearly understands that he/she has selected an item.
Could you please tell me how to do this in windows phone 8.
I'm looking forward for the response.
http://code.msdn.microsoft.com/wpapps/Highlight-a-selected-item-30ced444
Detailed example of how to do it
I like to have more control of my application through code and avoid doing things in the xaml where it can get complicated. Below is a simpler way I feel and gives more control in code and require minimal changes in the xaml. It keeps the xaml nice and clean for what should be a really simple action.
Add a "BackColor" (or other string) property to your bound object
public string BackColor { get; set; }
Bind that property to something in your xaml like the background or a stack panel or the border color of a border, something that will present the visual change. E.g.
<StackPanel Orientation="Horizontal" Background="{Binding BackColor}">
In your long list selector code "SelectionChanged" event update the bound objects using the AddedItems and RemovedItems collections from SelectionChangedEventArgs e
if (e.AddedItems.Count > 0)
{
if (e.AddedItems[0] != null)
{
oMyObject = (MyServices.MyObjectDao)e.AddedItems[0];
oMyObject.BackColor = "Red";
}
}
if (e.RemovedItems.Count > 0)
{
if (e.RemovedItems[0] != null)
{
oMyObject = (MySercvices.MyObjectDao)e.RemovedItems[0];
oMyObject.BackColor = "Black";
}
}
You can use simple colors like in the example, or you can use any predefined colors from your xaml

How change the event in [XTK]

Hello,
I wonder how do I change my mouse event
because I would like to X.Caption activates only when I right-clicked on it.
thank you very much
If I'm correct ( other contributors, don't be afraid to correct me ), you might have to edit the internals of XTK for your needs. XTK has the "controls" defined in X.event.js for it's event such as HoverEvent and PanEvent which are acquired from the Google Closure Library. You can look into the Google Closure Library more as I haven't completely reviewed it, but X.caption is mostly derived from Goog.ui.Tooltip which can be used with the events from Goog.ui.Popup, Goog.ui.PopupBase, and Goog.events. You can try to include Goog.events into your Javascript and define your own click event or something related to that and look through the Google Closure Library some more.
If what I just wrote in the small paragraph above is not completely correct or anything like that, there are of course basic Javascript DOM Events for clicking. I'm not quite sure how those would work with XTK because XTK uses WebGL for rendering. You can review the basic events at http://www.w3schools.com/jsref/dom_obj_event.asp. If you find any solution for yourself, you are more than welcome to contribute it to XTK.
I think there are too ways no ? Change the xtk source code and compile your own version or something like this :
window.onload = function() {
myrenderer.config.HOVERING_ENABLED = false; // disabling show caption on mousehover
var mouse_x, mouse_y;
myrenderer.interactor.onMouseMove = function(event) {
mouse_x = event.offsetX;
mouse_y = event.offsetY;
}
myrenderer.interactor.onMouseDown = function(left,middle, right) {
if (right) {
var picked_id = myrenderer.pick(mouse_x, mouse_y);
if (picked_id==-1) throw new Error("No object");
var picked_object = myrenderer.get(picked_id);
var mycontainer = myrenderer.container;
var caption = new X.caption(mycontainer,mycontainer.offsetLeft + mouse_x + 10, mycontainer.offsetTop + mouse_y + 10,myrenderer.interactor);
caption.setHtml(picked_object.caption);
// use settimeout or something else to destroy the caption
}
}
}
The following JSFiddle shows how to do that. To actually display a tooltip etc. you can use any JS Library like jQuery UI.
http://jsfiddle.net/haehn/ZpX34/

CKeditor - Custom tags and symbols inside the editorwindow

When you insert a flash object into the CKeditor the editor window will show this symbol:
I was wondering. Is it possible to do something similar when users inserts this tag into the editor (using regex {formbuilder=(\d+)}/ ):
{formbuilder=2}
If so, could someone please explain how to? :)
UPDATE:
I've been looking at the PageBreak plugin to try and understand what the hell is going on. The big difference between this plugin and mine is the way the HTML is inserted into the editor.
CKEDITOR.plugins.add('formbuilder',
{
init: function(editor)
{
var pluginName = 'formbuilder';
var windowObjectReference = null;
editor.ui.addButton('Formbuilder',
{
label : editor.lang.common.form,
command: pluginName,
icon: 'http://' + top.location.host + '/publish/ckeditor/images/formbuilder.png',
click: function (editor)
{
if (windowObjectReference == null || windowObjectReference.closed){
var siteid = $('#siteid').val();
windowObjectReference = window.open('/publish/formbuilder/index.php?siteid='+siteid,'Formbuilder','scrollbars=0,width=974,height=650');
} else {
windowObjectReference.focus();
}
}
});
}
});
As you can see, my plugin opens a new window and the tag is inserted with:
function InsertForm(form_id)
{
// Get the editor instance that we want to interact with.
var oEditor = CKEDITOR.instances.page_content;
// Check the active editing mode.
if ( oEditor.mode == 'wysiwyg' )
{
// Insert the desired HTML.
oEditor.insertHtml( '{formbuilder='+form_id+'}' );
}
else
alert( 'You must be on WYSIWYG mode!' );
}
The PageBreak plugin does everything when you click on the toolbar icon. This makes it possible to make the fakeImage inside the plugin file. For me on ther other hand, I don't see how this is possible :\
I'm looking to solve a similar issue, except that all my stuff looks like XML. So like, <cms:include page="whatever" />. In your case, you would be able to copy the placeholder plugin and change the placeholder regex to match your tags. In my case, looks like I'll be modifying the iframe plugin or something, and hopefully figuring out how to add each of my tags as self-closing...