473,799 Members | 3,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Drawing to a window's title bar problems

I'm trying to add another button to a window's title bar to use as a "keep
this window on top" button. I've managed to draw the button using the code
below (C#) but I'm having a couple of other problems...

....
private bool buttonChecked = false;
private int buttonSize = 16;
private int buttonX = 226;
private int buttonY = 6;

[DllImport("user 32.dll")]
public static extern IntPtr GetWindowDC(Int Ptr hWnd);
[DllImport("user 32.dll")]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user 32.dll")]
public static extern int ReleaseDC(IntPt r hWnd, IntPtr hDC);

public const int WM_NCPAINT = 0x085;
public const int WM_NCLBUTTONDOW N = 0x0A1;
public const int WM_NCLBUTTONUP = 0x0A2;
....

....
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCPAINT:
base.WndProc (ref m);
IntPtr hDC = GetWindowDC(m.H Wnd);
Graphics g = Graphics.FromHd c(hDC);
PaintNC(m.HWnd) ;
g.Dispose();
ReleaseDC(m.HWn d, hDC);
m.Result = IntPtr.Zero;
break;
case WM_NCLBUTTONDOW N:
base.WndProc (ref m);
Point pd = new Point((int)m.LP aram);
int xPos = pd.X - this.Location.X ;
int yPos = pd.Y - this.Location.Y ;
if (xPos > buttonX && xPos < (buttonX + buttonSize) && yPos > buttonY &&
yPos < (buttonY + buttonSize))
{
IntPtr hDCa = GetWindowDC(m.H Wnd);
Graphics gra = Graphics.FromHd c(hDCa);
buttonChecked = !buttonChecked;
}
break;
default:
base.WndProc(re f m);
break;
}
}

protected void PaintNC(System. IntPtr hWnd)
{
IntPtr hDC = GetWindowDC(hWn d);
Graphics g = Graphics.FromHd c(hDC);
if (buttonChecked)
ControlPaint.Dr awButton(g, buttonX, buttonY, buttonSize, buttonSize,
ButtonState.Che cked);
else
ControlPaint.Dr awButton(g, buttonX, buttonY, buttonSize, buttonSize,
ButtonState.Nor mal);
g.Dispose();
ReleaseDC(hWnd, hDC);
}
....

The first problem I'm having is that is doesn't use the Window theme
anymore for the window. It uses the classic win98 style theme with the
gradient title bar.

The second problem is that when I click the button it doesn't refresh the
window so it doesn't show that it's pushed it. I have to minimise and then
maximise the window to see the changes. I tried using the invalidate() method
but that only works in the client area of the window.

Any ideas?

Darrell

Nov 16 '05 #1
1 5094
Things to check:
1. The MSDN example on WM_NCPAINT uses GetDCEx to get the proper HDC, not
GetWindowDC
2. You dispose of the Graphics object created out of the DC, and then you
explicitely release the DC... I suspect you can actually end up releasing
the DC twice which is obviously not a good thing to do.
3. You always return zero as the result of processing WM_NCPAINT. Make sure
the DefWindowProc returns the same value - otherwise you can potentially
prevent the system from doing some more work, for example painting the theme
graphics. Sounds a bit far-fetched, but I'd still check the return value
just to be safe.
The second problem is that when I click the button it doesn't refresh the
window so it doesn't show that it's pushed it. I have to minimise and then
maximise the window to see the changes. I tried using the invalidate()
method
but that only works in the client area of the window.
As it should - Invalidate works for client area only. I didn't manage to
find a non-client counterpart - you can try to use SendMessage API directly
to send the WM_NCPAINT message to the window, but this is just a guess.

Also, take a look at this:

http://www.planet-source-code.com/vb...23250&lngWId=1
http://www.ActualTools.com/

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"redneon" <re*****@discus sions.microsoft .com> wrote in message
news:11******** *************** ***********@mic rosoft.com... I'm trying to add another button to a window's title bar to use as a "keep
this window on top" button. I've managed to draw the button using the code
below (C#) but I'm having a couple of other problems...

...
private bool buttonChecked = false;
private int buttonSize = 16;
private int buttonX = 226;
private int buttonY = 6;

[DllImport("user 32.dll")]
public static extern IntPtr GetWindowDC(Int Ptr hWnd);
[DllImport("user 32.dll")]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user 32.dll")]
public static extern int ReleaseDC(IntPt r hWnd, IntPtr hDC);

public const int WM_NCPAINT = 0x085;
public const int WM_NCLBUTTONDOW N = 0x0A1;
public const int WM_NCLBUTTONUP = 0x0A2;
...

...
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCPAINT:
base.WndProc (ref m);
IntPtr hDC = GetWindowDC(m.H Wnd);
Graphics g = Graphics.FromHd c(hDC);
PaintNC(m.HWnd) ;
g.Dispose();
ReleaseDC(m.HWn d, hDC);
m.Result = IntPtr.Zero;
break;
case WM_NCLBUTTONDOW N:
base.WndProc (ref m);
Point pd = new Point((int)m.LP aram);
int xPos = pd.X - this.Location.X ;
int yPos = pd.Y - this.Location.Y ;
if (xPos > buttonX && xPos < (buttonX + buttonSize) && yPos > buttonY &&
yPos < (buttonY + buttonSize))
{
IntPtr hDCa = GetWindowDC(m.H Wnd);
Graphics gra = Graphics.FromHd c(hDCa);
buttonChecked = !buttonChecked;
}
break;
default:
base.WndProc(re f m);
break;
}
}

protected void PaintNC(System. IntPtr hWnd)
{
IntPtr hDC = GetWindowDC(hWn d);
Graphics g = Graphics.FromHd c(hDC);
if (buttonChecked)
ControlPaint.Dr awButton(g, buttonX, buttonY, buttonSize, buttonSize,
ButtonState.Che cked);
else
ControlPaint.Dr awButton(g, buttonX, buttonY, buttonSize, buttonSize,
ButtonState.Nor mal);
g.Dispose();
ReleaseDC(hWnd, hDC);
}
...

The first problem I'm having is that is doesn't use the Window theme
anymore for the window. It uses the classic win98 style theme with the
gradient title bar.

The second problem is that when I click the button it doesn't refresh the
window so it doesn't show that it's pushed it. I have to minimise and then
maximise the window to see the changes. I tried using the invalidate()
method
but that only works in the client area of the window.

Any ideas?

Darrell


Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4993
by: Justin Koivisto | last post by:
Is there a way to create an alert-like message box that uses a custom image? Basically, I want to replace the default ! image with something else. -- Justin Koivisto - spam@koivi.com PHP POSTERS: Please use comp.lang.php for PHP related questions, alt.php* groups are not recommended.
3
24717
by: NeverLift | last post by:
But, if it's not open, I don't want to open it . . . using window.open will open it if it doesn't exist, even if the url in that open is null (the window is then empty -- but it's open). The situation is: A main window opens child windows one at a time as the user requests, each with its own name. The user may click in any child window or the main window to open a summary window, which has a name all windows know. I want to get that...
3
3546
by: John Bokma | last post by:
I have two windows in a frame. I want to be able that each can open a pop up window and that the handle to that window can be stored somewhere, so that each can talk to the pop up. is it possible that window 1 in a frameset calls a function in window 2, ie. something like: window 1: ....
3
8134
by: Russell | last post by:
I have a quirky issue that I believe involves timing and only 2 hairs left to pull. I have a modal dialog that is an IFrame. The IFrame contains another window - which contains the appropriate title. I am trying to change the title of the IFrame window to be that of the contained window title. If I uncomment the alert statement below - the title change works. Comment out the alert - and - no title change.
4
2987
by: Peter Kirk | last post by:
Hi, I want to open a popup-window which displays a list to the user. From this list, the user can select an item, and the data regarding this item is then transferred to some fields on the main window (and the popup then closes). But I am having several problems. I have an example "main.html" which opens an example "lookup.html". In the actual application "lookup.html" is generated dynamically be a server, and will have different lists...
19
4620
by: Eva | last post by:
Hi, I'm really really really new to javascripts and so i couldn't really comprehend thoroughly on the the previous posts regarding this issue. Anyways, I'm setting up a picture album site. When i click on a thumbnail or a picture, a larger picture pops up in a javascript window. But it has a space on the top and on the left of my larger pic in the pop-up window. How do i get rid of that? Also, i want the window to fit my my pic so...
1
1793
by: Andrew | last post by:
Hello Everyone I am receiving an error in an application I am working on. The application when its done will be a Dungeons and Dragons Network game. I am having problems with the Networked Canvas basically for drawing the dungeon maps If I initialize two of the Tkinter Canvas widgets with in the same window I can draw across the network, however if I open a second instance of the Application and initialize the canvas, on windows I recv
7
1823
by: joecap5 | last post by:
I have a main window from which I want to open a separate side menu window. I then want to create a list of items on that side menu by clicking on the item names in the main window. So far I am able to click on "Open Menu" and have the side menu open. I can then click on Items and have them added to the side menu using the innerHTML method or the node method. The trouble comes in when I try to have the side menu opened automatically...
15
4034
by: javelin | last post by:
I need to be able to create a javascript based drawing/signature box, and be able to save it. Can someone refer me to a script that will allow this, or even a 3rd party package for sale? It can't be Java or ActiveX. Thank you.
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10219
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10025
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.