1、需要用到的 APIs

  1. GetForegroundWindow()
  2. GetWindowThreadProcessId()

2、大致步骤

1、首先导入 DLL 和添加引用

//引用
using System.Diagnostics;
using System.Runtime.InteropServices;

//导入DLL(class内部)
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);

2、接着获取焦点窗口的 HWND

IntPtr myPtr = GetForegroundWindow();

3、通过 HWND 获取进程 ID,再通过 Process 的 GetProcessById 即可获取到进程

int calcID;
GetWindowThreadProcessId(myPtr, out calcID);
Process result = Process.GetProcessById(calcID);

值得注意的是,在 C# 7后 out 关键字支持在传入时定义,即为

GetWindowThreadProcessId(myPtr, out int calcID);
Process result = Process.GetProcessById(calcID);

110 个评论

  1. I just wanted to thank you once more for your amazing site you have made here.
    It’s full of ideas for those who are definitely interested in this kind of subject,
    specifically this very post. You really are all
    so sweet plus thoughtful of others as well as reading your blog posts is
    a good delight with me. And what a generous surprise!
    Tom and I will certainly have fun making use of your tips in what we have
    to do in the future. Our list is a kilometer long which means your tips might
    be put to very good use.

    My homepage; upper back pain

  2. Greetings from Los angeles! I’m bored at work so I decided to browse your website
    on my iphone during lunch break. I love the knowledge you present here and can’t wait to
    take a look when I get home. I’m surprised at how quick your blog loaded on my cell phone ..
    I’m not even using WIFI, just 3G .. Anyways, fantastic site!

    Look at my site – Lashawn

  3. I’m amazed, I have to admit. Rarely do I encounter
    a blog that’s both equally educative and entertaining, and without a doubt, you have
    hit the nail on the head. The issue is something which not enough folks are speaking intelligently about.
    I’m very happy I came across this in my search for something regarding this.

    Here is my homepage; yeast infection

  4. Hiya, I am really glad I’ve found this information. Nowadays
    bloggers publish just about gossips and internet and this
    is really annoying. A good blog with exciting content, this is what I
    need. Thanks for keeping this web site, I will be visiting it.
    Do you do newsletters? Can not find it.

    my blog :: Madeline

  5. This is the right site for anybody who would like to find out about this topic.

    You understand so much its almost tough to argue with you (not that I personally would want to?HaHa).
    You certainly put a brand new spin on a topic which has been discussed
    for a long time. Excellent stuff, just excellent!

    Feel free to surf to my page – drug addiction

  6. Hello there! I could have sworn I’ve been to this website
    before but after going through many of the posts I realized it’s new to me.
    Anyways, I’m definitely pleased I found it and I’ll be bookmarking it and checking back regularly!

  7. I?m amazed, I must say. Rarely do I encounter a blog that?s equally educative and interesting,
    and without a doubt, you’ve hit the nail on the head.
    The problem is something which too few men and
    women are speaking intelligently about. I am very happy I
    stumbled across this during my search for something regarding this.

    My page … crash diet

  8. With havin so much content do you ever run into any issues of plagorism or
    copyright infringement? My site has a lot of completely unique content I’ve
    either created myself or outsourced but it seems a lot of it is popping it up all over the internet without my agreement.

    Do you know any techniques to help stop content from being stolen?
    I’d genuinely appreciate it.

  9. I enjoy you because of your entire hard work on this
    blog. My mom take interest in working on investigations and it is
    obvious why. A number of us know all of the lively form you render sensible tactics through your
    blog and as well as invigorate participation from some others about this situation and
    my daughter is undoubtedly discovering a whole lot.

    Take pleasure in the rest of the year. You are always doing
    a useful job.

    My site; ketogenic weight loss

  10. Hello there, I believe your blog may be having internet
    browser compatibility issues. Whenever I take a look at your website in Safari, it looks fine but when opening in Internet Explorer,
    it’s got some overlapping issues. I simply wanted to provide you
    with a quick heads up! Other than that, fantastic website!

    Here is my blog post :: seed contains

  11. Very good site you have here but I was wanting to know if
    you knew of any user discussion forums that cover the same topics talked about here?
    I’d really like to be a part of online community where I
    can get advice from other experienced people that share the same interest.
    If you have any suggestions, please let me know.
    Thanks!

    my webpage http://www.aniene.net

  12. Simply want to say your article is as surprising. The clarity to your
    put up is simply spectacular and i could think you’re knowledgeable on this subject.

    Well with your permission let me to grasp your
    RSS feed to keep updated with drawing close post.
    Thanks 1,000,000 and please continue the gratifying work.

  13. Greetings! This is my first comment here so I just wanted to give
    a quick shout out and tell you I genuinely enjoy reading through your articles.
    Can you recommend any other blogs/websites/forums that deal with the same topics?
    Thanks a ton!

  14. My programmer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the expenses.
    But he’s tryiong none the less. I’ve been using WordPress on a variety of websites
    for about a year and am anxious about switching to another platform.

    I have heard great things about blogengine.net. Is there a way I
    can import all my wordpress content into it? Any help would be greatly appreciated!

  15. Excellent blog! Do you have any helpful hints for aspiring writers?

    I’m hoping to start my own blog soon but I’m a little lost on everything.
    Would you advise starting with a free platform like WordPress or go
    for a paid option? There are so many options out
    there that I’m completely confused .. Any tips? Appreciate it!

  16. Please let me know if you’re looking for a writer for your weblog.
    You have some really great posts and I feel I would be a good asset.
    If you ever want to take some of the load off, I’d
    absolutely love to write some content for your blog in exchange for a link back to mine.
    Please send me an email if interested. Thank you!

    Here is my web page – male skin care

  17. Just want to say your article is as amazing. The clearness on your submit is just
    great and i could suppose you’re a professional in this subject.
    Fine along with your permission allow me to grab your RSS feed to stay up to date with forthcoming post.
    Thank you 1,000,000 and please continue the gratifying
    work.

  18. Hey there! This is kind of off topic but I need some help
    from an established blog. Is it tough to set up your own blog?
    I’m not very techincal but I can figure things out pretty fast.
    I’m thinking about setting up my own but I’m not sure where to start.
    Do you have any points or suggestions? Thank you

    Take a look at my page healthy weight

  19. I was wondering if you ever considered changing the layout of your site?
    Its very well written; I love what youve got to say. But maybe you
    could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having one or two pictures.
    Maybe you could space it out better?

    my web blog – forum.vkmoravia.cz

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注