Solved

Have c# program open in middle of excel window

Posted on 2011-03-04
5
379 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

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