473,671 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What am I missing? (painting directly on screen dc using GDI API calls)


Am trying to paint a simple rectangle directly on the screen DC
using the code below, but nothing happens?!?

Weird. Anyone who can tell me what I am doing wrong? This
ought to be straightforward , but ... :(

TIA,

Joergen Bech

---

'Start new project, add a button to a form, then paste the following
'code:

Private Declare Function GetDesktopWindo w Lib "user32" () As Int32
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Int32)
As Int32
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As
Int32, ByVal hdc As Int32) As Int32
Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As
Int32, ByVal hObject As Int32) As Int32
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As
Int32, ByVal nWidth As Int32, ByVal crColor As Int32) As Int32
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject
As Int32) As Int32
Private Declare Function Rectangle Lib "gdi32" (ByVal hDC As
Int32, ByVal X1 As Int32, ByVal Y1 As Int32, ByVal X2 As Int32, ByVal
Y2 As Int32) As Int32
Private Declare Function CreateBrushIndi rect Lib "gdi32" (ByRef
lpLogBrush As LOGBRUSH) As Int32

Private Structure LOGBRUSH
Dim lbColor As Int32
Dim lbHatch As Int32
Dim lbStyle As Int32
End Structure

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
Test()
End Sub

Private Sub Test()
Dim newBrush As Int32
Dim newPen As Int32
Dim oldBrush As Int32
Dim oldPen As Int32

Dim dl As Int32
Dim lb As LOGBRUSH

' Get the desktop size in pixels.
Dim desktop_win As Int32 = GetDesktopWindo w()
Dim desktop_dc As Int32 = GetDC(desktop_w in)

Debug.WriteLine (desktop_win)
Debug.WriteLine (desktop_dc)

' Do something with the desktop device context
lb.lbStyle = 0 'BS_SOLID
lb.lbColor = 0
lb.lbHatch = 0

newBrush = CreateBrushIndi rect(lb)
newPen = CreatePen(0, 10, 0) 'Style=PS_SOLID

oldBrush = SelectObject(de sktop_dc, newBrush)
oldPen = SelectObject(de sktop_dc, newPen)

Rectangle(deskt op_dc, 0, 0, 100, 200)

dl = SelectObject(de sktop_dc, oldPen)
dl = SelectObject(de sktop_dc, oldBrush)

DeleteObject(ne wPen)
DeleteObject(ne wBrush)

' Release the bitmap's and desktop's DCs.
ReleaseDC(deskt op_win, desktop_dc)

End Sub
Nov 21 '05 #1
3 2654
Joergen,
Am trying to paint a simple rectangle directly on the screen DC
using the code below, but nothing happens?!?


I believe you want to use GetDC(0) rather than
GetDC(GetDeskto pWindow()).

And if you care about 64-bit compatibility, you really should use
IntPtr rather than Int32 for all handle parameters.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
On Tue, 04 Jan 2005 16:45:15 +0100, Mattias Sjögren
<ma************ ********@mvps.o rg> wrote:
Joergen,
Am trying to paint a simple rectangle directly on the screen DC
using the code below, but nothing happens?!?
I believe you want to use GetDC(0) rather than
GetDC(GetDeskt opWindow()).


Yep. That was it. I got the GetDesktopWindo w() code from
a screen grabber program. Worked great for the screen grabber,
but not for me.
And if you care about 64-bit compatibility, you really should use
IntPtr rather than Int32 for all handle parameters.


OK. Will do. Thanks. I was wondering about that. Seems like
Integer/IntPtr/Int32 are used randomly depending on what
sample code one is looking at. I just picked one.

I converted all handles to IntPtr, but this meant that I could not
use GetDC(0) as the compiler complained about not being able
to convert an integer to intptr.

I used GetDC(Nothing) instead. Check the revised code/definitions
below. Is this correct? Seems to work now.

/Joergen Bech

----

Private Declare Function GetDesktopWindo w Lib "user32" () As
IntPtr
Private Declare Function GetDC Lib "user32" (ByVal hwnd As IntPtr)
As IntPtr
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As
IntPtr, ByVal hdc As IntPtr) As IntPtr
Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As
IntPtr, ByVal hObject As IntPtr) As IntPtr
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As
Int32, ByVal nWidth As Int32, ByVal crColor As Int32) As IntPtr
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject
As IntPtr) As Int32
Private Declare Function Rectangle Lib "gdi32" (ByVal hDC As
IntPtr, ByVal X1 As Int32, ByVal Y1 As Int32, ByVal X2 As Int32, ByVal
Y2 As Int32) As Int32
Private Declare Function CreateBrushIndi rect Lib "gdi32" (ByRef
lpLogBrush As LOGBRUSH) As IntPtr

Private Structure LOGBRUSH
Dim lbColor As Int32
Dim lbHatch As Int32
Dim lbStyle As Int32
End Structure

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
Test()
End Sub

Private Sub Test()
Dim newBrush As IntPtr
Dim newPen As IntPtr
Dim oldBrush As IntPtr
Dim oldPen As IntPtr

Dim dl As IntPtr
Dim lb As LOGBRUSH

' Get the desktop size in pixels.
Dim desktop_dc As IntPtr = GetDC(Nothing)

' Do something with the desktop device context
lb.lbStyle = 0 'BS_SOLID
lb.lbColor = 0
lb.lbHatch = 0

newBrush = CreateBrushIndi rect(lb)
newPen = CreatePen(0, 10, 0) 'Style=PS_SOLID

oldBrush = SelectObject(de sktop_dc, newBrush)
oldPen = SelectObject(de sktop_dc, newPen)

Rectangle(deskt op_dc, 0, 0, 100, 200)

dl = SelectObject(de sktop_dc, oldPen)
dl = SelectObject(de sktop_dc, oldBrush)

DeleteObject(ne wPen)
DeleteObject(ne wBrush)

' Release the bitmap's and desktop's DCs.
ReleaseDC(Nothi ng, desktop_dc)

End Sub

Nov 21 '05 #3
Joergen,
OK. Will do. Thanks. I was wondering about that. Seems like
Integer/IntPtr/Int32 are used randomly depending on what
sample code one is looking at. I just picked one. Integer is an alias for Int32 so you can use them interchangeable . I
normally use Integer for VB.NET code, while I will use Int32 for API calls.
IntPtr represents a platform independent pointer. For the current 32-bit
version of .NET it is a 32-bit pointer, for the 64-bit version of .NET
(available as part of VS.NET 2005, aka Whidbey, due out later in 2005)
IntPtr will be a 64-bit pointer.

I converted all handles to IntPtr, but this meant that I could not
use GetDC(0) as the compiler complained about not being able
to convert an integer to intptr.
I used GetDC(Nothing) instead. Check the revised code/definitions
below. Is this correct? Seems to work now.
I normally IntPtr.Zero when I need to pass a 0 to an IntPtr, Nothing will
work also, as Nothing represents the "default" value for the type, IntPtr is
a structure, so Nothing = 0 for it...

Hope this helps
Jay

"Joergen Bech @ post1.tele.dk>" <jbech<NOSPAMNO SPAM> wrote in message
news:lt******** *************** *********@4ax.c om... On Tue, 04 Jan 2005 16:45:15 +0100, Mattias Sjögren
<ma************ ********@mvps.o rg> wrote:
Joergen,
Am trying to paint a simple rectangle directly on the screen DC
using the code below, but nothing happens?!?


I believe you want to use GetDC(0) rather than
GetDC(GetDesk topWindow()).


Yep. That was it. I got the GetDesktopWindo w() code from
a screen grabber program. Worked great for the screen grabber,
but not for me.
And if you care about 64-bit compatibility, you really should use
IntPtr rather than Int32 for all handle parameters.


OK. Will do. Thanks. I was wondering about that. Seems like
Integer/IntPtr/Int32 are used randomly depending on what
sample code one is looking at. I just picked one.

I converted all handles to IntPtr, but this meant that I could not
use GetDC(0) as the compiler complained about not being able
to convert an integer to intptr.

I used GetDC(Nothing) instead. Check the revised code/definitions
below. Is this correct? Seems to work now.

/Joergen Bech

----

Private Declare Function GetDesktopWindo w Lib "user32" () As
IntPtr
Private Declare Function GetDC Lib "user32" (ByVal hwnd As IntPtr)
As IntPtr
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As
IntPtr, ByVal hdc As IntPtr) As IntPtr
Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As
IntPtr, ByVal hObject As IntPtr) As IntPtr
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As
Int32, ByVal nWidth As Int32, ByVal crColor As Int32) As IntPtr
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject
As IntPtr) As Int32
Private Declare Function Rectangle Lib "gdi32" (ByVal hDC As
IntPtr, ByVal X1 As Int32, ByVal Y1 As Int32, ByVal X2 As Int32, ByVal
Y2 As Int32) As Int32
Private Declare Function CreateBrushIndi rect Lib "gdi32" (ByRef
lpLogBrush As LOGBRUSH) As IntPtr

Private Structure LOGBRUSH
Dim lbColor As Int32
Dim lbHatch As Int32
Dim lbStyle As Int32
End Structure

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
Test()
End Sub

Private Sub Test()
Dim newBrush As IntPtr
Dim newPen As IntPtr
Dim oldBrush As IntPtr
Dim oldPen As IntPtr

Dim dl As IntPtr
Dim lb As LOGBRUSH

' Get the desktop size in pixels.
Dim desktop_dc As IntPtr = GetDC(Nothing)

' Do something with the desktop device context
lb.lbStyle = 0 'BS_SOLID
lb.lbColor = 0
lb.lbHatch = 0

newBrush = CreateBrushIndi rect(lb)
newPen = CreatePen(0, 10, 0) 'Style=PS_SOLID

oldBrush = SelectObject(de sktop_dc, newBrush)
oldPen = SelectObject(de sktop_dc, newPen)

Rectangle(deskt op_dc, 0, 0, 100, 200)

dl = SelectObject(de sktop_dc, oldPen)
dl = SelectObject(de sktop_dc, oldBrush)

DeleteObject(ne wPen)
DeleteObject(ne wBrush)

' Release the bitmap's and desktop's DCs.
ReleaseDC(Nothi ng, desktop_dc)

End Sub

Nov 21 '05 #4

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

Similar topics

16
44590
by: BOOGIEMAN | last post by:
Beginners question, but really what can you do with it ? How hard is Python to learn compared with other languages (let's say C#). Can you make fullscreen game with it (for example) ? I've looked at http://www.python.org but nothing concrete there
121
10041
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
5
15114
by: Alien | last post by:
I have a hex editor-type class that extends UserControl and paints its data to a PictureBox. Basically the problem is that repainting it takes usually between 60 and 80ms, which may seem pretty fast but is not good enough when you have to repaint very frequently. For example, when you scroll the control or select blocks of text quickly. I have it paint its graphics to an offscreen Graphics instance, which I then transfer to the...
126
4284
by: ramyach | last post by:
Hi friends, I need to write a parallel code in 'C' on the server that is running SGI Irix 6.5. This server supports MIPS Pro C compiler. I don't have any idea of parallel C languages. I looked into few posts in this group. I could make out that there are several languages for parallel programming and parallel C is one of them. I need to know if this is supported by MIPS Pro C Compiler. Or are there any other parallel C languages that have...
2
1802
by: Carl Gilbert | last post by:
Hi I am developing a custom on screen keyboard. So far I have an array of buttons and then using SendKeys to send the text of the button to the active control to receive the text. The only problem is that the application is rather slow at painting the buttons. Ideally I wanted to allow the user to re-size the control and change the text and style of the buttons without such rendering issues. (I have a tool to re-size the buttons,...
0
1270
by: Litani | last post by:
Hi everyone, We have a winform application that uses webservices. It has list screens. The user selects a row from the grid and an edit screen is presented to the user with the selected row. Switching from the list screen to edit is slow if I have decent amount of controls. I can see the screen painting. In some cases, I can see the application painting some controls read only or disabled. Any suggestions on how to display the...
6
1524
by: BJ | last post by:
I just started this week on a new project. The existing project uses BEA Tuxedo as a second layer service broker. The clients make calls to the Tux services which in turn retrieves data from an Oracle DB. My job is to support the current application and possibly upgrade teh client app to .Net. I have a few options: I can rewrite the client desktop application .in Dot Net (FW3.0) utilizing the same Tux calls or Write a new desktop...
15
2056
by: jim | last post by:
Maybe I'm missing something, but it doesn't look like Microsoft writes a lot of apps in .Net (although they certainly push it for others). What does MS write using pure .Net? If applications like Symantec's antivirus, NeatReciepts or Franklin Covey's PlanPlus for Windows is any guide, .Net applications are slow and clunky. But, maybe the developers of these apps simply don't know how to write a decent app with .Net.
4
1865
by: Marina Levit | last post by:
I am trying to do some processing on a background thread while keeping the UI painting. However, this is a generic server side call routine - and it needs to block until the server side call completes. So the idea is, a server side call begins, it gets spun off on another thread to keep the UI painting - but, the code flow cannot return to the caller until the server side call completes, because the caller expects the data. The only way...
0
8476
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
8821
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8598
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
8670
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
7437
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
5696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4407
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2812
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.