Recently, I found in my laptop that the battery icon of battery capacity often does not appear normally (although it is set to always display), and I have previously written the battery capacity display function on the PDA software, therefore, you can write a lightweight display of power on the desktop.Program. The API function getsystempowerstatus is used to query power consumption. This API function is not described here (see the API documentation for details ).
[Dllimport (Kernel32)]
Public static extern bool getsystempowerstatus (
Ref system_power_status psystempowerstatus
);
Initially, I used a progressbar and a label to display them separately. This is very simple. However, if the style attribute of progressbar does not work, it cannot be displayed as a continuous style. Instead, the progress is always displayed in a green small square (XP system). This display is not very accurate, so you decide to draw it by yourself. At the same time, the label is omitted and the percentage is drawn directly on the progress bar. So this leads to the following draw problem, how to make the drawing text always visible, using the following method ,:
Zoom in to 800%:
We place the window in this position. When a window is maximized, it is close to the right of the title bar, but it does not block controlbuttons. (For example)
To obtain the display size, use the getsystemmatrix function in VC and use the screen. primaryscreen. bounds attribute in C.
The string in is split into two parts. Therefore, we first prepare two pictures, bitmap1 and 2, respectively using different foreground and background. When the progress bar is in the middle of the text, we paste the image twice.
The size of the percentage string can be measured:
Sizef = graphics. measurestring (text, font)
Start position (center) of the string to be drawn ):
Startx = (this. width-sizef.width)/2;
Starty = (this. height-sizef.height)/2;
Note that integer coordinates must be used instead of Float coordinates when creating a texture. Otherwise, interpolation of the image is generated, resulting in blurring.
This is a window program, so in most cases it should be a window that is "Transparent" to the mouse message, that is, the mouse penetration function. Because the program is very small, we do not set a separate option to control it. This function is implemented by setting window styles using API functions,CodeAs follows:
// Set to mouse penetration
Setwindowlong (this. Handle, gwl_exstyle,
Getwindowlong (this. Handle, gwl_exstyle) | ws_ex_transparent | ws_ex_layered );
Features to be added in the future include: allow movement (because there is no title bar, some windows message processing skills are required ).
Source code:
Http://files.cnblogs.com/hoodlum1980/PowerStatus.rar
Appendix: Maintenance History
2008.01.16: Multiple icon switches are added to the notification bar. The charging prompt is displayed. When the power usage is lower than 25%, the notification bar icon flashes.
2008.01.18: The mouse penetration function.
2008.01.22: fixed the bug that the interface could not be updated when the power was refreshed immediately.
2008.01.24: two tray icons are added to make it more reasonable to select the icons in the notification tray area (based on the actual remaining power.
2008.12.16: the icon is added when the AC power supply is online, and the outline is drawn to better suit the battery appearance.