Move form with no titlebar

Hi, I am using Microsoft Visual C++ 2010 express edition. I made a totally transparent window with nothing but a picture on it. I want it where if I click the picture I can drag and move the form. I've been googling for a while but I can't find c++ code where I can do it in.
I see how to make it move once, but I don't see how to make it move while draging it. I'm not very good at c++ yes :/
In Win32, this is how I do it - to the window's procedure, add variables to hold the mouse click position, and add handlers for WM_LBUTTONDOWN, WM_LBUTTONUP, and WM_MOUSEMOVE messages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    //Add these two static variables
    static int xClick;
    static int yClick;

    //...other declarations...

    switch (message)
    {
        //...other handlers...

	case WM_LBUTTONDOWN:
            //Restrict mouse input to current window
	    SetCapture(hWnd);

            //Get the click position
	    xClick = LOWORD(lParam);
	    yClick = HIWORD(lParam);
	    break;

	case WM_LBUTTONUP:
            //Window no longer requires all mouse input
	    ReleaseCapture();
	    break;

	case WM_MOUSEMOVE:
	{
	    if (GetCapture() == hWnd)  //Check if this window has mouse input
	    {
                //Get the window's screen coordinates
	        RECT rcWindow;
		GetWindowRect(hWnd,&rcWindow);

                //Get the current mouse coordinates
		int xMouse = LOWORD(lParam);
		int yMouse = HIWORD(lParam);

                //Calculate the new window coordinates
                int xWindow = rcWindow.left + xMouse - xClick;
                int yWindow = rcWindow.top + yMouse - yClick;

                //Set the window's new screen position (don't resize or change z-order)
		SetWindowPos(hWnd,NULL,xWindow,yWindow,0,0,SWP_NOSIZE|SWP_NOZORDER);
	    }
	    break;
	}
         
        //...rest of switch block...
    }

    //...rest of procedure...
}
Why debugging it I get the following error
Form1.h(104): error C3641: 'ProjectA::WndProc' : invalid calling convention '__stdcall ' for function compiled with /clr:pure or /clr:safe

It's gibberish to me :/
Is your window procedure a member function? Do you use a static window procedure to pass messages to it?
I honestly don't know. The code is auto generated for the form. How do I check if it is either of those things?

Edit: I didn't word it well the first time lol.
Last edited on
I know nothing about forms, sorry. How are you processing window messages? What is the function where you added jrohde's code?
I added it to the very end. And I'm not sure how I'm processing messages lol.
The very end of what? Would you care to post the function?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#pragma once
#include <Windows.h>
#include <iostream>
#include <fstream>
int varx;
int vary;

namespace ProjectA {
	using namespace std;
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			ifstream in;
			in.open("Settings.txt");
			in >> varx;
			in >> vary;
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

	private: System::Windows::Forms::PictureBox^  pictureBox1;

	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
			this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
			this->SuspendLayout();
			// 
			// pictureBox1
			// 
			this->pictureBox1->BackColor = System::Drawing::Color::Transparent;
			this->pictureBox1->Cursor = System::Windows::Forms::Cursors::Hand;
			resources->ApplyResources(this->pictureBox1, L"pictureBox1");
			this->pictureBox1->Name = L"pictureBox1";
			this->pictureBox1->TabStop = false;
			this->pictureBox1->Click += gcnew System::EventHandler(this, &Form1::pictureBox1_Click);
			// 
			// Form1
			// 
			this->AccessibleRole = System::Windows::Forms::AccessibleRole::None;
			this->AllowDrop = true;
			resources->ApplyResources(this, L"$this");
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackColor = System::Drawing::SystemColors::Window;
			this->ControlBox = false;
			this->Controls->Add(this->pictureBox1);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;
			this->MaximizeBox = false;
			this->MinimizeBox = false;
			this->Name = L"Form1";
			this->ShowIcon = false;
			this->SizeGripStyle = System::Windows::Forms::SizeGripStyle::Show;
			this->TopMost = true;
			this->TransparencyKey = System::Drawing::Color::Transparent;
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
			this->ResumeLayout(false);

		}

#pragma endregion
	private: System::Void pictureBox1_Click(System::Object^  sender, System::EventArgs^  e) {
			 }
	};
	LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    //Add these two static variables
    static int xClick;
    static int yClick;

    //...other declarations...

    switch (message)
    {
        //...other handlers...

	case WM_LBUTTONDOWN:
            //Restrict mouse input to current window
	    SetCapture(hWnd);

            //Get the click position
	    xClick = LOWORD(lParam);
	    yClick = HIWORD(lParam);
	    break;

	case WM_LBUTTONUP:
            //Window no longer requires all mouse input
	    ReleaseCapture();
	    break;

	case WM_MOUSEMOVE:
	{
	    if (GetCapture() == hWnd)  //Check if this window has mouse input
	    {
                //Get the window's screen coordinates
	        RECT rcWindow;
		GetWindowRect(hWnd,&rcWindow);

                //Get the current mouse coordinates
		int xMouse = LOWORD(lParam);
		int yMouse = HIWORD(lParam);

                //Calculate the new window coordinates
                int xWindow = rcWindow.left + xMouse - xClick;
                int yWindow = rcWindow.top + yMouse - yClick;

                //Set the window's new screen position (don't resize or change z-order)
		SetWindowPos(hWnd,NULL,xWindow,yWindow,0,0,SWP_NOSIZE|SWP_NOZORDER);
	    }
	    break;
	}
         
        //...rest of switch block...
    }

    //...rest of procedure...
}
	}


That is the entire thing
I don't know anything about Windows forms, but it looks like you're mixing it with raw Win32 API. You need to understand what you're doing.
That's a problem, because I don't know where to learn so I can understand what I'm doing.
I see. Well, first things first. You should get comfortable with programming and a programming language before you move on to Windows programming. If you choose C++, I recommend reading Programming Principles and Practices Using C++. It's a big book but it'll give you a solid foundation to build on.

After that, you can look for a book on Windows programming, or try and pick it up from the documentation on MSDN.
I suck at learning from books so I want to work on this as my way of learning :/

Anyway I figured out after searching through all the properties I could find, how to switch how it compiles to /clr instead of /clr:safe(do they have difference syntaxs or something?) and it now says

error C4716: 'WndProc' : must return a value

So why is WnProc not returning a value?
I apologize for misreading your original post - you did say "form", so I should have taken that as a hint. My method is, of course, for straight Win32 and would work similarly in MFC, but I have never used Windows Forms.
It does not matter since WndProc will never be called in an MFC application(it is handled internally by MFC).

I suck at learning from books so I want to work on this as my way of learning


I am sorry, but this is not going to work. C++ is a pretty complex language that takes MONTHS to master. You NEED to get a C++ book(or at the VERY least read the tutorial on this site) in order to understand the language.
I did the tutorial on this site, but I haven't programmed hardly anything in a while, and I use to only do very basic stuff to learn a little.
Topic archived. No new replies allowed.