Skip to main content

Posts

Showing posts with the label code

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....

Indexers in C#

Definition of Indexers from MSDN Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters. Whenever you are accessing individual element from List<T> or Hashtable<T,T> etc you are using Indexers . For example List<string> str=new List<string>(); str.Add("Rameez"); str.Add("Usmani"); string firstName=str[0];  //[0] is implying that you are calling indexer on instance of List class How Indexer is added to a class To add an indexer to a class you have to expose a property named "this(actually a keyword)" followed by "data type of index" in square brackets . Like this public ReturnDataType this[indexDataType] {     get { //logic to return the correct value }     set { //logic to set the correct value } } Lets say you have collection of Students and you want to access Student by name using Indexer , here is h...

Resizing image in C#

In this blog post I will explain in short how to load and re size an image in C# and save the resulting image in a file. Actually the process is very easy. Lets break it in chunks Loading image from a file .NET has multiple ways to load an image. 1) From a file 2) From a Stream object 3) From a GDI Bitmap object For this post we will only load image from a File.Loading image from file is straight forward. It requires calling static method FromFile of Image class , passing the file path as a parameter. Here is how Image myImage=Image.FromFile(sourcePath); Re-sizing image As we now have Image instance we have to re size it. For re-sizing we will use one overload of Bitmap class constructor which takes Image instance and new width and height for scaling. So here is the one liner to do this job Bitmap bmp=new Bitmap(myImage,newWidth,newHeight); Saving the resulting image We have a Bitmap containing scaled version of our original image.Now we will save it to a ...

Blackberry round corner TextField with place holder text

While working on a blackberry project I was required to display a round corner text field with a place holder text. The logic was to display place holder text when TextField is not in focus and no text is entered by user (or text is cleared by user). As soon as TextField receives focus, placeholder text should be removed. Displaying place holder text when TextField is empty is very easy . You can just override TextField's  paint method and check if text is empty you can set text to place holder text. Here is a simple example protected void paint(Graphics g){     if (getText().compareTo("")==0){         setText( "Place holder text.." );     }     super.paint(g); } But displaying and removing of placeholder text on focus and unfocus was a bit of task so I came up with my own class and want to share with other fellow coders. import net.rim.device.api.ui.Color; import net.rim.device.api.ui.Graphics; import net.rim.device...