Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8940037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:56:57+00:00 2026-06-15T10:56:57+00:00

I am having a problem calling DefWindowsProc on Windows 8 from a C# winform.

  • 0

I am having a problem calling DefWindowsProc on Windows 8 from a C# winform. I have this form that I need it to be dragabble from anywhwere inside the form.

Here is my code.

[DllImport("user32.dll")]
static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, UIntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern bool ReleaseCapture(IntPtr hwnd);

const uint WM_SYSCOMMAND = 0x112;
const uint MOUSE_MOVE = 0xF012;

public void Drag()
{
    DefWindowProc(this.Handle, WM_SYSCOMMAND, (UIntPtr)MOUSE_MOVE, IntPtr.Zero);
}


private void OnMainPanelMouseDown(object sender, MouseEventArgs e)
{
    Control ctrl = sender as Control;
    ReleaseCapture(ctrl.Handle);

    this.Drag(); // put the form into drag mode.
}

DefWindowProc always return 0 yet I am unable to drag my window. This call works on XP, Vista and 7 but not on 8. I am guessing it has something to do with the decleration of DefWindowProc that is not working well on Windows 8.

Please note that on Windows 8 I am building my application with the .NET 4.0 framework yet on other platforms I am using the 2.0 version to build the software.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:56:59+00:00Added an answer on June 15, 2026 at 10:56 am

    I achieved your functionality with a slightly different approach.

    I’m running Windows 8 and Visual Studio 2012.

    I tested my solution an all versions of the framework, versions 2, 3, 4, 4.5.

    Of course your main form will need to trap the mouse_down() method.

      private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(282, 253);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.mouse_down);
            this.ResumeLayout(false);
    
        }
    

    First I defined a class called UnsafeNativeMethods:

    [SuppressUnmanagedCodeSecurity]
    internal static class UnsafeNativeMethods
    {
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;
    
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hwnd, uint Msg, int wParam, int lParam);
    
        [DllImport("User32.dll", EntryPoint = "ReleaseCapture")]
        public static extern bool ReleaseCapture();
    
    }
    

    Inside the form you wish to move with mouse from anywhere:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void mouse_down(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                UnsafeNativeMethods.ReleaseCapture();
                UnsafeNativeMethods.SendMessage(Handle, UnsafeNativeMethods.WM_NCLBUTTONDOWN, UnsafeNativeMethods.HT_CAPTION, 0);
            }
        }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem in calling a function that returns a result, from another
I am having a problem with a form that is inside of a jquery
I am having a problem calling a model property from my view. I may
I am having a problem with Vlad on windows. After calling rake vlad:deploy I
Im having problem in my UIscrollView ,this is what I have done: Whenever a
I'm having a big problem when calling a web service from my WPF application.
I'm having a problem with the a mysqli query I'm calling from a php
I'm having a problem with calling: HomeScreen.updateIcon(_Icon); or HomeScreen.setName(${app.title}); This throws an exception Module
I am having a problem calling string values from the strings.xml resource in Android.
I am having some problem accessing class instances. I am calling the class from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.