Wednesday, January 30, 2013
line selection in WPF richtextbox
Methods to highlight /or change the colors of line inside a WPF RichTextBox element.
Select a line according to its index number:
TextPointer start = richTextBox1.CaretPosition.GetLineStartPosition(LineNumber);
TextPointer stop = richTextBox1.CaretPosition.GetLineStartPosition(LineNumber+ 1);
TextRange textrange = new TextRange(start, stop);
textrange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
Select all line:
TextPointer start = richTextBox1.CaretPosition.DocumentStart;
TextPointer stop = richTextBox1.CaretPosition.DocumentEnd;
TextRange textrange = new TextRange(start, stop);
textrange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Black));
if you get an error of type "The calling thread cannot access this object because a different thread owns it" or if the program "freezes" in an inexplicable way. You should you the Dispatcher to avoid thread conflict:
private delegate void SetColorDelegate();
private void ExecuteInstruction()
{
try
{
Dispatcher.BeginInvoke(new SetColorDelegate(setcolor));
}
catch (Exception e)
{
//The calling thread cannot access this object because a different thread owns it
}
}
private void setcolor()
{
TextPointer start = richTextBox1.CaretPosition.GetLineStartPosition(ProtocolPointer);
TextPointer stop = richTextBox1.CaretPosition.GetLineStartPosition(ProtocolPointer + 1);
TextRange textrange = new TextRange(start, stop);
textrange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
}
Thursday, October 4, 2012
Matlab fread optimization
The problem statement is to read in-phase and quadrature binary data from a file in which they are alternated:
I1
Q1
I2
Q2
...
In
Qn
each value is a 32-bit integer.
the trivial way to do it is as follow using fread function:
fid=fopen(filepath);
for k=1:n
I(k)=fread(fid,1,'int','l');
Q(k)=fread(fid,1,'int','l');
end
An other possibility with minimizing the access to fread function:
temp=fread(fid,IQA_sz/4,'int','l');
I(:)=temp(1:2:end);
Q(:)=temp(2:2:end);
The result is similar much the execution time is speedup by a factor 5 to 6
Tuesday, March 13, 2012
Alternative PDF viewer
A friend advised me on a pdf viewer with extended commenting capabilities, a bit like the original Adobe solution, but for free:
http://www.tracker-software.com/product/pdf-xchange-viewer
This company also offers non-free pro features, last time I checked single license was 74 $ which is an excellent price compared to Adobe Solutions (more than 800$), honestly it may not be a fair comparison since I did not check if both solution are comparable in term of features and capabilities.
http://www.tracker-software.com/product/pdf-xchange-viewer
This company also offers non-free pro features, last time I checked single license was 74 $ which is an excellent price compared to Adobe Solutions (more than 800$), honestly it may not be a fair comparison since I did not check if both solution are comparable in term of features and capabilities.
Friday, January 27, 2012
Thursday, January 26, 2012
DataBinding to a WPF grid's cell
The idea was to use the data binding capabilities of WPF to ensure that a given element placed inside a grid's cell stays square (i.e. width = height). The problem is that it is not possible to directly access the height of a given row... Thus the trick is to place a border (zero thickness = not visible) inside that row and bind the element that should stay square to it.
For example like that:
For example like that:
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="100*"/>
<RowDefinition Height="10*" />
<RowDefinition Height="30*" />
Grid.RowDefinitions>
<Grid Grid.Row="0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100*" />
<ColumnDefinition Width="10*" />
Grid.ColumnDefinitions>
<Border Grid.Column="0" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="MyBorder"
/>
<WindowsFormsHost Grid.Column="0" Grid.Row="0" Width="{Binding ElementName=MyBorder, Path=ActualHeight}" Name="host" VerticalAlignment="Stretch" Loaded="host_Loaded"/>
<my:ColorScaleViewer Grid.Column="1" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="colorScaleViewer1"/>
Grid>
Grid>
Wednesday, January 25, 2012
OnIdle Event in WPF
The OnIdle Event is fired as soon as the computer does not have any tasks to execute. Thus it is a very convenient way to decide when refreshing graphical components. Assuming you have a program that streams a bunch of data from a given source, each time a couple of data arrive you trigger the refresh display function along with your processing functions. What will appends is that the graphical elements refreshing is pretty slow, thus the data start to accumulate in the buffer and the time lag between the data live stream and the graphical result on the screen increases.
The solution to this issue is to process the data when they arrive, but not do any refreshing on the screen nor the graphical components. The screen will be refreshed only when the computer has time, which will be the case when the event OnIdle fires.
Use the following code to subscribe to the OnIdle Event:
The method where to put the refreshing functions:
Notice that this way of processing the data and refreshing the screen, prioritize the processing (possible also the recording) of the data compared to displaying the data to the user. The human eye can only see 25 images per second anyway, so it does not make sense in most of the case to refresh each time a data arrives.
The solution to this issue is to process the data when they arrive, but not do any refreshing on the screen nor the graphical components. The screen will be refreshed only when the computer has time, which will be the case when the event OnIdle fires.
Use the following code to subscribe to the OnIdle Event:
using System.Windows.Interop;
private void
Window_Loaded(object sender, RoutedEventArgs e)
{
ComponentDispatcher.ThreadIdle += new EventHandler(OnIdle);
}
The method where to put the refreshing functions:
private void OnIdle(object sender, EventArgs
e)
{
// Refreshing display
functions
}
Cross-thread operation not valid in WPF
Elegant solution to commonly encountered issue:
this.Dispatcher.Invoke((Action)(()
=>
{
//add your cross-threads operations here for example
//richTextBoxOut.AppendText(text);
}));
Subscribe to:
Posts (Atom)