Change visual c++ application font - visual-c++

How to change font in all dialog forms in a visual c++ application?
I want to set Tahoma style.
Thanks.

You can set the font for a dialog in the resource it's created from. I believe that'll change the font on all the standard controls as well. If you have custom controls, you'll have to do additional work.
Note that if you want to have the font match the default UI font for the computer, then you can use a virtual font like "MS Shell Dlg 2" which will be mapped to Tahoma on XP, and Segoe UI on Vista+.

Replacing font in each dialog of your application would be rather tedious job.
You can employ MFC to do it for you.
Check InitInstance of your app. Look at AfxEnableControlContainer();
It is being called woithout any parameter even though AfxEnableControlContainer is declared as
void AFX_CDECL AfxEnableControlContainer(COccManager* pOccManager=NULL);
COccManager is a very interesting class and is used when has occ ( OLE custom controls) support, managing OLE container and site classes. All MFC applications are created by default with occ support. If you do not see AfxEnableControlContainer in the code generated by wizard, you do not have occ support enabled.
Anyway, instead using default occ implementation, use own and change it to change the font.
Derive class from COccManager. In this sample I call it CDlgOccManager. Override virtual PreCreateDialog:
virtual const DLGTEMPLATE* PreCreateDialog(_AFX_OCC_DIALOG_INFO* pOccDialogInfo,
const DLGTEMPLATE* pOrigTemplate);
In the implementation:
const DLGTEMPLATE* CDlgOccManager::PreCreateDialog(_AFX_OCC_DIALOG_INFO* pOccDialogInfo, const DLGTEMPLATE* pOrigTemplate)
{
CDialogTemplate RevisedTemplate(pOrigTemplate);
// here replace font for the template
RevisedTemplate.SetFont(_T("Tahoma"), -16);
return COccManager::PreCreateDialog (pOccDialogInfo, (DLGTEMPLATE*)RevisedTemplate.Detach());
}
Now you are changin font for all dialogs. Remember changing AfxEnableControlContainer call:
PROCESS_LOCAL(CDlgOccManager, pManager);
BOOL CDlgFontChangeApp::InitInstance()
{
AfxEnableControlContainer(pManager.GetData());
.
.
.
}
DO not forget to
#include "DlgOccManager.h"
For new verion of the MFC include afxdisp.h for older, occimpl.h for COccManager.

I just noticed something. It is not a blunder but it needs an explanation.
I have kept this code in my repository for a very, very, very long time.
It was a time when DLLs kept all data as global, making data available to all modules that loaded this dll. In order to force data to be stored in TLS area, I used PROCESS_LOCAL macro that expands to invoking CProcessLocal class that is still alive.
You can remove this macro and replace it with:
BOOL CDlgFontChangeApp::InitInstance()
{
CDlgOccManager* pManager = new CDlgOccManager();
AfxEnableControlContainer(pManager);
.
.
.
}

Related

How to differentiate color scheme

I would like to change color scheme Eiffel so that the keyword class in java files will be different color than keyword int? If I change Storage in Eiffel, both keywords change the color.
You need to find out the exact scopes of class and int. The PackageResourceViewer is a great way to open files inside .sublime-package files.
Once installed, launch *”PackageResourceViewer: Open Resource, open the Java package and look for the syntax files (e.g.Java.sublime-syntax`).
I'm not familiar enough with Java, but it looks to me like class uses the scope meta.class and int uses storage.type.primitive.array. That means, that in your theme you would at least have to change meta or storage. Needless to say that you can be more specific than that.
Using a nice little plugin called ScopeAlways, you can see in the status bar the full scope underlying your cursor (if you have multiple cursors, it just uses the first one). In Sublime Text 2, using the following code:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
int integer = 13;
}
}
The word class is scoped as storage.modifier.java, whereas int is scoped as storage.type.primitive.array.java (I removed a few unimportant extraneous scopes from each for clarity). So, in your theme, you could have one color for storage.modifier, and another for storage.type, to differentiate between class and int. Be aware, however, that many other keywords will fall under both scopes, so your custom colors won't just highlight those particular words.
Good luck!

When are the bounds and font of a UITextView initialized in Xamarin Forms?

I'm trying to implement an Editor with hint text functionality for a Xamarin.Forms project. This is trivial in Android, because the underlying EntryEditText control has a Hint property. In iOS, the implementation is a bit more complex because the UITextView class does not implement hint text.
I don't like the technique, "set text to the placeholder, clear it if typing starts, return it if typing ends and the text is blank". It means I have to do extra work to tell if the control's blank, and there's a lot of fiddling with the text color involved. But I've been having so much trouble I'm going to have to resort to it. Maybe someone can help me with this.
I started with the answer to Placeholder in UITextView. I started a new Xamarin iOS project and stumbled through a rough Obj-C to C# conversion, and it worked great with a minor change: the Font property of the UITextView isn't initialized yet in the constructor, so I had to override AwakeFromNib() to set the placeholder label's font. I tested it and it worked, so I brought that file into a Xamarin Forms project, and things started getting a little nutty.
The first problem is it turns out apparently MonoTouch has some slight API differences in Xamarin Forms, such as using some types like RectangleF instead of CGRect. This was obvious, if not unexpected. I've been wrestling with some other differences for the past few days, and can't seem to overcome them in a way that makes me happy. Here's my file, trimmed down significantly because I've been trying all kinds of debugging things:
using System;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using MonoTouch.CoreGraphics;
using System.Drawing;
namespace TestCustomRenderer.iOS {
public class PlaceholderTextView : UITextView {
private UILabel _placeholderLabel;
private NSObject _notificationToken;
private const double UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION = 0.25;
private string _placeholder;
public string Placeholder {
get {
return _placeholder;
}
set {
_placeholder = value;
if (_placeholderLabel != null) {
_placeholderLabel.Text = _placeholder;
}
}
}
public PlaceholderTextView() : base(RectangleF.Empty) {
Initialize();
}
private void Initialize() {
_notificationToken = NSNotificationCenter.DefaultCenter.AddObserver(TextDidChangeNotification, HandleTextChanged);
_placeholderLabel = new UILabel(new RectangleF(8, 8, this.Bounds.Size.Width - 16, 25)) {
LineBreakMode = UILineBreakMode.WordWrap,
Lines = 1,
BackgroundColor = UIColor.Green,
TextColor = UIColor.Gray,
Alpha = 1.0f,
Text = Placeholder
};
AddSubview(_placeholderLabel);
_placeholderLabel.SizeToFit();
SendSubviewToBack(_placeholderLabel);
}
public override void DrawRect(RectangleF area, UIViewPrintFormatter formatter) {
base.DrawRect(area, formatter);
if (Text.Length == 0 && Placeholder.Length > 0) {
_placeholderLabel.Alpha = 1;
}
}
private void HandleTextChanged(NSNotification notification) {
if (Placeholder.Length == 0) {
return;
}
UIView.Animate(UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION, () => {
if (Text.Length == 0) {
_placeholderLabel.Alpha = 1;
} else {
_placeholderLabel.Alpha = 0;
}
});
}
public override void AwakeFromNib() {
base.AwakeFromNib();
_placeholderLabel.Font = this.Font;
}
protected override void Dispose(bool disposing) {
base.Dispose(disposing);
if (disposing) {
NSNotificationCenter.DefaultCenter.RemoveObserver(_notificationToken);
_placeholderLabel.Dispose();
}
}
}
}
A notable change here is relocation of the label's initialization from DrawRect() to the constructor. As far as I can tell, Xamarin never lets DrawRect() be called. You'll also note I'm not setting the Font property. It turned out in the iOS MonoTouch project, sometimes the parent's font was null and it's illegal to set the label's font to null as well. It seems at some point after construction Xamarin sets the font, so it's safe to set that property in AwakeFromNib().
I wrote a quick Editor-derived class and a custom renderer so Xamarin Forms could render the control, the Renderer is slightly of note because I derived from NativeRenderer instead of EditorRenderer. I needed to call SetNativeControl() from an overridden OnModelSet(), but peeking at the assembly viewer showed that EditorRenderer makes some private calls I'll have to re-implement in mine. Boo. Not posted because this is already huge, but I can edit it in if needed.
The code above is notable because the placeholder isn't visible at all. It looks like in iOS-oriented MonoTouch, you typically initialize a control with a frame, and resizing is a rare enough circumstance you can assume it doesn't happen. In Xamarin Forms, layout is performed by layout containers, so a constructor-provided frame is irrelevant. However, the size of the label is intended to be set in the constructor, so it ends up having negative width. Whoops.
I assumed this could be solved by moving instantiation of the label into AwakeFromNib(), or at least sizing it there. This is when I discovered that for some reason, AwakeFromNib() isn't called in the control. Welp. I tried to find an equivalent callback/event that happened late enough for the bounds to be set, but couldn't find anything on the iOS side. After trying many, many things, I noticed the custom renderer received property change events for the Xamarin Forms Model side of this mess. So, if I listen for Height/Width change events, I can then call a method on the label to give it a reasonable size based on the current control. That exposed another problem.
I cannot find a way to set the label's font to match the UITextView's font. In the constructor, the Font property is null. This is true in both the iOS and Xamarin Forms project. In the iOS project, by the time AwakeFromNib() is called, the property is initialized and all is well. In the XF project, it's never called, and even when I pull stunts like invoking a method from a 5-second delayed Task (to ensure the control is displayed), the property remains null.
Logic and iOS documentation dictates the default value for the font should be 17-point Helvetica. This is true for the placeholder label if I fudge the size so it's visible. It is not true for the UITextView control, though since it reports its font as null I'm unable to see what the font actually is. If I manually set it all is well, of course, but I'd like to be able to handle the default case. This seems like a bug; the box seems to be lying about its font. I have a feeling it's related to whatever reason the Xamarin.Forms.Editor class doesn't have a Font property.
So I'm looking for the answer to two questions:
If I'm extending an iOS control in XF to add a subview, what is the best way to handle sizing that subview? I've found Height/Width changes raise events in the renderer, is this the only available way?
When the property has not been set by a user, is the Font of a UITextView in Xamarin Forms ever set to a non-null value? I can live with a requirement that this control requires the font to be explicitly set, but it's yucky and I'd like to avoid it.
I'm hoping I've missed something obvious because I started barking up the wrong trees.
If I'm extending an iOS control in XF to add a subview, what is the
best way to handle sizing that subview? I've found Height/Width
changes raise events in the renderer, is this the only available way?
This is the only way I know of since the exposed elements of the renderer are so limited.
When the property has not been set by a user, is the Font of a
UITextView in Xamarin Forms ever set to a non-null value? I can live
with a requirement that this control requires the font to be
explicitly set, but it's yucky and I'd like to avoid it.
No, the Font is not assigned a default non-null value.

Set Font globally in JavaFX

How can I set the Font type globally in a JavaFX application?
Is there any solution that I can use? In JavaFX 8 the default Font has changed, and I would like to use the same Font used in JavaFX 2.2.
You can skin your application with CSS as described on the Oracle Website.
Using following syntax you may set the general theme for your application:
.root{
-fx-font-size: 16pt;
-fx-font-family: "Courier New";
-fx-base: rgb(132, 145, 47);
-fx-background: rgb(225, 228, 203);
}
You include the css as followed:
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
Changing the Default Font for a Scene
This is the solution outlinked in nyyrikki's answer.
You can change the default font used for most things in any given scene by applying the following CSS stylesheet to the scene:
.root {
-fx-font: 28px Vivaldi;
}
Substitute whatever settings you require for the -fx-font value according the font definition in the JavaFX CSS reference guide.
Changing the Default Font for an Application
If you want to change the default font used for most things in a JavaFX application, you can override the default style sheet using Application.setUserAgentStylesheet. Using this method you can set the default style for a JavaFX 8 application to the caspian stylesheet which was default for JavaFX 2.2 rather than the modena stylesheet which is default for JavaFX 8. If you want some hybrid of the two default styles or some custom default stylesheet such as AquaFX, then you will need to perform the customization yourself.
Switching Font Rendering Technology
Additionally, on some platforms, JavaFX 2.2 uses a different font rendering mechanism than JavaFX 8, which can account for subtle differences in font rendering between the two. There is an undocumented and unsupported command line switch which can be used to switch between font rendering mechanisms in JavaFX 8, but I don't know what the switch is off-hand and, even if I did, I wouldn't recommend deploying an application using the switch as it is unsupported.
FWIW, if you want to make the font change programmatically (no external css files involved), you can make a "global" change by using the .setStyle() method on the top parent node.
Example, I wrote a quick GUI (a bare bones testing framework) that both a Windows and a Mac user needed to run (and I'm writing on Linux/Ubuntu). The Mac user complained that the fonts were too small. So I added the following:
String os = System.getProperty("os.name","generic").toLowerCase(Locale.US);
if (os.indexOf("mac") > 0) {
root.setStyle("-fx-font-size: 14pt");
}
In this case, root is the Parent node in the Scene instantiation. All nodes connected to root will share this font size setting, unless the setting is overwritten.
Just a TornadoFX example
package bj
import tornadofx.*
class MyView : View() {
override val root = vbox {
button("天地玄黄")
button("宇宙洪荒")
}
}
class MyStylesheet : Stylesheet() {
init {
root {
fontFamily = "Noto Sans CJK SC Regular"
}
}
}
class MyApp : App(MyView::class, MyStylesheet::class)
fun main(args: Array<String>) {
launch<MyApp>(*args)
}

Setting Caliburn IWindowmanager's Owner Property to Excel with handle HWND

I have an Excel Vsto addin application in which I host WPF application build using Calibrun Micro Autofac.I have a dialog popping up the excel and I want that Pop up window's Owner to be set to this excel window.Only way I see doing this is using WindowInteropHelper Class which needs Window instance.
And I am using settings like this :
dynamic settings = new ExpandoObject();
And I show window like this :
windowManager.ShowDialog(viewModel, settings: settings);
So What should I do to set the settings.Owner Property to this excel window(Whose Handle is known) so that the Pop up window is always on top of excel window??
It looks like you are hosting a WPF application (add-in) inside Excel which is an Office application and Caliburn.Micro has a constructor in BootstrapperBase class exactly for this situation, it looks like this: BootstrapperBase(useApplication = true), so you should derive your bootstrapper from BootstrapperBase and pass in false to the base constructor. something like this:
class MyBootstrapper : BootstrapperBase {
MyBootstrapper()
: base(false)
{
}
}
Then Caliburn.Micro will set the owner property correctly for you, you don't have to worry about it. Now if you knew about this but it didn't work for then comment on this and i will give you a solution specific to your situation.
Edit: To set the owner of the created window we need to set the Owner property (which is of type Window) but the problem is that you are working with a native win32 window so you only have a handle and WPF windows don't accepts handles as Owners, and the second problem is that we don't have a reference to the created window so we can wrap it inside a WindowInteropHelper, in order to solve this i suggest the following:
Add information to the created window so we can identify it later, we can do it like this:
var settings = new ExpandoObject();
settings.Tag = "THE_ONE"
windowManager.ShowDialog(viewModel, settings: settings);
After doing so we need to get a reference to that window, so we can
do something like this: var ourWindow = Application.Current.Windows.FirstOrDefault(w => w.Tag == "THE_ONE");
Now we wrap that with a WindowInteropHelper like this: var
interopHelper = new WindowInteropHelper(ourWindow);
Now we can set owner to the native window handle like this:
interopHelper.Owner = (IntPtr) //
PUT_YOUR_NATIVE_WINDOW_HANDLE_HERE;
That's all i can help you with, i hope it works.

How do you load data into an attributed database class generated by the MFC application wizard?

Using the MFCApplication wizard in Visual C++ 2012, if "Generate attributed database class" is checked, a header with some special syntax (attributed C++ classes) are generated, which look like this:
// MFCApplication2Set.h: interface of the CMFCApplication2Set class
//
#pragma once
// code generated on March-05-13, 9:26 AM
[
db_source(L"Provider=SQLNCLI11.1;..."),
db_table(L"dbo.tblEmployee")
]
class CMFCApplication2Set
{
public:
... big list of stuff that corresponds to the fields in your db table omitted ...
}
The above header corresponds to a mostly empty implementation file:
// MFCApplication2Set.cpp : implementation of the CMFCApplication2Set class
//
#include "stdafx.h"
#include "MFCApplication2.h"
#include "MFCApplication2Set.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMFCApplication2Set implementation
That was the WHOLE implementation class. Notice that:
A. No parent class name is specified anywhere.
B. There's some "Visual C++ magic" going on here, that is, as a novice to modern C++, I'm lost.
db_source is documented here but the documentation is pretty thin or at least opaque to me.
Questions:
I was mystified by this syntax when I first saw it, but I figured out it's probably a variant of this Attributes feature in Visual C++. It is that, right?
How am I meant to I use these generated "attributed database class" objects? I found this documentation but if you look carefully at that documentation, that code sample is showing people the Old Way and the New Way, and is not telling me what I want to know which is how to use this new object that the IDE can not give me any documentation or code completion features for. Also since the generated code for the CMFCApplication2Set class generated by the wizard does not reference any types or class names, I'm lost. If I could even use some IDE feature to know what methods and stuff have been Magically Injected into this Magical mystery Object, I'd be better off. The only think I can think to do is to learn the old way and learn all the things you can call from the old two-separate-ATL-types world, and then somehow learn to combine them.
In a nutshell, I'm looking for the minimum syntax I need to know to actually use one of these Attributed Database Class instances, variables, as they are generated in a new MFC app by the wizard. The instance shown below is a member of an MFC document class and CMFCApplication2Set m_MFCApplication2Set is declared as a field inside the MFC document class.
What I have tried is to use this "untyped object". This object appears to have lots of data fields (m_X) and has only one method that shows up in IDE code completion, called GetRowSetProperties. Thanks to whatever magic or injection is going on, this generated Attributed Database Class (which does not visibly inherit anything) is a complete mystery to me at edit time and compile time.
Here's me just trying to inspect this thing to see if it even initialized itself when its constructor ran:
BOOL CMFCApplication2Doc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE;
TRACE( m_MFCApplication2Set.m_AStringFieldName); // outputs NOISE.
return TRUE; }
At compile time and edit time, the IDE gives me NO help about the types involved in this "anonymous" class that inherits from nothing, but which gets lots of secret powers due to some kind of Injection via those attributes I'm guessing. At debug time, I can see that there is more than just a bunch of data fields in this C++ class, but this still doesn't help me know how to use it. A minimal code sample of using one of these to go get a recordset from the database, would be great.
Update: Calling OpenDataSource is fun, because it compiles but the IDE doesn't think it should be valid. Nevertheless, it runs, and returns 0 as the result, but that doesn't actually initialize this CThingyThatVisualStudioGaveYouThatYouDontKnowWhatItIs:
This is a deprecated feature of attributed C++ code. Pre-processor expands code and replaces attributes with actual base classes. If you enable generation of these intermediate files, things are going to be more clear to you:
You will have XXX.mrg.cpp and XXX.mrg.h files generated, which you can review and see the real C++ code forwarded to compiler.
The attributes will be replaced with substituted bases classes, maps like BEGIN_COLUMN_MAP etc. The attributed source code is compact, but the feature is deprecated and looking into expanded code it should be easy (if necessary) to strip the attributes and copy expanded code right into source. It's easy with DB attributes, and more difficult with COM attributes since the internal dependencies are most sophisticated.

Resources