Screen Resolution problem

Posted on 1998-01-18
Last Modified: 2006-11-17
I want to test my program in other resolutions, but the
BackGround doesn't fit...
I used a PictureBox control to contain the BackGround
(and i can't change it into an image control because of
some functions of the program).
I'd like to know if there is something like the stretch
property of the image controls for the picturebox control.
Or, maybe, how to fit it accordingly to the resolution of
the user...Thank you!
Question by:Brando
  • 4
  • 3
  • 2
  • +1

Expert Comment

ID: 1452010
You can get the resoultion of the screen by the Screen.Width and Screen.Height properties.  Then adjust your picturebox Width and Height accordingly.  Once you have your picturebox to the right size, use LoadBitmap to get a handle to the bitmap and copy the picture to the picturebox with StrechBlt.

Declare Function StretchBlt Lib "gdi32" Alias "StretchBlt" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long

Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long

In LoadImage, hInst should be null, lpsz should be the path and filename of the Bitmap, un1 should be IMAGE_BITMAP, n1 and n2 are the width and height of the bitmap, and un2 should be LR_LOADFROMFILE.

In StrechBlt, hDC will be the handle to the device you are copying to, in this case, Picture1.hDC.  x and y are the upper left coordinates of the destination.  nWidth and nHeight are the width and height of the destination.  hSrcDC is the Device context of the bitmap, returned by the LoadImage call.  xSrc and ySrc are the upper left coordinates of the bitmap.  nSrcWidth and nSrcHeight are the width and height of the bitmap.  dwRop should be SRCCOPY.

Expert Comment

ID: 1452011
Sorry dirtdart, but this won't work as you propose because

"The LoadImage() API can be used to load a bitmap from a BMP file. However, it does not return palette information."

So, you end up with a picture with background color.

Expert Comment

ID: 1452012
In that case, the easiest way would probably be to load the bitmap into a "hidden" picturebox, then transfer it from one to the other using StrechBlt.
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.


Expert Comment

ID: 1452013
Yes, that is the easiest way.

Expert Comment

ID: 1452014
Any particular reason why my answer was rejected?  Just curious, and it might help others to find out what you want.

Expert Comment

ID: 1452015
dirtdart, provide a sample Form_Resize event procedure that demonstrates your previously proposed answer - and propose it as the answer to this question.

Expert Comment

ID: 1452016
... as for the LoadImage API comments, you could use the native VB method:

Set Picture1.Picture = LoadPicture("FilePathName")


Expert Comment

ID: 1452017
... but then again, you could also use the native VB "PaintPicture" method to copy/stretch the bitmap.


Accepted Solution

mrmick earned 200 total points
ID: 1452018
On second thought, here's the solution without any API calls using only native VB calls.  Create a new project, place two picturebox controls on Form1 and set the Form1.BorderStyle = 0.  Paste the code below into the Form1.Module and run.

Private Sub Form_Resize()

   Const strBitmap = "C:\Windows\Clouds.bmp"
   Static Here As Boolean
   If Here Then Exit Sub

   Here = True
   Me.Move 0, 0, Screen.Width, Screen.Height
   Picture1.AutoRedraw = True
   Picture1.BorderStyle = 0
   Picture2.AutoSize = True
   Set Picture2.Picture = LoadPicture(strBitmap)
   Picture2.AutoSize = False
   Picture1.Move 0, 0, Screen.Width, Screen.Height
   If (Picture2.Width < Picture1.Width) Or (Picture2.Height < Picture1.Height) Then
      Picture1.PaintPicture Picture2.Picture, 0, 0, Picture1.Width, Picture1.Height, 0, 0, Picture2.Width, Picture2.Height, vbSrcCopy
      Picture2.Move 0, 0, Screen.Width, Screen.Height
      Picture1.PaintPicture Picture2.Picture, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, 0, 0, Picture2.ScaleWidth, Picture2.ScaleHeight, vbSrcCopy
   End If
   Picture2.Visible = False
   Here = False

End Sub


Author Comment

ID: 1452019
I got it working using mrmicks suggestion using native VB methods .I would like mrmick to have the points. Thank you, MrMick.
( i don't know what an api should be...)

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question