Solved

Have c# program open in middle of excel window

Posted on 2011-03-04
5
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

623 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