I'm developing VB.net application in MS Visual Studio 2019 (community Edition) in which I programatically paste an image from the clipboard into a Richtextbox.
I need to add an option to force that image to completely fill the control's visible area.
Examining the richtext which results from pasting the image, and then again after I've manually dragged the image to resize it to fill the control, it looks like all I need to do is change the picwgoal and pichgoal rtf tags for the image to the dimensions of the control.
The problem is that picwgoal and pichgoal are in twips.
So my question is: Is there a reliable way to dynamically calculate a control's dimensions in twips from it's pixel width and height, such that it will work when the app runs in various resolutions/scaling ?
I understand that the TwipsPerPixelX method is obsolete and not compatable with 64 bit processes.
Thanks in advance.
..Think I've worked it out myself
..Think I've worked it out myself
assuming a point is always 1/72 inches and a point is 20 twips..
Dim g = Me.CreateGraphics()
Dim RTBxTwips = (RTB.Width * 72 / g.DpiX) * 20
Dim RTByTwips = (RTB.Height * 72 / g.DpiY) * 20
g.Dispose()
..seems to give results equal to the picwgoal and pichgoal tags for an image resized to the controls visible area
Related
I have a Windows form with a TabControl. In Tab 1 I have a RichTextBox (rtb1) and a Picturebox (pbx). In Tab 2 I have another RichTextBox (rtb2).
When I click a button, text from rtb1 and the current picture in pbx is put into rtb2. Problem is that the picturesize won't always fit. My current code is this:
Dim text = rtb1.Text
rtb2.AppendText(text)
Clipboard.SetImage(pbx.Image)
rtb2.Paste()
Is it possible to have the code resize the image as it's pasted?
It is possible to get the image from a windows path, I just didn't know how to resize this in a RichTextBox either. If this makes it easier?
Edit: This did it!
Dim newWidth = 500
Dim newHeight = 500
Clipboard.SetImage(New Bitmap(pbxDatasheet.Image, newWidth, newHeight))
rtbArmy.Paste()
Edit 2: It still doesn't work.
With the exact same code these things happen:
1. Most of the time the pasted picture(s) are the same size. But not the right size. I can change newWidth and newHeight all I want, they rarely differ in size. Although sometimes the picture is way larger than the others.
2. Sometimes it doesn't even add the picture to the RichTextBox. There's no pattern to when it happens.
The same lines of code can do all the above things. I have no idea what to do here.
We have developed windows application which is including many forms and controls with default small display size and it is working fine.
but in client systems,displays are not consistent. so when the same code runs with medium/large screen size, the controls are overlapping
It would be great help if anyone gives solution for dynamic re-size/fit the form & controls for any display (small-100%, medium-125%, large-150%)
and also Is there any way to find the Display size (small-100%, medium-125%, large-150%) in vb.net
You can change display size in control-Panel.
You can use the Anchor property to automatically adjust the control size according to its form size. You can anchor any side of the control to that side of the form. For example, if I anchor all four sides of a picturebox to the form, the picturebox will mimic the shape of the form when the form is resized. If you anchor only the top and the bottom of the picturebox, for example, it will change height with the form, but the width and horizontal position will remain constant.
Finally found a solution by doing R&D
call the below function in the Form_Load like ScaleForm(me)
Public Sub ScaleForm(WindowsForm As System.Windows.Forms.Form)
Using g As System.Drawing.Graphics = WindowsForm.CreateGraphics
Dim sngScaleFactor As Single
Dim sngFontFactor As Single = 1
If g.DpiX > 96 Then
sngScaleFactor = g.DpiX / 96
'sngFontFactor = 96 / g.DpiY
End If
If WindowsForm.AutoScaleDimensions = WindowsForm.CurrentAutoScaleDimensions Then
'ucWindowsFormHost.ScaleControl(WindowsForm, sngFontFactor)
WindowsForm.Scale(sngScaleFactor)
End If
End Using
End Sub
I'm currently combining multiple Rich text objects, to create one larger document (A word Doc).In doing this I've encountered a problem where if i set the font size to 11, it doesn't get properly translated.Rich Text contains a FS(number) entry which indicates the font size, it then doubles it.
(In this example im assigning the Font size after assigning text, this doesn't matter and occurs if its set before or after as you are setting what appears to be the global FS property)
Dim masterFont = New System.Drawing.Font("Times New Roman", 11, FontStyle.Regular)
Dim RTB As New RichTextBox
RTB.Font = masterFon
RTB.AppendText("this is the start of a test")
Yeilds:
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}}
\viewkind4\uc1\pard\f0\fs23 this is the start of a test\par
}
In this case, the number is 23 which is 11.5 Doubled, when this gets Pasted, sure enough the Font size is 11.5
If i do the same thing with 10 or 13 (Testing odds and evens) it works as expected!!
Dim masterFont = New System.Drawing.Font("Times New Roman", 13, FontStyle.Regular)
Dim RTB As New RichTextBox
RTB.Font = masterFont
RTB.AppendText("this is the start of a test")
Yeilds
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}}
\viewkind4\uc1\pard\f0\fs26 this is the start of a test\par
}
In this case, the font size is 26, 13 doubled.
Now, if i set the SelectionFont property to the MasterFont initially it will work however it pushes the Default Font size to the bottom (Adding extra linebreaks and potentially changing the font of any included documents)
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil Times New Roman;}{\f1\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\f0\fs22 this is the start of a test\b OF BOLD \b0 and this is After bold\f1\fs17\par
}
As the Font Size is doubled, there is some kind of conversion happening here and to that I've tried the Overload of all available Graphics Units and none of them matched!
Now, if i do a RichText.SelectAll (When the RTB object is ready to be pasted) and set the selection font, this works however it overrides formatting assigned to the combined paragraphs!
Im thinking there is a way to simply specify the exact FS Units however, through testing the Graphics Units, i did not see any FS11 So im really unsure if thats even possible.
Any help with this would be more than appreciated.
I'm struggling with the same problem. The RTF spec says that the font size (fs) is in half points, which explains the doubling.
Another observation is that when you set the Font of the windows forms RichTextBox to size 11, then the SelectionFont will become a different value, and this depends on the dpi of your display. You get different RTF created at 96dpi to that at 120dpi if you just set the Font property.
The font size in points is converted to pixels. There are 72 points per inch. The dpi setting of your monitor (or device context) says how many pixels per inch.
At 120dpi, a size 11 font = 11 * 120/72 = 18.333 pixels. This is rounded down to 18 pixels. This corresponds to a point size of 18 * 72 / 120 = 10.8 points. This is 21.6 half points, and rounds up to 22. You get fs22 in the rtf.
At 96dpi, a size 11 font = 11 * 96/72 = 14.667 pixels. Rounded up to 15 pixels and backcalculates to 11.25. This is 22.5 half pixels, rounded up to 23. You get fs23 in the rtf.
If you add a handler to the selectionchanged event of the RTB then you can see that the SelectionFont.Size takes this display-dependent font size.
I think that you have to be more specific, RichTextBox controls work with rich text which can have multiple fonts, colors, and objects embedded. To append text with a particular font append the text, then select it, then set the font of the selection. Don't rely on just setting the Font property.
On another note, if you show the Windows Forms FontDialog, then the font that is returned will also have this display-dependent font size.
And finally, if you want to just set up an empty RichTextBox with the font set to say, 11, then you might find that as soon as the user starts typing, the RTF has the 11.5 size. This is because you need to set the font of the insertion point, and you can't seem to do that from .Net. I've managed to do it using SendMessage to set the insertion point, and then setting the SelectionFont. The rtf will then start with the correct dpi-independent fs value:
Dim masterFont As New System.Drawing.Font("Calibri", 11, FontStyle.Regular, GraphicsUnit.Point, 0)
' select insertion point on an empty rtb.
Dim result As IntPtr = SendMessage(RichTextBox1.Handle, &HB1, IntPtr.Zero, New IntPtr(-1))
RichTextBox1.Font = masterFont
RichTextBox1.SelectionFont = masterFont
I have a word doc with a bunch of ActiveX Control buttons or whatever on it, and each time a button is clicked, a corresponding image needs to be displayed in a popup box.
I have a userform called ImageForm, and this is what I'm doing right now:
Sub Button_Clicked()
ImageForm.Picture = LoadPicture("appropriate_image_path")
ImageForm.Show
End Sub
Each of these images has a width of 8.5 inches, but their heights can vary anywhere from like 3 to 20 inches (they're snippets of a pdf). So I've set the width of the userform to a little more than 8.5 inches, and that looks fine. But I need to be able to scroll vertically through the image in the userform, since some of the images could be taller than a user's monitor.
I'm completely stuck on this. What I've tried so far is adding a frame to the form, then adding an image control inside the form, and setting the "ScrollBars" property of the frame to vertical. Then instead of using "ImageForm.Picture = ..." I use "ImageForm.ImageControl.Picture = ..." But it doesn't work.
Any insight here would be greatly appreciated. Hopefully this question is clear enough, I've only been using VBA for a month or so now. (I miss Java so, so much)
Thanks!
Here is a neat little trick based on one of my posts
The idea is to ensure that the image control is in frame control and the image control doesn't have a border. Also the image control's PictureSizeMode is set to fmPictureSizeModeClip so that we can scroll the image
SNAPSHOT (DESIGN TIME)
SNAPSHOT (RUN TIME)
CODE
Private Sub UserForm_Initialize()
With Frame1
'~~> This will create a vertical scrollbar
.ScrollBars = fmScrollBarsVertical
'~~> Change the values of 2 as Per your requirements
.ScrollHeight = .InsideHeight * 2
.ScrollWidth = .InsideWidth * 9
End With
With Image1
.Picture = LoadPicture("C:\Users\Public\Pictures\Sample Pictures\Desert.jpg")
.BorderStyle = fmBorderStyleNone
.PictureSizeMode = fmPictureSizeModeClip
End With
End Sub
I have a simple form in Access 2003. The form has a List control and a button. I wanted to make the list to resize with the form (only vertically) while the button remains at the bottom right of the List. I'm using the following code on the form's resize event.
list.Height = Me.InsideHeight - list.Top - 200
commandButton.Top = list.Height + list.Top + 50
This works fine as I resize the form, until the form height gets to a certain height. Then I get this error;
Run-time error '2100':
The control or subform control is too large for this location
This error is occurring at the line where I'm assigning the commandButton.Top. If I remove this line then the list height changes fine. I don't have any subforms in the form.
Does anyone know why this is happening?
Thanks
I think it is because you need to resize the detail section of the form first.
Try changing the code to
Me.Section(0).Height = Me.InsideHeight
list.Height = Me.InsideHeight - list.Top - 200
commandButton.Top = list.Height + list.Top + 50
Came by here (as many have) with the same problem and then realised my issue. Be mindful when resizing a control to a larger size with regard to the order of setting properties.
I would recommend setting the TOP and LEFT positions before HEIGHT and WIDTH.
Although my final sized control should have fitted OK once resized, I had originally tried setting the WIDTH first which attempted to enlarge the control exceeding the width of the form. My application threw the 2100 error at that point. I really hope this helps someone! Also, don't forget to set dimensions in TWIPS which is set as INCHESS x 1440 (or CM x 566.9291), ie: .Width = 10 * 566.9291 to set a control width to 10cm.
I get this same error if I set the width to greater the 31680.
With a little more research, I notice MS Access only allows a form width to be 22" wide. 22" = 31680 TWIPS.
So my workaround solutions it to add a check:
If newWidth > 31680 Then newWidth = 31680