Showing tag results for Code

Jul 8, 2013
Post comments count0
Post likes count0

A program for my nieces: The ABCs, part 1

Raymond Chen
Raymond Chen

I'm going to spend the next few weeks developing a Little Program in several parts. This is a program I wrote for my nieces, who always wanted to play with my laptop (instead of playing with me). Initially, I fired up Notepad and maximized it, and cranked the font size, but that became cumbersome, because I had to reset the font size and Word Wr...

Code
Jul 1, 2013
Post comments count0
Post likes count0

Building on our program that draws content at a fixed screen position regardless of window position

Raymond Chen
Raymond Chen

Today's Little Program uses the technique we saw last week of drawing content at a fixed screen position, regardless of window position, but adds a little physics to it. Start with our scratch program and make these changes: The class simulates damped motion. Ask a physicist how it works. The rest point is the center of the window. T...

Code
Jun 28, 2013
Post comments count0
Post likes count0

It’s the address space, stupid

Raymond Chen
Raymond Chen

Nowadays, computers have so much memory that running out of RAM is rarely the cause for an "out of memory" error. Actually, let's try that again. For over a decade, hard drive have been so large (and cheap) that running out of swap space is rarely the cause for an "out of memory" error. In user-mode, the term memory refers to virtual memory,...

Code
Jun 27, 2013
Post comments count0
Post likes count0

Once you return from the WM_ENDSESSION message, your process can be terminated at any time

Raymond Chen
Raymond Chen

A customer had a program which performed some final I/O operations as it exited. Various C++ objects deleted files or flushed buffers as part of their destructors. The customer found that if their program was left running when the user shut down Windows, then the files never got deleted, and the buffers were never flushed. On the other hand, if the...

Code
Jun 26, 2013
Post comments count0
Post likes count0

The default error mode (SetErrorMode) is not zero

Raymond Chen
Raymond Chen

A customer put the following code at the start of their program: // If this assertion fires, then somebody else changed the error mode // and I just overwrote it with my error mode. ASSERT(SetErrorMode(SEM_FAILCRITICALERRORS) == 0); The customer wanted to know whether it was a valid assumption that the initial error mode for a process is zero....

Code
Jun 24, 2013
Post comments count0
Post likes count0

Drawing content at a fixed screen position, regardless of window position

Raymond Chen
Raymond Chen

Today's Little Program draws content at a fixed screen position. The idea is that the window is really a viewport into some magical world. Unfortunately, our magical world just has a sign that says "Booga booga." Creating a more interesting magical world is left as an exercise. Start with our scratch program and make these changes: void OnMov...

Code
Jun 21, 2013
Post comments count0
Post likes count0

Of what use is the RDW_INTERNALPAINT flag?

Raymond Chen
Raymond Chen

For motivational purposes, let's start with a program that displays a DWM thumbnail. Start with the scratch program and add the following: #include <dwmapi.h> HWND g_hwndThumbnail; HTHUMBNAIL g_hthumb; void UpdateThumbnail(HWND hwndFrame, HWND hwndTarget) { if (g_hwndThumbnail != hwndTarget) { g_hwndThumbnail = hwndTarget; if (g...

Code
Jun 19, 2013
Post comments count0
Post likes count1

AttachThreadInput is like taking two threads and pooling their money into a joint bank account, where both parties need to be present in order to withdraw any money

Raymond Chen
Raymond Chen

Consider this code: // Code in italics is wrong foregroundThreadId = ::GetWindowThreadProcessId(::GetForegroundWindow(), 0); myThreadId = GetCurrentThreadId(); if (foregroundThreadId != myThreadId) { AttachThreadInput(foregroundThreadId, myThreadId, TRUE); BringWindowToTop(myWindowHandle); If you try to step ov...

Code
Jun 17, 2013
Post comments count0
Post likes count0

Displaying a property sheet for multiple files

Raymond Chen
Raymond Chen

Today's Little Program will show a property sheet that covers multiple files, just like the one you get from Explorer if you multi-select a bunch of files and right-click them all then select Properties. In fact, that description of how you do the operation interactively maps directly to how you do the operation programmatically! #define UNICO...

Code
Jun 14, 2013
Post comments count0
Post likes count0

A big little program: Monitoring Internet Explorer and Explorer windows, part 3: Tracking creation and destruction

Raymond Chen
Raymond Chen

Last time, we listener for window navigations. Today we'll learn about tracking window creation and destruction. The events to listen to are the DShell­Windows­Events. The Window­Registered event fires when a new window is created, and the Window­Revoked event fires when a window is destroyed. The bad news is that the paramet...

Code