Skip to main content

C Windows API - Enumerate Windows and some manipulation

Few years back I wrote a program in C language using Windows API that displays the list of all "Windows" opened on my PC (Windows) and I can make any of them visible/invisible.

I decided to share the part of code which Enumerate windows, find a window and make them visible/invisible.

Enumerating Windows

Windows API provide a function EnumWindows which takes a pointer to Callback function which it calls for each window found. Second parameter is a LPARAM which you can use to pass any argument you want, this LPARAM will be passed to our callback function.

Here is how to use EnumWindows
BOOL isSuccess=EnumWindows(OurAppCallback,(LPARAM)userDefinedVariable);
Syntax of callback (OurAppCallback) will be like this
BOOL CALLBACK OurAppCallback(HWND h,LPARAM l) 
Important Remarks about EnumWindows from MSDN: 
The EnumWindows function does not enumerate child windows, with the exception of a few top-level windows owned by the system that have the WS_CHILD style. 

 Finding a Window by its Title

My app lists all the windows in a listbox showing title of each window found. To hide/unhide a window I first find its Handle using FindWindow method provided by Windows API. FindWindow function takes Classname and Title of window and return the HWND(handle) to that window or NULL on error.
HWND wHandle=FindWindow(NULL,title);

Show/Hide a Window

To show/hide a window we will use ShowWindow function. It takes Handle to a window and style (SW_SHOW / SW_HIDE).

To show window
BOOL isSuccess=ShowWindow(wHandle,SW_SHOW);
To hide window
BOOL isSuccess=ShowWindow(wHandle,SW_HIDE);

To determine if Window is already visible or not

To check if Window is already visible or not we can use IsWindowVisible function. It takes window handle as input parameter and return TRUE (non zero) if window visible otherwise FALSE (zero). 

Comments

Popular posts from this blog

Decoding JPEG image file using libavcodec

I got a chance to work on a video encoding application that decodes series of jpeg files and convert them into ogg theora video file. I used the infamous libavcodec library that is used in FFMPEG . I decided to write blog posts explaining how I decode jpeg images and convert them into ogg video file. This is the first part and in this I will explain how to decode jpeg images using libavcodec. To learn how to write decoded images as a ogg video file please read http://random-stuff-mine.blogspot.com/2017/07/encoding-raw-images-to-ogg-theora-video.html Before reading this blog post you must be aware of using and setting up libavcodec. I highly recommend this tutorial to get basics of using libavcodec http://www.ffmpeg.org/doxygen/0.6/api-example_8c-source.html Allocating input format context We will first allocate input format for reading the file. We will use avformat_open_input function that will allocate AVFormatContext structure passed to it , the function detects input typ

CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information

I created a new Blazor Server app in Visual Studio 2019 and tried to run it. But I was getting this error CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information. I couldn't find any reason or solution to this problem. I tried creating the project multiple times but same error. I created a new .Net Core Web App and added a new razor component and included that component in a razor page (cshtml file) like this @(await Html.RenderComponentAsync<GeofenceWork>(RenderMode.ServerPrerendered)) and <component type="typeof(GeofenceWork)" render-mode="serverprerendered" /> As soon as I navigate to this page that has component added I got the same error: CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information. This was very frustrating. After hours of trying and searching I figured out the solution. 

Multithreaded C# TCP server to handle multiple clients

I decided to write a minimal multithreaded TCP based server as a blog post. Following class can serve as a skeleton for any small or large scale multithreaded TCP socket server. It do not contain much error handling , it is only to give an idea that how multithreaded server works and how it can process multiple clients using threading. using System; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; using System.Collections.Generic; namespace RandomStuffMine { public class MTServer {     public int Port{get;set;}     public Socket ServerSocket{get;set;}     private List<Client> Clients=new List<Client>();     private bool runServer=true;     public MTServer(int port)     {         Port=port;         ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);     }     public void Start()     {         Thread thr=new Thread(new ThreadStart(StartServer));         thr.IsBackground=t