Solved

Have c# program open in middle of excel window

Posted on 2011-03-04
5
366 Views
Last Modified: 2012-08-13
I have a c# program that consists of a form and a few controls on it.  I have a VBA macro that will open this program using this code

dblShellRetn = Shell("C:\theProgram.exe", vbNormalFocus)

This opens the program however I want it to be centered within the excel application window.  

Is there some way with Excel vba to not only open an external application but center it within the excel window?  Otherwise how would I program it in c# so that whenever it is opened it finds out the center of the excel application and positions itself to be in the middle of the excel window?  
0
Comment
Question by:ecoder27
  • 3
5 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35041451
One way that I can think of doing this is using APIs.

If you have the name of the C# Exe then You can use the API FindWindow to get the handle of that C# application and then use the API SetWindowPos to change the position of your C# window.

Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35041454
>>> C# Exe

I meant C# Window Caption

Sid
0
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 250 total points
ID: 35041621
I searched my database if I had some similar kind of code but I couldn't find it. I then searched Google but couldn't find any so I created this from scratch for you :)

I have also put comments in the code so that you may not have any problems understanding it.

Sid

Tested and Tried Successfully with Notepad and Excel

Please paste this code in a module.

'~~> Code Created By Siddharth Rout on 5th March 2011
'~~> You may freely use it in your Application however
'~~> Please do not delete these 3 lines from the code

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Declare Function SetWindowPos Lib "user32" _
(ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, _
ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Public Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, _
lpRect As RECT) As Long

Sub Center3rdPartyApp()
    Dim Ret As Long
    
    '~~> Caption of C# Application
    Dim strCaption As String
    
    '~~> For Storing Width and Height of C# Application
    Dim CAppWidth As Long, CAppHeight As Long
    
    '~~> For Storing Width and Height of Excel Workbook
    Dim ExcelAppWidth As Long, ExcelAppHeight As Long
    
    '~~> This is required to get the Height and Width of
    '~~> C# Application and Excel Workbook
    Dim rc As RECT
    
    '~~> For new position of the C# App
    Dim NewLeft As Long, NewTop As Long
    
    '~~> Testing it with Notepad. Replace this with the C# App caption
    strCaption = "Untitled - Notepad"
    
    '~~> Get the handle of the Excel - Book1. Please change the caption
    '~~> accordingly. This is one classic case where WYSIWYG doesn't apply
    '~~> Window will show "Book1 - Microsoft Excel" but it is the opposite
    Ret = FindWindow(vbNullString, "Microsoft Excel - Book1")
    
    '~~> Get the width and height of "Microsoft Excel - Book1"
    GetClientRect Ret, rc
    ExcelAppWidth = rc.Right - rc.Left
    ExcelAppHeight = rc.Bottom - rc.Top
    
    '~~> Get the width and height of C# App
    Ret = FindWindow(vbNullString, strCaption)
    GetClientRect Ret, rc
    CAppWidth = rc.Right - rc.Left
    CAppHeight = rc.Bottom - rc.Top
    
    '~~> Calculate the new position of the C# App
    NewLeft = (ExcelAppWidth / 2) - (CAppWidth / 2) + ActiveWindow.Left
    NewTop = (ExcelAppHeight / 2) - (CAppHeight / 2) + ActiveWindow.Top
    
    '~~> Set the position
    SetWindowPos Ret, 0, NewLeft, NewTop, CAppWidth, CAppHeight, 0
End Sub

Open in new window

0
 
LVL 24

Expert Comment

by:broomee9
ID: 35357000
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now