Jump to content

dllcall


layer
 Share

Recommended Posts

hey guys, trying to make a little paint application using dllcall and need a little help :"> heres my code so far to see if i could even paint in the window...

#include <GUIConstants.au3>
$hwnd= GUICreate ("paint some stuff :)", 300, 400)
GUISetState ()
GUISetCursor (3)
$getdc= DllCall ("user32.dll", "int", "GetDC", "hwnd", $hwnd)
Func _IsPressed($hexKey)

  
  Local $aR, $bRv
  $hexKey = '0x' & $hexKey
  $aR = DllCall("user32", "int", "GetAsyncKeyState", "int", $hexKey)
 
  If $aR[0] <> 0 Then
     $bRv = 1
  Else
     $bRv = 0
  EndIf
  
  Return $bRv
EndFunc 
DLLCall ("user32.dll", "int", "ReleaseDC", "hwnd", $hwnd, "int", $getdc)
While 1
   $pos= MouseGetPos ()
   $get= GUIGetMsg ()
   If $get= -3 Then
      Exit
   EndIf
   If _IsPressed('01') = 1 Then
      $pen= DllCall ("Gdi32.dll", "int", "CreatePen", "int", $getdc, "int", "PS_SOLID", "int", 1, "int", 0xffffff) 
      $draw= DllCall ("Gdi32.dll", "int", "SetPixel", "int", $getdc, "int", $pos[0], "int", $pos[1], "int", 0xffffff)
   EndIf
   WEnd

i did some research on the dll functions up there but they don't seem to paint!?!?! could someone point out to me what im doing wrong? :idiot:

FootbaG
Link to comment
Share on other sites

  • Administrators

Before we even start working out if drawing on a window at the mouse position is possible (will probably get wiped as soon as you move the mouse) the following are wrong:

- You are calling ReleaseDC before the while loop

- IsPressed('01')? What key is 0x01?

- You are Creating a pen lots of times without freeing it.

Edit: Also from the docs on CreatePen:

After an application creates a logical pen, it can select that pen into a device context by calling the SelectObject function. After a pen is selected into a device context, it can be used to draw lines and curves.

Link to comment
Share on other sites

pretty please :idiot:

<{POST_SNAPBACK}>

@layer

You're trying to run before you can walk. You don't seem to understand how to use DllCall properly so if I want to help you with what you are doing, I will have to hold your hand through all the basics.

Anyway, I start by quoting this section of the help entry for DllCall

If the function call fails then @error is set to 1. Otherwise an array is returned that contains the function return value and a copy of all the parameters (including parameters that the function may have modified).

$return[0]= function return value

$return[1] = param1

$return[2] = param2

...

$return[n] = paramn

Therefore, the real DC after your GetDC call is $getdc[0]

Next, you should put your _IsPressed UDF and ReleaseDC call after your main program loop. When passing a string to DllCall such as "PS_SOLID", you should specify "str" as the type description and not "int"

Amended code below:

#include <GUIConstants.au3>
$hwnd= GUICreate ("paint some stuff :)", 300, 400)
GUISetState ()
GUISetCursor (3)
$getdc= DllCall ("user32.dll", "int", "GetDC", "hwnd", $hwnd)
While 1
   $pos= MouseGetPos ()
   $get= GUIGetMsg ()
   If $get= -3 Then
      Exit
   EndIf
   If _IsPressed('01') = 1 Then
      $pen= DllCall ("Gdi32.dll", "int", "CreatePen", "int", $getdc[0], "str", "PS_SOLID", "int", 1, "int", 0xffffff) 
      $draw= DllCall ("Gdi32.dll", "int", "SetPixel", "int", $getdc[0], "int", $pos[0], "int", $pos[1], "int", 0xffffff)
   EndIf
   WEnd
DLLCall ("user32.dll", "int", "ReleaseDC", "hwnd", $hwnd, "int", $getdc[0])

Func _IsPressed($hexKey)  
  Local $aR, $bRv
  $hexKey = '0x' & $hexKey
  $aR = DllCall("user32", "int", "GetAsyncKeyState", "int", $hexKey)

  If $aR[0] <> 0 Then
     $bRv = 1
  Else
     $bRv = 0
  EndIf
  
  Return $bRv
EndFunc

I have only corrected the mistakes I spotted in your code. The rest is up to you.

Link to comment
Share on other sites

  • Administrators

When passing a string to DllCall such as "PS_SOLID", you should specify "str" as the type description and not "int"

PS_SOLID is a int value, he needs to find out what it is from a windows header file. :idiot:
Link to comment
Share on other sites

theres one thing i dont get... (maybe many in your point of view) the return type...

Help File says...

Type Details

none no value (only valid for return type - equivalent to void in C)

short a 16 bit integer

int a 32 bit integer

long a 32 bit integer

short_ptr a pointer to a 16 bit integer 

int_ptr a pointer to a 32 bit integer

long_ptr a pointer to a 32 bit integer

str a string

wstr a wide character string (converted to/from an ANSI string during the call)

hwnd a window handle

ptr a general pointer (void *)

could someone just please explain what those mean? i think i know int, str, and hwnd... but the rest, i have no clue

EDIT: cause like, what would be the return type of SelectObject... hwnd right? :idiot: could someone please verify

EDIT: Return Values

If the selected object is not a region and the function succeeds, the return value is a handle to the object being replaced. If the selected object is a region and the function succeeds, the return value is one of the following values.

:"> thats from msdn just to let ye know

Edited by layer
FootbaG
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...